-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02.cpp
More file actions
300 lines (259 loc) · 7.75 KB
/
02.cpp
File metadata and controls
300 lines (259 loc) · 7.75 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
int* Locate_Laser(Mat image) {
Mat lst = image.col(image.cols / 2).clone();
Mat_<uchar> m1 = lst;
int idx_1 = 0, idx_2 = 0, idx_3 = 0;
int max = 0, temp = 0, j = 0;
int y_1 = 0, y_2 = 0;
int dist = 50;
// find idx_1
max = 0; temp = 0; j = 0;
for (j = 0; j < image.rows; j++) {
temp = m1(j, 0);
//cout << (int)temp << endl;
if (temp > max) {
max = temp;
idx_1 = j;
}
}
// find idx_2
y_1 = idx_1 - dist > 0 ? idx_1 - dist : 0;
y_2 = idx_1 + dist < image.rows ? idx_1 + dist : image.rows;
for (j = y_1; j < y_2; j++) {
m1(j, 0) = 0;
}
max = 0; temp = 0;
for (j = 0; j < image.rows; j++) {
temp = m1(j, 0);
if (temp > max) {
max = temp;
idx_2 = j;
}
}
// find idx_3
y_1 = idx_2 - dist > 0 ? idx_2 - dist : 0;
y_2 = idx_2 + dist < image.rows ? idx_2 + dist : image.rows;
for (j = y_1; j < y_2; j++) {
m1(j, 0) = 0;
}
max = 0; temp = 0;
for (j = 0; j < image.rows; j++) {
temp = m1(j, 0);
if (temp > max) {
max = temp;
idx_3 = j;
}
}
int* idx_ = new int[3];
idx_[0] = idx_1;
idx_[1] = idx_2;
idx_[2] = idx_3;
return idx_;
}
double get_center(Mat img) {
Scalar mean = cv::mean(img);
int matMean = round(mean.val[0]);
double sum1 = 0;
double sum2 = 0;
int i = 0; int temp = 0;
int ind = 0;
for (i; i < img.rows; i++) {
temp = img.ptr<uchar>(i)[0];
if (temp > matMean) {
sum2 = sum2 + (double)temp;
}
}
sum2 = sum2 / 2;
i = 0;
for (i; i < img.rows; i++) {
temp = img.ptr<uchar>(i)[0];
if (temp > matMean) {
sum1 = sum1 + (double)temp;
ind = i;
if (sum1 > sum2) {
break;
}
}
}
double value = ind + 1 - (sum1 - sum2) / (double)img.at<uchar>(ind, 0);
return value;
}
int extract_right(int index1, Mat& img, double* laser_position, int gap, int zones_1, int zones_2) {
int i = 0;
uchar* ptr = (uchar*)img.data;
for (i = img.cols / 2; i < img.cols; i++) {
int max = 0; int temp = 0;
int j = 0; int m_index_1 = 0; int m_index_2 = 0;
for (j = index1 - zones_1; j < index1 + zones_1; j++) {
temp = *(ptr + j * img.cols + i);
if (temp > max) {
max = temp;
m_index_1 = j;
m_index_2 = j;
}
else if (temp == max) {
m_index_2 = j;
}
else {}
}
// cout << m_index_1 << ' ' << m_index_2 << endl;
index1 = (m_index_1 + m_index_2) / 2;
Mat lst2 = img(Range(index1 - zones_2, index1 + zones_2), Range(i, i + 1));
double center = get_center(lst2);
laser_position[i] = center + (double)index1 - (double)zones_2;
double maxvalue = 0, minvalue = 0;
Point max_ind, min_ind;
Mat_<uchar> m2 = lst2;
minMaxLoc(m2, &minvalue, &maxvalue, &min_ind, &max_ind);
if (maxvalue - minvalue < gap) {
return i;
}
}
return 0;
}
int extract_left(int index1, Mat& img, double* laser_position, int gap, int zones_1, int zones_2) {
int k = 0; int i = 0;
uchar* ptr = (uchar*)img.data;
for (k = img.cols / 2 + 1; k < img.cols + 1; k++) {
i = img.cols - k;
int max = 0; int temp = 0;
int j = 0; int m_index_1 = 0; int m_index_2 = 0;
for (j = index1 - zones_1; j < index1 + zones_1; j++) {
temp = *(ptr + j * img.cols + i);
if (temp > max) {
max = temp;
m_index_1 = j;
m_index_2 = j;
}
else if (temp == max) {
m_index_2 = j;
}
}
index1 = (m_index_1 + m_index_2) / 2;
Mat lst2 = img(Range(index1 - zones_2, index1 + zones_2), Range(i, i + 1));
double center = get_center(lst2);
laser_position[i] = center + index1 - zones_2;
double maxvalue = 0, minvalue = 0;
Point max_ind, min_ind;
Mat_<uchar> m2 = lst2;
minMaxLoc(m2, &minvalue, &maxvalue, &min_ind, &max_ind);
if (maxvalue - minvalue < gap) {
return i;
}
}
return 0;
}
double sum_(double* lst, int begin, int end) {
int i = begin;
double sum = 0;
for (i; i < end; i++) {
sum += lst[i];
}
return sum;
}
int main() {
String path = ".\\0.bmp";
Mat img = imread(path, COLOR_BGR2GRAY);
clock_t start, end;
start = clock();
// Locate_Laser
double t1 = (double)getTickCount();
int* idx_ = Locate_Laser(img);
t1 = ((double)getTickCount() - t1) / getTickFrequency();
// array storing laser line
double t2_1 = (double)getTickCount();
double laser_position_1[4200] = { 0 };
double laser_position_2[4200] = { 0 };
double laser_position_3[4200] = { 0 };
// define parameters
int gap = 20; int zones_1 = 20; int zones_2 = 20;
t2_1 = ((double)getTickCount() - t2_1) / getTickFrequency();
// extract_right
double t2_2 = (double)getTickCount();
int laser_1_right = extract_right(idx_[0], img, laser_position_1, gap, zones_1, zones_2);
int laser_2_right = extract_right(idx_[1], img, laser_position_2, gap, zones_1, zones_2);
int laser_3_right = extract_right(idx_[2], img, laser_position_3, gap, zones_1, zones_2);
t2_2 = ((double)getTickCount() - t2_2) / getTickFrequency();
// extract_left
double t2_3 = (double)getTickCount();
int laser_1_left = extract_left(idx_[0], img, laser_position_1, gap, zones_1, zones_2);
int laser_2_left = extract_left(idx_[1], img, laser_position_2, gap, zones_1, zones_2);
int laser_3_left = extract_left(idx_[2], img, laser_position_3, gap, zones_1, zones_2);
t2_3 = ((double)getTickCount() - t2_3) / getTickFrequency();
// smooth
double t3 = (double)getTickCount();
double laser_position_1_new[4200] = { 0 };
double laser_position_2_new[4200] = { 0 };
double laser_position_3_new[4200] = { 0 };
int size = 20;
int i = 0;
for (i = laser_1_left + size; i < laser_1_right - size; i++) {
laser_position_1_new[i] = sum_(laser_position_1, i - size, i + size) / (size * 2);
}
for (i = laser_2_left + size; i < laser_2_right - size; i++) {
laser_position_2_new[i] = sum_(laser_position_2, i - size, i + size) / (size * 2);
}
for (i = laser_3_left + size; i < laser_3_right - size; i++) {
laser_position_3_new[i] = sum_(laser_position_3, i - size, i + size) / (size * 2);
}
t3 = ((double)getTickCount() - t3) / getTickFrequency();
end = clock();
cout << "time = " << double(end - start) << "ms" << endl;
cout << "locate time = " << t1 * 1000 << "ms" << endl;
cout << "extract time 1 = " << t2_1 * 1000 << "ms" << endl;
cout << "extract time 2 = " << t2_2 * 1000 << "ms" << endl;
cout << "extract time 3 = " << t2_3 * 1000 << "ms" << endl;
cout << "smooth time = " << t3 * 1000 << "ms" << endl;
// release sourse
delete[] idx_;
// result preview
Mat img_new(img.rows, img.cols, CV_8UC1, Scalar(0));
for (int i = 0; i < img.cols; i++) {
if (laser_position_1[i] != 0) {
img_new.at<uchar>((int)laser_position_1_new[i], i) = 255;
}
if (laser_position_2[i] != 0) {
img_new.at<uchar>((int)laser_position_2_new[i], i) = 255;
}
if (laser_position_3[i] != 0) {
img_new.at<uchar>((int)laser_position_3_new[i], i) = 255;
}
}
imwrite(".\\output.jpeg", img_new);
// sub pixel result preview
Size dsize = Size(img.cols, 10 * img.rows);
Mat img_show;
resize(img, img_show, dsize, 0, 0, INTER_AREA);
Mat img_showRGB;
cvtColor(img_show, img_showRGB, COLOR_GRAY2RGB);
for (int i = 0; i < 4200; i++) {
double a = laser_position_1_new[i];
double b = laser_position_2_new[i];
double c = laser_position_3_new[i];
if (laser_position_1[i] != 0) {
img_showRGB.at<Vec3b>((int)(a * 10), i)[0] = 0;
img_showRGB.at<Vec3b>((int)(a * 10), i)[1] = 255;
img_showRGB.at<Vec3b>((int)(a * 10), i)[2] = 0;
}
if (laser_position_2[i] != 0) {
img_showRGB.at<Vec3b>((int)(b * 10), i)[0] = 0;
img_showRGB.at<Vec3b>((int)(b * 10), i)[1] = 255;
img_showRGB.at<Vec3b>((int)(b * 10), i)[2] = 0;
}
if (laser_position_3[i] != 0) {
img_showRGB.at<Vec3b>((int)(c * 10), i)[0] = 0;
img_showRGB.at<Vec3b>((int)(c * 10), i)[1] = 255;
img_showRGB.at<Vec3b>((int)(c * 10), i)[2] = 0;
}
}
imwrite(".\\output_sub_pixel.jpeg", img_showRGB);
system("pause");
return 0;
}