-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtextlineextract.cpp
More file actions
271 lines (199 loc) · 7.51 KB
/
textlineextract.cpp
File metadata and controls
271 lines (199 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <opencv2/opencv.hpp>
#include <random>
#define PI 3.14
using namespace cv;
using namespace std;
const int DIAMETER_VALUES[] = {64, 128, 256};
void mserExtractor(Mat&, vector<cv::Rect>&);
void getLinePointsAtDeg(int, cv::Point, int, cv::Point&, cv::Point&);
cv::Point getRectCenterPoint(cv::Rect);
void getLinePointsAtMidPoint(int, int, int, int, cv::Point&, cv::Point&, cv::Point&);
void projectionProfile(cv::Rect, vector<cv::Rect>, int, int, Mat&, Mat&);
bool rectInLine(cv::Rect, cv::Point, cv::Point);
bool rectInCircle(cv::Rect, cv::Point, int);
int main(int argc, char** argv) {
Mat in_grey_img = imread(argv[1], IMREAD_GRAYSCALE);
vector<cv::Rect> mser_bbox;
// Original Photo Display
mserExtractor(in_grey_img, mser_bbox);
// Estimate CC states
int num_ccs = mser_bbox.size();
int num_orientations = 32; /* Orientation angles are quantized to 32 levels */
int num_scales = 10; /* Set of scales: {12.8, 16.0, 21.3, 25.6, 32.0, 42.7, 51.2, 64.0, 85.3, 128.0} */
int num_labels = num_orientations * num_scales;
GCoptimizationGeneralGraph gc = new GCoptimizationGeneralGraph(num_ccs, num_labels);
// TODO: Set the neighbors to the Deaunay triangulation
//namedWindow("Projection Profile");
namedWindow("Image");
imshow("Image", in_grey_img);
waitKey(0);
// Main Loop
srand(time(NULL));
while(1) {
Mat img_region, projection_img;
in_grey_img.copyTo(img_region);
int rand_rect_ind = rand() % mser_bbox.size();
int rand_deg = rand() % 360;
int rand_diameter = DIAMETER_VALUES[rand() % 3];
cv::Rect rand_cc = mser_bbox[rand_rect_ind];
cout << "Degree: " << rand_deg << endl;
cout << "Diameter: " << rand_diameter << endl;
projectionProfile(rand_cc, mser_bbox, rand_diameter, rand_deg, img_region, projection_img);
cout << endl;
imshow("Image", img_region);
imshow("Projection Profile", projection_img);
int key_code = waitKey(0);
//cout << "The KeyCode pressed was " << key_code << endl;
if (key_code == 1048689) {
break;
}
}
}
void projectionProfile(cv::Rect cc, vector<cv::Rect> mser_bbox, int diameter, int degree, Mat& img_region, Mat& projection_img) {
cv::Point center = getRectCenterPoint(cc);
cv::Point p1, p2;
int radius = diameter / 2;
vector<cv::Rect> ccs_in_region;
/* collecting connected components that are in the circle region */
for (cv::Rect r : mser_bbox) {
if (rectInCircle(r, center, diameter)) {
ccs_in_region.push_back(r);
}
}
rectangle(img_region, cc, Scalar(0), -1);
circle(img_region, center, radius, Scalar(0), 5);
getLinePointsAtDeg(degree, center, radius, p1, p2);
line(img_region, p1, p2, Scalar(0), 5);
LineIterator proj_axis(img_region, p1, p2);
vector<int> histogram(proj_axis.count, 0);
cout << "Iterator length: " << proj_axis.count << endl;
for (int i = 0; i < proj_axis.count; i++, proj_axis++) {
cv::Point poi = proj_axis.pos();
cv::Point start_point, end_point;
getLinePointsAtMidPoint(i, proj_axis.count, radius, degree, poi, start_point, end_point);
for (cv::Rect r : ccs_in_region) {
if (rectInLine(r, start_point, end_point)) {
histogram[i] += 1;
}
}
// Testing Purposes
//if (i % 40 == 0) {
// line(img_region, start_point, end_point, Scalar(0), 5);
// circle(img_region, poi, 5, Scalar(255), -1);
// imshow("Image", img_region);
// waitKey(0);
//}
}
projection_img = Mat(diameter*2, diameter, CV_8UC1, Scalar(0));
float proj_scale = static_cast<float>(histogram.size()) / diameter;
for(int i = 0; i < diameter; i++) {
int hist_ind = i * proj_scale;
cv::Point bottomPoint = cv::Point(i, diameter*2);
cv::Point topPoint = cv::Point(i, (diameter*2) - histogram[hist_ind]);
line(projection_img, bottomPoint, topPoint, Scalar(255));
}
//img_region.at<uchar>(p1) = 255;
}
double dataCostFn(int p, int l) {
CCState cc = CCState(p, l);
//cv::Point center = getRectCenterPoint(cc);
cv::Point p1, p2;
int radius = diameter / 2;
vector<cv::Rect> ccs_in_region;
/* collecting connected components that are in the circle region */
for (cv::Rect r : mser_bbox) {
if (rectInCircle(r, center, diameter)) {
ccs_in_region.push_back(r);
}
}
rectangle(img_region, cc, Scalar(0), -1);
circle(img_region, center, radius, Scalar(0), 5);
getLinePointsAtDeg(degree, center, radius, p1, p2);
line(img_region, p1, p2, Scalar(0), 5);
LineIterator proj_axis(img_region, p1, p2);
vector<int> histogram(proj_axis.count, 0);
cout << "Iterator length: " << proj_axis.count << endl;
for (int i = 0; i < proj_axis.count; i++, proj_axis++) {
cv::Point poi = proj_axis.pos();
cv::Point start_point, end_point;
getLinePointsAtMidPoint(i, proj_axis.count, radius, degree, poi, start_point, end_point);
for (cv::Rect r : ccs_in_region) {
if (rectInLine(r, start_point, end_point)) {
histogram[i] += 1;
}
}
}
double v1;
double v2;
return v1 + v2;
}
bool rectInLine(cv::Rect r, cv::Point start, cv::Point end) {
bool cond1, cond2;
float slope = static_cast<float>(end.y - start.y) / (end.x - start.x);
float intercept = static_cast<float>(end.y) - slope * end.x;
cv::Point top_left, top_right, bottom_left, bottom_right;
top_left = r.tl();
top_right = r.tl();
top_right.x += r.width;
bottom_right = r.br();
bottom_left = r.br();
bottom_left.x -= r.width;
cond1 = (static_cast<float>(bottom_left.y) > slope * bottom_left.x + intercept) != \
(static_cast<float>(top_right.y) > slope * top_right.x + intercept);
cond2 = (static_cast<float>(bottom_right.y) > slope * bottom_right.x + intercept) != \
(static_cast<float>(top_left.y) > slope * top_left.x + intercept);
return (cond1 || cond2);
}
bool rectInCircle(cv::Rect r, cv::Point center, int radius) {
cv::Point r_center = getRectCenterPoint(r);
return pow(r_center.x - center.x, 2) + pow(r_center.y - center.y, 2) < pow(radius, 2);
}
void getLinePointsAtMidPoint(int ind_pos, int line_it_count, int radius, int deg, cv::Point& poi, cv::Point& start, cv::Point& end) {
int x = radius * 2 * (static_cast<float>(ind_pos) / line_it_count) - radius;
int y = sqrt(pow(radius, 2) - pow(x, 2));
int del_x = y * cos(static_cast<float>((90 + deg) % 360) * PI / 180.0);
int del_y = -y * sin(static_cast<float>((90 + deg) % 360) * PI / 180.0);
//if (ind_pos % 40 == 0) {
// cout << "\tdelta x = " << del_x << endl;
// cout << "\tdelta y = " << del_y << endl;
// cout << "\tx = " << x << endl;
// cout << "\ty = " << y << endl;
// cout << endl;
//}
start.x = poi.x + del_x;
start.y = poi.y + del_y;
end.x = poi.x - del_x;
end.y = poi.y - del_y;
}
void getLinePointsAtDeg(int degree, cv::Point center, int radius, cv::Point& p1, cv::Point& p2) {
int del_x, del_y;
del_x = radius * cos(static_cast<float> (degree) * PI / 180.0);
del_y = - radius * sin(static_cast<float> (degree) * PI / 180.0);
//cout << "del_x = " << del_x / static_cast<float>(N) << endl;
//cout << "del_y = " << del_y / static_cast<float>(N) << endl;
p1.x = center.x + del_x;
p1.y = center.y + del_y;
p2.x = center.x - del_x;
p2.y = center.y - del_y;
}
cv::Point getRectCenterPoint(cv::Rect r) {
return (r.br() + r.tl())*0.5;
}
void mserExtractor(Mat& image, vector<cv::Rect>& mser_bbox) {
Ptr<MSER> mserExtractor = MSER::create();
vector<vector<cv::Point> > mserContours;
mserExtractor->detectRegions(image, mserContours, mser_bbox);
// Draw Contours and Bounding Boxes
for (cv::Rect r : mser_bbox) {
rectangle(image, r, Scalar(255));
}
for (vector<cv::Point> v : mserContours) {
for (cv::Point p : v) {
image.at<uchar>(p.y, p.x) = 255;
}
}
}