Line Detection

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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <iostream>
#include <raspicam/raspicam_cv.h>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;
typedef struct {
unsigned char r;
unsigned char g;
unsigned char b;
} Color;
typedef struct PosList {
int x;
int y;
struct PosList *next;
} PosList;
int colordiff(Color a, Color b)
{ int dr, dg, db;
dr = (int)((a.r < b.r) ? (b.r - a.r) : (a.r - b.r));
dg = (int)((a.g < b.g) ? (b.g - a.g) : (a.g - b.g));
db = (int)((a.b < b.b) ? (b.b - a.b) : (a.b - b.b));
return dr + dg + db;
}PosList* newnode(int x, int y)
{ PosList *pos;
pos = (PosList *)malloc(sizeof(PosList));
pos->x = x;
pos->y = y;
pos->next = NULL;
return pos;
}void delnode(PosList **pos)
{ free(*pos);
*pos = NULL;
}void pl_push(PosList **list, PosList *pos)
{ pos->next = *list;
*list = pos;
}PosList* pl_pop(PosList **list)
{ PosList *pos;
pos = *list;
*list = (*list)->next;
return pos;
}void dellist(PosList **list)
{ PosList *a, *b;
a = *list;
while (a != NULL) {
b = a->next;
delnode(&a);
a = b;
}
}int contains(PosList *list, int x, int y)
{ while (list != NULL) {
if (list->x == x && list->y == y)
return 1;
list = list->next;
}
return 0;
}void rgrow(IplImage *source, IplImage *dest, Color color, int threshold)
{ PosList *list_n;
PosList *node_r;
PosList *list_r;
Color curcolor;
int x, y;
int sx, sy;
int dx, dy;
int offset;
int mindiff, curdiff;
mindiff = 255 * 3;
for (y = 0; y < source->height; y++) {
for (x = 0; x < source->width; x++) {
offset = y * source->width * 3 + x * 3;
curcolor.b = source->imageData[offset];
curcolor.g = source->imageData[offset + 1];
curcolor.r = source->imageData[offset + 2];
curdiff = colordiff(color, curcolor);
if (curdiff < mindiff) {
sx = x;
sy = y;
mindiff = curdiff;
}
dest->imageData[y * dest->width + x] = 0;
}
}
list_n = newnode(sx, sy);
list_r = NULL;
int* map = new int[source->width*source->height];
for (int i = 0; i<source->width*source->height; i++)
map[i] = 0;
map[sy*source->width + sx] = 1;
while (list_n != NULL)
{
sx = list_n->x;
sy = list_n->y;
pl_push(&list_r, pl_pop(&list_n));
for (dy = -1; dy <= 1; dy++)
{
for (dx = -1; dx <= 1; dx++)
{
if (dx == 0 && dy == 0)
continue;
if (dx != 0 && dy != 0)
continue;
if (sx + dx == -1 || sx + dx == source->width ||
sy + dy == -1 || sy + dy == source->height)
continue;
if (map[(sy + dy)*source->width + sx + dx] == 1)
continue;
offset = (sy + dy) * source->width * 3 + (sx + dx) * 3;
curcolor.b = source->imageData[offset];
curcolor.g = source->imageData[offset + 1];
curcolor.r = source->imageData[offset + 2];
curdiff = colordiff(color, curcolor);
if (curdiff <= threshold)
{
pl_push(&list_n, newnode(sx + dx, sy + dy));
map[(sy + dy)*source->width + sx + dx] = 1;
}
}
}
}
delete [] map;
node_r = list_r;
while (node_r != NULL) {
dest->imageData[node_r->y * dest->width + node_r->x] = 255;
node_r = node_r->next;
}
dellist(&list_r);
}float sum1(std::vector<float>* x, std::vector<float>* y, float yCurr)
{ float sum = 0;
for (int i = 0; i < x->size(); i++)
{
sum += ((*x)[i] * ((*y)[i] - yCurr));
}
return sum / x->size()*-2;
}float sum2(std::vector<float>* x, std::vector<float>* y, float yCurr)
{ float sum = 0;
for (int i = 0; i < x->size(); i++)
{
sum += ((*y)[i] - yCurr);
}
return sum / x->size()*-2;
}void linearRegression(std::vector<float>* x, std::vector<float>* y, int nbData, float& b0, float& b1)
{ float xave = 0;
float yave = 0;
for (int i = 0; i < nbData; i++)
{
xave += (*x)[i];
yave += (*y)[i];
}
xave /= (float)nbData;
yave /= (float)nbData;
float a1 = 0;
float a2 = 0;
for (int i = 0; i < nbData; i++)
{
a1 += ((*x)[i] - xave)*((*x)[i] - xave);
a2 = a2 + ((*x)[i] - xave)*((*y)[i] - yave);
}
b1 = a2 / a1;
b0 = yave - b1*xave;
}void rgrow(Mat *source, Mat *dest, Color color, int threshold)
{ PosList *list_n;
PosList *node_r;
PosList *list_r;
Color curcolor;
int x, y;
int sx, sy;
int dx, dy;
int offset;
int mindiff, curdiff;
int width = source->cols;
int height = source->rows;
mindiff = 255 * 3;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
offset = y * width * 3 + x * 3;
curcolor.b = source->data[offset];
curcolor.g = source->data[offset + 1];
curcolor.r = source->data[offset + 2];
curdiff = colordiff(color, curcolor);
if (curdiff < mindiff) {
sx = x;
sy = y;
mindiff = curdiff;
}
dest->data[y * width + x] = 0;
}
}
list_n = newnode(sx, sy);
list_r = NULL;
int* map = new int[width*height];
for (int i = 0; i<width*height; i++)
map[i] = 0;
map[sy*width + sx] = 1;
while (list_n != NULL)
{
sx = list_n->x;
sy = list_n->y;
pl_push(&list_r, pl_pop(&list_n));
for (dy = -1; dy <= 1; dy++)
{
for (dx = -1; dx <= 1; dx++)
{
if (dx == 0 && dy == 0)
continue;
if (dx != 0 && dy != 0)
continue;
if (sx + dx == -1 || sx + dx == width ||
sy + dy == -1 || sy + dy == height)
continue;
if (map[(sy + dy)*width + sx + dx] == 1)
continue;
offset = (sy + dy) * width * 3 + (sx + dx) * 3;
curcolor.b = source->data[offset];
curcolor.g = source->data[offset + 1];
curcolor.r = source->data[offset + 2];
curdiff = colordiff(color, curcolor);
if (curdiff <= threshold)
{
pl_push(&list_n, newnode(sx + dx, sy + dy));
map[(sy + dy)*width + sx + dx] = 1;
}
}
}
}
delete[] map;
node_r = list_r;
while (node_r != NULL) {
dest->data[node_r->y * width + node_r->x] = 255;
node_r = node_r->next;
}
dellist(&list_r);
}void findCenterLine(Mat* img, Vec4i& l)
{ int imageHeight=img->rows;
int imageWidth=img->cols;
Mat dist(img->rows, img->cols, CV_8UC1);
Mat dist1(img->rows, img->cols, CV_8UC1);
distanceTransform(*img, dist, DIST_C, CV_DIST_MASK_PRECISE);
normalize(dist, dist, 0.0, 1.0, NORM_MINMAX);
std::vector<float> array;
if (dist.isContinuous())
{
array.assign((float*)dist.datastart, (float*)dist.dataend);
}
else
{
for (int i = 0; i < dist.rows; ++i) {
array.insert(array.end(), dist.ptr<float>(i), dist.ptr<float>(i) + dist.cols);
}
}
double max = 0;
for (int i = 0; i < imageHeight*imageWidth; i++)
{
if (array[i]>max)
{
max = array[i];
}
}
std::vector<float> x;
std::vector<float> y;
for (int i = 0; i < imageHeight; i++)
{
for (int j = 0; j < imageWidth; j++)
{
if (array[i*imageWidth+j]>max*0.1)
{
dist1.data[i*imageWidth + j]=255;
x.push_back((float)j);
y.push_back((float)i);
}
else
{
dist1.data[i*imageWidth + j] = 0;
}
}
}
float b0, b1;
linearRegression(&x, &y, x.size(), b0, b1);
//printf("%f %f\n",b0,b1);
l[0] = 0; l[1] = b0 + b1*l[0];
if(l[1]<=0)
{
l[1]=0;
l[0]=(l[1]-b0)/b1;
}
if(l[1]>=imageHeight)
{
l[1]=imageHeight;
l[0]=(l[1]-b0)/b1;
}
l[2] = imageWidth; l[3] = b0 + b1*l[2];
if(l[3]<=0)
{
l[3]=0;
l[2]=(l[3]-b0)/b1;
}
if(l[3]>=imageHeight)
{
l[3]=imageHeight;
l[2]=(l[3]-b0)/b1;
}
imshow("Dist", dist);
//imshow("Dist1", dist1);
}
int main (void)
{ int imageWidth = 640;
int imageHeight = 480;

raspicam::RaspiCam_Cv Camera;
Camera.set( CV_CAP_PROP_FORMAT, CV_8UC3);
Camera.set( CV_CAP_PROP_FRAME_WIDTH, imageWidth);
Camera.set( CV_CAP_PROP_FRAME_HEIGHT, imageHeight);
Mat orgImg;
Mat cvEdge;
Mat filteredImg(imageHeight, imageWidth, CV_8UC1);

if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;}
while(1)
{
Camera.grab();
Camera.retrieve(orgImg);
CV_Assert(orgImg.data);
blur(orgImg, orgImg, Size(10,10));
int threshold = 250;
Color color; color.r = 255; color.g = 0; color.b = 0;
rgrow(&orgImg, &filteredImg, color, threshold);

//Canny Edge detector
Canny(orgImg, cvEdge, 10, 10*3, 3);
//Fine center line
Vec4i lineSeg;
findCenterLine(&filteredImg, lineSeg);
line(orgImg, Point(lineSeg[0], lineSeg[1]), Point(lineSeg[2], lineSeg[3]), Scalar(255, 0, 255), 3, CV_AA);

imshow("Org", orgImg);
imshow("Filtered", filteredImg);
imshow("Edge", cvEdge);

if ( waitKey(20) == 27 )break; //ESC키 누르면 종료
}

Camera.release();
}