forked from Motu-Lootera/Task-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
197 lines (172 loc) · 4.25 KB
/
test.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
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <bits/stdc++.h>
using namespace cv;
using namespace std;
struct point //used for storing points
{
int x;
int y;
float d;
bool operator<(const point& rhs) const
{
return d > rhs.d;
}
};
Mat img = imread("Test1.png", 1); //Test Image
Mat vis(img.rows,img.cols,CV_8UC1,Scalar(0)); //for storing if pixel visited
//for storing distance.Initialised at greater than possible in the image
priority_queue<point> qu; //priority queue for implementing bfs
float dist[1080][1080];
// 2-d array of vectors storing points for shortest path
vector<point> sp[1080][1080];
void binary()
{
for (int i = 0; i < img.rows; ++i)
{
for (int j = 0; j < img.cols; ++j)
{
if(img.at<Vec3b>(i,j)[0]>100 && img.at<Vec3b>(i,j)[1]>100 && img.at<Vec3b>(i,j)[2]>100)
img.at<Vec3b>(i,j)={255,255,255};
if(img.at<Vec3b>(i,j)[0]<100 && img.at<Vec3b>(i,j)[1]<100 && img.at<Vec3b>(i,j)[2]<100)
img.at<Vec3b>(i,j)={0,0,0};
}
}
}
bool isValid(int i,int j)
{
if(i<0||j<0||i>=img.rows||j>=img.cols)
return 0;
if(img.at<Vec3b>(i,j)[0]==255 && img.at<Vec3b>(i,j)[1]==255 && img.at<Vec3b>(i,j)[2]==255)
return 0;
return 1;
}
/*void add_qu(point p)
{
for (int a = -1; a < 2; a++)
{
for (int b = -1; b < 2; b++)
{
// for only selectig the adjacent 4 valid pixels
if (isValid(p.x+a,p.y+b) && vis.at<uchar>(p.x+a,p.y+b)==0)
{
point tmp={p.x+a,p.y+b};
qu.push(tmp);
vis.at<uchar>(p.x+a,p.y+b)=255;
}
}
}
}
*/
void djik(int i,int j)
{
vis.at<uchar>(i,j)=255;
/*namedWindow("Image",WINDOW_NORMAL);
imshow("Image",vis);
waitKey(1);*/
//Main Code
for (int a = -1; a < 2; ++a)
{
for (int b = -1; b < 2; ++b)
{
if(isValid(i+a,j+b))
{
if((abs(a+b)==1) && (dist[i][j] + 1< dist[i+a][j+b]))
{
dist[i+a][j+b]=dist[i][j] + 1; //distance is updated
sp[i+a][j+b]={}; //vector is cleared out
//shortest path for (i,j) appended to sp for (i+a,j)
sp[i+a][j+b].insert(sp[i+a][j+b].begin(),sp[i][j].begin(),sp[i][j].end());
//finally (i+a,j) appended to the vector
sp[i+a][j+b].push_back({i+a,j+b,dist[i+a][j+b]});
qu.push({i+a,j+b,dist[i+a][j+b]});
}
else if (abs(a*b)==1 && (dist[i][j] + 1.414< dist[i+a][j+b]))
{
dist[i+a][j+b]=dist[i][j] + 1.414; //distance is updated
sp[i+a][j+b]={}; //vector is cleared out
//shortest path for (i,j) appended to sp for (i+a,j)
sp[i+a][j+b].insert(sp[i+a][j+b].begin(),sp[i][j].begin(),sp[i][j].end());
//finally (i+a,j) appended to the vector
sp[i+a][j+b].push_back({i+a,j+b,dist[i+a][j+b]});
qu.push({i+a,j+b,dist[i+a][j+b]});
}
}
}
}
}
point centre(int chnl)
{
Mat img2=img.clone();
int sumx=0, sumy=0, ctr=0;
for(int i=0; i<img.rows; i++){
for(int j=0; j<img.cols; j++){
if(img.at<Vec3b>(i,j)[0]>=220 && img.at<Vec3b>(i,j)[1]>=220 && img.at<Vec3b>(i,j)[2]>=220){
img2.at<Vec3b>(i,j)[0]=0;
img2.at<Vec3b>(i,j)[1]=0;
img2.at<Vec3b>(i,j)[2]=0;
}
}
}
for(int i=0; i<img.rows; i++){
for(int j=0; j<img.cols; j++){
if(img2.at<Vec3b>(i,j)[chnl] >= 230){
sumx += i;
sumy += j;
ctr++;
}
}
}
point centre = {sumx/ctr, sumy/ctr, (float)(img.rows*img.cols+200)};
return centre;
}
void path(int x,int y)
{
Mat img1=img.clone();
namedWindow("Path",WINDOW_NORMAL);
point a;
int i=0;
// a=sp[x][y][i];
// cout << sp[x][y].size() << endl;
while(a.x!=x || a.y!=y)
{
a=sp[x][y][i];
// cout << a.x << " " << a.y << endl;
imshow("Path",img1);
img1.at<Vec3b>(a.x,a.y)[1]=255;
i++;
waitKey(1);
}
img1.at<Vec3b>(x,y)[1]=255;
imshow("Path",img1);
printf("Distance b/w src & dest= %f\n",dist[x][y]);
waitKey(0);
}
int main()
{
binary();
point src, dest;
src= centre(1);
for (int i = 0; i < img.rows; ++i)
{
for (int j = 0; j < img.cols; ++j)
{
dist[i][j]=(img.rows*img.cols)+200;
}
}
dist[src.x][src.y]=0; // initial distance of src set to 0
sp[src.x][src.y].push_back({src.x,src.y}); //shortest path for src plugged
point curr={src.x,src.y,0};
qu.push(curr);
dest = centre(2);
while(!qu.empty())
{
priority_queue<point> it= qu;
djik(qu.top().x,qu.top().y);
qu.pop();
// add_qu(curr);
}
path(dest.x,dest.y);
return 0;
}