-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTechniche_MinimumCostPath.cpp
567 lines (453 loc) · 11.9 KB
/
Techniche_MinimumCostPath.cpp
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <fstream>
using namespace cv;
using namespace std;
int mouse_x[2], mouse_y[2];
int mouse_condition = 0;
//Filename to read image
char *fname = "9.jpg";
int rows, cols;
void getEdges();
//Code for curser in output starts
void CallBackMouse(int event, int x, int y, int flag, void *userdata)
{
if (event == EVENT_LBUTTONDOWN)
{
if (mouse_condition < 2)
{
mouse_x[mouse_condition] = x;
mouse_y[mouse_condition] = y;
mouse_condition++;
if (mouse_condition == 2)
{
getEdges();
}
}
cout << "MOUSE " << x << ' ' << y << endl;
}
}
// ends
void getColorImages(Mat image);
double CORNER_MIN_GAP;
/// Parameters for Shi-Tomasi algorithm
vector<Point2f> corners;
/// Global variables
Mat src, src_gray;
int maxCorners = 23;
int maxTrackbar = 100;
double edgeLen, edgeThres;
RNG rng(12345);
const char* source_window = "Image";
/// Function header
int getColorLine(int *, int, int, Point2f, Point2f);
//void goodFeaturesToTrack_Demo( int, void* );
double getDistance(Point2f, Point2f);
/**
* @function main
*/
int main()
{
/// Load source image and convert it to gray
src = imread(fname, 1);
cvtColor(src, src_gray, COLOR_BGR2GRAY);
CORNER_MIN_GAP = src.size().width / 20;
/// Create Window
namedWindow(source_window, WINDOW_AUTOSIZE);
setMouseCallback(source_window, CallBackMouse, NULL);
/// Create Trackbar to set the number of corners
//createTrackbar( "Max corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo );
//maxCorners = 96;
imshow(source_window, src);
//goodFeaturesToTrack_Demo( 0, 0 );
waitKey(0);
return 0;
}
double getDistance(Point2f a, Point2f b)
{
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
void makeAttached(int *A, int w, int h, int i, int j, int c)
{
if (*(A + i*w + j) == 1)
{
*(A + i*w + j) = c;
makeAttached(A, w, h, i + 1, j, c);
makeAttached(A, w, h, i, j + 1, c);
makeAttached(A, w, h, i, j - 1, c);
makeAttached(A, w, h, i - 1, j, c);
}
}
class Edge
{
public:
Point2f a, b;
int cost = 1, color;
int na, nb;
Edge(int i, int j, Point2f A, Point2f B, int c)
{
na = i;
nb = j;
a.x = A.x;
a.y = A.y;
b.x = B.x;
b.y = B.y;
color = c;
setCost();
}
Edge()
{
}
int getNum(Point2f p)
{
if ((p.x == a.x && p.y == a.y))
return na;
if (p.x == b.x && p.y == b.y)
return nb;
return -1;
}
Edge(Edge &e){
a = e.a;
b = e.b;
cost = e.cost;
color = e.color;
na = e.na;
nb = e.nb;
}
void setCost()
{
cost = getCost(color);
}
static int getCost(int color)
{
int cost = 1e8;
if (color == 0) cost = 25;
else if (color == 1) cost = 20;
else if (color == 2) cost = 10;
else if (color == 3) cost = 15;
return cost;
}
static int maxColor()
{
return 30;
}
int giveOther(int n)
{
if (na == n)
return nb;
if (nb == n)
return na;
return -1;
}
};
void getEdges()
{
IplImage* src = cvLoadImage(fname, 1); // Loading the image
IplImage* copy = cvCreateImage(cvGetSize(src), 8, 3); //Create a new image of,8 bit,3 channel
CvScalar s; // create two scalar variables
int height = src->height, width = src->width;
////////////CORNERS
corners.clear();
int *FC = new int[height*width];
for (int i = 0; i <height; i++)
for (int j = 0; j < width; j++)
{
s = cvGet2D(src, i, j); //Get the RGB values of src's i,j into a scalar s
//Detectiion OF WHITE INTERESECTIOn ********************************************************************************
if ((s.val[2]>150) && (s.val[1]>170) && (s.val[0]>180))
FC[i*width + j] = 1;
else FC[i*width + j] = 0;
}
//ALGO
int c = 2;
for (int i = 0; i <height; i++)
for (int j = 0; j < width; j++)
if (FC[i*width + j] == 1)
{
makeAttached(FC, width, height, i, j, c++);
}
cout << c << " Corners";
//return;
for (int num = 2; num < c; num++)
{
double x = 0, y = 0;
int n = 0;
for (int i = 0; i <height; i++)
for (int j = 0; j < width; j++)
if (FC[i*width + j] == num)
{
x += j; y += i; n++;
}
if (n == 0) continue;
x /= n; y /= n;
Point2f p;
p.x = x;
p.y = y;
corners.push_back(p);
}
//ALGO for detecting corners ENDS
delete[] FC;
/// Draw corners detected
// cout<<"** Number of corners detected: "<<corners.size()<<endl;
int r = 4;
for (size_t i = 0; i < corners.size(); i++)
{
for (size_t j = i + 1; j < corners.size(); j++)
{
if (getDistance(corners[i], corners[j]) < CORNER_MIN_GAP)
{
corners.erase(corners.begin() + j);
j--;
}
}
}
edgeLen = getDistance(corners[0], corners[1]);
for (size_t i = 0; i < corners.size(); i++)
for (size_t j = i + 1; j < corners.size(); j++)
edgeLen = min(edgeLen, getDistance(corners[i], corners[j]));
edgeThres = edgeLen *0.4;
Mat copy2;
copy2 = (::src).clone();
for (size_t i = 0; i < corners.size(); i++)
{
circle(copy2, corners[i], r, Scalar(255, 255, 0), -1, 8, 0);
}
/// Show what you got
namedWindow(source_window, WINDOW_AUTOSIZE);
//for ( size_t i = )
imshow(source_window, copy2);
/////////////////////CORNERS END
int *MAT = new int[4 * height*width];
for (int i = 0; i<height; i++)
for (int j = 0; j<width; j++)
{
MAT[0 * width*height + i*width + j] = 0;
MAT[1 * width*height + i*width + j] = 0;
MAT[2 * width*height + i*width + j] = 0;
MAT[3 * width*height + i*width + j] = 0;
//Detectiion OF Edges ********************************************************************************
//RED
s = cvGet2D(src, i, j); //Get the RGB values of src's i,j into a scalar s
if ((s.val[2]>150) && (s.val[1]<100) && (s.val[0]<100))
MAT[0 * width*height + i*width + j] = 1;
else
//GREEN
if ((s.val[2]<100) && (s.val[1]>100) && (s.val[0]<120))
MAT[1 * width*height + i*width + j] = 1;
else
//YELLOW
if ((s.val[2]>150) && (s.val[1]>150) && (s.val[0]<150))
MAT[2 * width*height + i*width + j] = 1;
else
//BLUE
if ((s.val[2]<100) && (s.val[1]<100) && (s.val[0]>100))
MAT[3 * width*height + i*width + j] = 1;
}
c = 0;
vector<Edge> edges;
for (size_t i = 0; i < corners.size(); i++)
for (size_t j = i + 1; j < corners.size(); j++)
if (fabs(getDistance(corners[i], corners[j]) - edgeLen)<edgeThres)
{
int color = getColorLine(MAT, width, height, corners[i], corners[j]);
if (color == -1) continue;
//cout << color << ' ' << corners[i].x << ' ' << corners[i].y << ' ' << corners[j].x << ' ' << corners[j].y << endl;
Scalar *s = NULL;
if (color == 0)
s = new Scalar(0, 0, 255);
else if (color == 1)
s = new Scalar(0, 255, 0);
else if (color == 2)
s = new Scalar(0, 255, 255);
else if (color == 3)
s = new Scalar(255, 0, 0);
line(copy2, corners[i], corners[j], *s, 2);
edges.push_back(Edge(i, j, corners[i], corners[j], color));
delete s;
c++;
}
imshow(source_window, copy2);
/***************Link Edges********/
/********** MINIMUM COST ALGO *************/
int Corner_N_Start = 0;
int Corner_N_End = 0;
Point2f mouseS, mouseE;
mouseS.x = mouse_x[0];
mouseE.x = mouse_x[1];
mouseS.y = mouse_y[0];
mouseE.y = mouse_y[1];
for (int i = 1; i < corners.size(); i++)
{
if (fabs(getDistance(corners[Corner_N_Start], mouseS))>fabs(getDistance(corners[i], mouseS)))
Corner_N_Start = i;
if (fabs(getDistance(corners[Corner_N_End], mouseE))>fabs(getDistance(corners[i], mouseE)))
Corner_N_End = i;
}
// for (int i = 0; i < edges.size(); i++)
// cout << edges[i].na << " " << edges[i].nb <<','<<edges[i].giveOther(2)<< endl;
vector<int> isDone(corners.size(), 0);
int MAX = edges.size() * Edge::maxColor();
vector<int> minCost(corners.size(), MAX);
vector<int> lastCorner(corners.size(), -1);
//make initally minCost = 0
minCost[Corner_N_Start] = 0;
int aa = 0;
while (1)
{
int minCorner = -1, minVAL = MAX;
for (int j = 0; j<corners.size(); j++)
if (minVAL >= minCost[j] && isDone[j] == 0)
{
minCorner = j; minVAL = minCost[j];
}
//cout << " M" << minCorner;
if (minCorner == -1) break;
isDone[minCorner] = 1;
//for (size_t j = 0; j < edges.size(); j++)
for (int j = 0; j < corners.size(); j++)
{
//cout << j << "> " << edges[j].na << ' ' << edges[j].nb << ';' <<minCorner<<": "<< edges[j].giveOther(minCorner)<<endl;
if (fabs(getDistance(corners[minCorner], corners[j]) - edgeLen)<edgeThres)
//if (edges[j].giveOther(minCorner) >= 0)
{
int other = j;
//int other = edges[j].giveOther(minCorner);
//if (other != -1)
if (!isDone[other])
{
int cost = Edge::getCost(getColorLine(MAT, width, height, corners[minCorner], corners[j]));
if (cost == 0) cost = MAX;
cout << cost << ' ';
if (minCost[minCorner] + cost <= minCost[other])
{
minCost[other] = minCost[minCorner] + cost;// edges[j].cost;
lastCorner[other] = minCorner;
//cout << "SET MIN " << other << " : " << minCost[other] << endl;
}
}
}
}
}
int current = Corner_N_End;
cout << " PATH" << endl;
vector<int> path;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5);
while (current != -1 && current != Corner_N_Start)
{
path.push_back(current);
cout << minCost[current] << '<'; // min Cost[current]-for cost of path and current - for vertex of path
String s = to_string(current);
// addText(copy2, s, corners[current], font);
current = lastCorner[current];
}
cout << Corner_N_Start;
path.push_back(Corner_N_Start);
for (int i = 0; i < path.size() - 1; i++)
line(copy2, corners[path[i]], corners[path[i + 1]], Scalar(255, 255, 255), 5);
imshow(source_window, copy2);
cout << "DONE\n" << edges[2].color << ' ' << edges[2].cost;
/********************************************** MOVING CONTROLS ******************/
/****
0 UP
1 RIGHT
2 DOWN
3 LEFT
*****/
ofstream fout("moves.txt");
fout << "int Path[]={";
int direction = 0;
for (int i = path.size() - 2; i >= 0; i--)
{
int motion = 0;//0 - fwd, -1 anticlock, +1 clockwise
double delta_x = corners[path[i]].x - corners[path[i + 1]].x;
double delta_y = corners[path[i]].y - corners[path[i + 1]].y;
int new_direction;
if (fabs(delta_x)>fabs(delta_y))
{
delta_y = 0;
}
else
{
delta_x = 0;
}
if (delta_x == 0)
{
if (delta_y > 0) new_direction = 2;
else new_direction = 0;
}
else {
if (delta_x > 0) new_direction = 1;
else new_direction = 3;
}
//cout << "> " <<delta_x<<','<<delta_y<<','<< new_direction<< "<";
if (new_direction == 1){
if (direction == 0)
fout << "1,";
if (direction == 2)
fout << "-1,";
if (direction == 3)
fout << "-1,-1,";
}
else if (new_direction == 2){
if (direction == 0)
fout << "1,1,";
if (direction == 1)
fout << "1,";
if (direction == 3)
fout << "-1,";
}
else if (new_direction == 3){
if (direction == 0)
fout << "-1,";
if (direction == 1)
fout << "1,1,";
if (direction == 2)
fout << "1,";
}
else if (new_direction == 0){
if (direction == 1)
fout << "-1,";
if (direction == 2)
fout << "1,1,";
if (direction == 3)
fout << "1,";
}
fout << "0,";
direction = new_direction;
}
fout << "100};\n";
fout.close();
/********************************************************************************/
/***********MINIMUM COST ALGO ENDS******************************/
delete[] MAT;
}
int getColorLine(int *MAT, int width, int height, Point2f a, Point2f b)
{
int MAX = 10; /**********************SAMPLE POINTS ON LINE **************************************************************/
int A[4] = { 0 };
for (int i = 1; i < MAX; i++)
{
double x = (a.x*i + b.x*(MAX - i)) / MAX;
double y = (a.y*i + b.y*(MAX - i)) / MAX;
for (int j = 0; j < 4; j++)
{
if (*(MAT + j*width*height + ((int)y)*width + (int)x))
{
A[j]++;
}
}
}
int in = 0;
int flag = 0;
for (int i = 1; i < 4; i++)
if (A[in] < A[i]) in = i;
// cout << A[0] << ":" << A[1] << ":" << A[2] << ":" << A[3];
if (A[in] == 0)
return -1;
return in;
}