-
Notifications
You must be signed in to change notification settings - Fork 0
/
minCut.cpp
438 lines (370 loc) · 9.95 KB
/
minCut.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
#include <iostream>
#include <queue>
#include <vector>
#include <list>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
enum connectivity // directions
{
N = 1, NE, E, SE, S, SW, W, NW
};
struct edge
{
Point pt;
float weight;
};
void initialMouseCallback(int, int, int, int, void*);
void finalMouseCallback(int, int, int, int, void*);
Point neighbour(Point, int, int, int);
bool bfs(vector<vector<list<edge>>>, Point, Point, vector<vector<Point>>*, int, int, int maxCapacity = 0);
float getEdgeWeight(vector<vector<list<edge>>>, Point, Point);
bool increaseEdgeWeight(vector<vector<list<edge>>>*, Point, Point, float);
bool decreaseEdgeWeight(vector<vector<list<edge>>>*, Point, Point, float);
void markCut(vector<vector<list<edge>>>, vector<vector<list<edge>>>, Point, Mat*);
int main(int argc, char** argv)
{
if(argc != 3)
{
cout << "Incorrect number of arguments" << endl;
cout << "Usage : ./minCut <path of image> 0/1" << endl;
cout << "0 for without capacity scaling, 1 for with capacity scaling" << endl;
return 0;
}
Mat input = imread(argv[1], IMREAD_COLOR);
Mat gray_input;
cvtColor(input, gray_input, COLOR_BGR2GRAY); // convert to grayscale (weighted formula)
Mat output(gray_input.rows, gray_input.cols, gray_input.type(), Scalar(0)); // initialize same sized image - all black
// black in output image means the pixel is not connected to any component
namedWindow("gray", WINDOW_NORMAL); // display grayscale image
imshow("gray", gray_input);
waitKey(100);
vector<Point> seeds;
cout << "Select a point from the foreground and background respectively and then press any key" << endl;
setMouseCallback("gray", initialMouseCallback, &seeds);
waitKey(0);
setMouseCallback("gray", finalMouseCallback, NULL);
vector<vector<list<edge>>> adjList; // adjacency list
vector<vector<Point>> parent; // 2-D parent array for recording path found using BFS
adjList.resize(gray_input.rows);
parent.resize(gray_input.rows);
for(int i = 0; i < gray_input.rows; i++)
{
adjList[i].resize(gray_input.cols);
parent[i].resize(gray_input.cols);
fill(parent[i].begin(), parent[i].end(), Point(-1, -1)); // initialize parent array
for(int j = 0; j < gray_input.cols; j++)
{
int curIntensity = gray_input.at<uchar>(i, j);
for(int k = 1; k <= 8; k+=2) // k++ implies 8-connectivity, k+=2 implies 4 connectivity
{
Point nbh = neighbour(Point(j, i), k, gray_input.cols, gray_input.rows); // neighbour
if(nbh.x != -1 && nbh.y != -1)
{
int adjIntensity = gray_input.at<uchar>(nbh);
edge temp;
temp.pt = nbh;
temp.weight = 256-abs(curIntensity - adjIntensity); // weight of edge; higher weight implies less difference in intensities
adjList[i][j].push_back(temp);
}
}
}
}
vector<vector<list<edge>>> originalAdjList(adjList); // copy adjacency list
if(atoi(argv[2]) == 0) // normal approach without capacity scaling
{
while(bfs(adjList, seeds[0], seeds[1], &parent, gray_input.rows, gray_input.cols)) // while there is a path from source to target (bfs funciton populates "parent")
{
count++;
float flow = FLT_MAX;
Point foo = seeds[1];
while(foo != seeds[0]) // calculating minimum of all weights in the path; equivalent to finding minimum/bottleneck capacity in the chosen path
{
Point fooParent = parent[foo.y][foo.x];
float edgeWeight = getEdgeWeight(adjList, fooParent, foo);
if(edgeWeight < 0)
{
cerr << "Error!";
return 0;
}
if(flow > edgeWeight)
{
flow = edgeWeight;
}
foo = fooParent;
}
foo = seeds[1];
while(foo != seeds[0]) // increase and decrease edge weights by amount "flow"- minimum weight of all edges, as found above
{
Point fooParent = parent[foo.y][foo.x];
increaseEdgeWeight(&adjList, foo, fooParent, flow);
decreaseEdgeWeight(&adjList, fooParent, foo, flow);
foo = fooParent;
}
for(int i = 0; i < gray_input.rows; i++) // reset parent array
{
fill(parent[i].begin(), parent[i].end(), Point(-1, -1));
}
}
}
else if(atoi(argv[2]) == 1) // capacity scaling approach
{
int maxCapacity = 256;
while(maxCapacity >= 1)
{
while(bfs(adjList, seeds[0], seeds[1], &parent, gray_input.rows, gray_input.cols, maxCapacity)) // while there is a path from source to target (bfs funciton populates "parent")
{
count++;
float flow = FLT_MAX;
Point foo = seeds[1];
while(foo != seeds[0]) // calculating minimum of all weights in the path; equivalent to finding minimum/bottleneck capacity in the chosen path
{
//cout << "(" << foo.y << "," << foo.x << ") ";
Point fooParent = parent[foo.y][foo.x];
float edgeWeight = getEdgeWeight(adjList, fooParent, foo);
if(edgeWeight < 0)
{
cerr << "Error!";
return 0;
}
if(flow > edgeWeight)
{
flow = edgeWeight;
}
foo = fooParent;
}
//cout << count << endl;
foo = seeds[1];
while(foo != seeds[0]) // increase and decrease edge weights by amount "flow"- minimum weight of all edges, as found above
{
Point fooParent = parent[foo.y][foo.x];
increaseEdgeWeight(&adjList, foo, fooParent, flow);
decreaseEdgeWeight(&adjList, fooParent, foo, flow);
foo = fooParent;
}
for(int i = 0; i < gray_input.rows; i++) // reset parent array
{
fill(parent[i].begin(), parent[i].end(), Point(-1, -1));
}
}
maxCapacity /= 2;
}
}
else
{
cout << "Incorrect argument for capacity scaling" << endl;
return 0;
}
markCut(originalAdjList, adjList, seeds[0], &output); // mark the cut in the output image
namedWindow("final", WINDOW_NORMAL);
imshow("final", output);
waitKey(0);
return 0;
}
void initialMouseCallback(int event, int x, int y, int flags, void* v) // record mouse clicks
{
if(event == EVENT_LBUTTONDOWN)
{
cout << x << " " << y << endl;
((vector<Point>*)v)->push_back(Point(x, y)); // enqueue
}
}
void finalMouseCallback(int event, int x, int y, int flags, void* userdata)
{
return;
}
Point neighbour(Point input, int direction, int cols, int rows) // calculates neighbour based on direction input
{
switch(direction)
{
case N:
{
input.y--;
break;
}
case NE:
{
input.x++;
input.y--;
break;
}
case E:
{
input.x++;
break;
}
case SE:
{
input.x++;
input.y++;
break;
}
case S:
{
input.y++;
break;
}
case SW:
{
input.x--;
input.y++;
break;
}
case W:
{
input.x--;
break;
}
case NW:
{
input.x--;
input.y--;
break;
}
}
if(input.x < 0 || input.x >= cols || input.y < 0 || input.y >= rows)
{
return Point(-1, -1);
}
return input;
}
bool bfs(vector<vector<list<edge>>> adjList, Point s, Point t, vector<vector<Point>>* parent, int rows, int cols, int maxCapacity)
{
bool** visited = new bool*[rows]; // 2-D "visited" array
for(int i = 0; i < rows; i++)
{
visited[i] = new bool[cols];
memset(visited[i], false, sizeof(bool) * cols);
}
queue<Point> q;
q.push(s);
visited[s.y][s.x] = true;
(*parent)[s.y][s.x] = Point(-1, -1);
while(!q.empty() && visited[t.y][t.x] != true)
{
Point temp = q.front();
q.pop();
auto it = adjList[temp.y][temp.x].begin();
while(it != adjList[temp.y][temp.x].end()) // iterate through neighbours of the node
{
if((*it).weight >= maxCapacity && !visited[(*it).pt.y][(*it).pt.x]) // if positive weight and neighbour has not been visited, enqueue, mark visited as true and mark parent
{
q.push((*it).pt);
visited[(*it).pt.y][(*it).pt.x] = true;
(*parent)[(*it).pt.y][(*it).pt.x] = temp;
}
it++;
}
}
bool returnValue = visited[t.y][t.x];
for(int i = 0; i < rows; i++)
{
delete [] visited[i];
}
delete [] visited;
return returnValue;
}
float getEdgeWeight(vector<vector<list<edge>>> adjList, Point u, Point v)
{
auto it = adjList[u.y][u.x].begin();
while(it != adjList[u.y][u.x].end())
{
if((*it).pt == v)
{
return (*it).weight;
}
it++;
}
return -1.0;
}
bool increaseEdgeWeight(vector<vector<list<edge>>>* adjList, Point u, Point v, float increase)
{
auto it = (*adjList)[u.y][u.x].begin();
while(it != (*adjList)[u.y][u.x].end())
{
if((*it).pt == v)
{
(*it).weight += increase;
return true;
}
it++;
}
}
bool decreaseEdgeWeight(vector<vector<list<edge>>>* adjList, Point u, Point v, float decrease)
{
auto it = (*adjList)[u.y][u.x].begin();
while(it != (*adjList)[u.y][u.x].end())
{
if((*it).pt == v)
{
(*it).weight -= decrease;
if((*it).weight < 0)
{
cerr << "Negative error!" << endl;
}
if((*it).weight < 0.00001) // assume weight is 0 and erase edge
{
(*adjList)[u.y][u.x].erase(it);
}
return true;
}
it++;
}
cerr << "Decrease error!" << endl;
return false;
}
void markCut(vector<vector<list<edge>>> originalAdjList, vector<vector<list<edge>>> adjList, Point s, Mat* output)
{
// initial BFS
bool** visited = new bool*[output->rows];
for(int i = 0; i < output->rows; i++)
{
visited[i] = new bool[output->cols];
memset(visited[i], false, sizeof(bool) * output->cols);
}
queue<Point> q;
q.push(s);
visited[s.y][s.x] = true;
while(!q.empty())
{
Point temp = q.front();
q.pop();
auto it = adjList[temp.y][temp.x].begin();
while(it != adjList[temp.y][temp.x].end())
{
if((*it).weight > 0 && !visited[(*it).pt.y][(*it).pt.x])
{
q.push((*it).pt);
visited[(*it).pt.y][(*it).pt.x] = true;
}
it++;
}
}
for(int i = 0; i < output->rows; i++)
{
for(int j = 0; j < output->cols; j++)
{
if(visited[i][j]) // nodes reachable from source vertex
{
auto it = originalAdjList[i][j].begin();
while(it != originalAdjList[i][j].end())
{
if(!visited[(*it).pt.y][(*it).pt.x]) // if it has an edge to a non-reachable vertex in the original graph, that edge is part of the min cut
{
output->at<uchar>(i, j) = 255;
output->at<uchar>((*it).pt.y, (*it).pt.x) = 255;
}
it++;
}
}
}
}
for(int i = 0; i < output->rows; i++)
{
delete [] visited[i];
}
delete [] visited;
}