/
main.cpp
executable file
·433 lines (309 loc) · 9.51 KB
/
main.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
#include "commonHeaders.h"
int x_coord=-1;
int y_coord=-1;
//Plots a green dot around point (x,y)
void plot(Mat img,int x,int y,int b, int g, int r){
int k=1;
unsigned char * imgPtr = (unsigned char *)img.data;
for(int j = y-k; j < y+k; j++){
for(int i = x-k; i < x+k; i++ ){
//Bounds check
if( j < 0 || j >= img.rows || i < 0 || i >= img.cols){
continue;
}
imgPtr[img.step*j+i*3+0] = b;
imgPtr[img.step*j+i*3+1] = g;
imgPtr[img.step*j+i*3+2] = r;
}
}
}
Mat toGray(Mat img){
//We only need one channel
Mat grayImg(img.rows, img.cols,CV_32FC1);
float * grayPtr = (float *)grayImg.data;
unsigned char * imgPtr = (unsigned char *) img.data;
int grayStep = grayImg.step/sizeof(float);
int imgStep = img.step/sizeof(unsigned char);
//Loop over the image pixels. Convert to float to avoid precision issues.
for(int y = 0; y < img.rows; y++){
for(int x = 0; x < img.cols; x++){
int idxGray = y*grayStep + x;
int idxImg = y*imgStep + x*3;
grayPtr[idxGray] = ((float)imgPtr[idxImg]+(float)imgPtr[idxImg+1]+(float)imgPtr[idxImg+2])/(3.0*100); //TODO take out scale factor 100
}
}
return grayImg;
}
Mat lap_dir(Mat img, int direction)
{
int cols = img.cols;
int rows = img.rows;
Mat img_filtered;
if (direction%2)
{
float vertical_fk[1][3] = {1,-2,1};
Mat filter_kernel = Mat(1, 3, CV_32FC1, vertical_fk);
filter2D(img, img_filtered, -1, filter_kernel);
}
else
{
float horizontal_fk[3][1] = {{1}, {-2}, {1}};
Mat filter_kernel = Mat(3, 1, CV_32FC1, horizontal_fk);
filter2D(img, img_filtered, -1, filter_kernel);
}
return img_filtered;
}
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
if ( event == EVENT_LBUTTONDOWN )
{
cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
x_coord=x;
y_coord=y;
}
if ( flags == (EVENT_FLAG_CTRLKEY + EVENT_FLAG_LBUTTON) )
{
cout << "Left mouse button is clicked while pressing CTRL key - position (" << x << ", " << y << ")" << endl;
x_coord=0;
y_coord=0;
}
}
void alignToPrevImage(Mat imgPrevUnAlligned,Mat imgNextUnAlligned, Mat &imgPrev,Mat &imgNext){
int rows=imgPrevUnAlligned.rows;
int cols=imgPrevUnAlligned.cols;
int chans=imgPrevUnAlligned.channels();
cout << "cols " << cols << " rows " << rows << " chans " << chans << endl;
unsigned char * imgPrevPtr = (unsigned char *)imgPrevUnAlligned.data;
unsigned char * imgNextPtr = (unsigned char *)imgNextUnAlligned.data;
int iOff = 0;
int jOff = 0;
int blockSize = 70;
int Wind = 100;
float s = 1.3;
int jStop = (rows/s + Wind);
int iStop = (cols/s + Wind);
int jStart = (rows/s - Wind);
int iStart = (cols/s - Wind);
int jRef = (jStart + jStop)/2;
int iRef = (iStart + iStop)/2;
//Create the window from which the template is to be matched.
Mat windImg;
Rect window(iRef-Wind/2,jRef-Wind/2,Wind,Wind);
imgNextUnAlligned(window).copyTo(windImg);
windImg = imgNextUnAlligned;
//namedWindow("window Image",CV_WINDOW_NORMAL);
//imshow("window Image",windImg);
//cvWaitKey(0);
//Create the template
Mat templateImage;
Rect temp(iRef-blockSize/2,jRef-blockSize/2,blockSize,blockSize);
imgPrevUnAlligned(temp).copyTo(templateImage);
//namedWindow("window Image",CV_WINDOW_NORMAL);
//imshow("window Image",templateImage);
//cvWaitKey(0);
int r_cols = windImg.cols-templateImage.cols+1;
int r_rows = windImg.rows-templateImage.rows+1;
Mat result(r_cols,r_rows,CV_32FC1);
matchTemplate(windImg,templateImage,result,CV_TM_CCORR_NORMED);
double minVal;
double maxVal;
Point minLoc;
Point maxLoc;
Point matchLoc;
minMaxLoc(result,&minVal,&maxVal,&minLoc,&maxLoc,Mat());
matchLoc = maxLoc;
//rectangle(windImg,matchLoc,Point(matchLoc.x+templateImage.cols,matchLoc.y+templateImage.rows),Scalar::all(0),2,4,0);
//rectangle(result,matchLoc,Point(matchLoc.x+templateImage.cols,matchLoc.y+templateImage.rows),Scalar::all(0),2,4,0);
iOff =-( matchLoc.x -iRef+blockSize/2);
jOff = -(matchLoc.y - jRef+blockSize/2);
cout << iOff << "," << jOff << endl;
int newH = imgPrevUnAlligned.rows - abs(jOff);
int newW = imgPrevUnAlligned.cols - abs(iOff);
Rect prevBounds(iOff > 0? iOff : 0, jOff < 0? 0 : jOff,newW,newH);
imgPrevUnAlligned(prevBounds).copyTo(imgPrev);
Rect nextBounds(iOff > 0? 0 : -iOff, jOff < 0? -jOff : 0,newW,newH);
imgNextUnAlligned(nextBounds).copyTo(imgNext);
/*
namedWindow("result prev",CV_WINDOW_NORMAL);
imshow("result prev",imgPrev);
cvWaitKey(0);
namedWindow("result next",CV_WINDOW_NORMAL);
imshow("result next",imgNext);
cvWaitKey(0);
return;
*/
}
int main()
{
int N = 2;
Mat imageStack[N];
Mat grayStack[N];
Mat focal_Measure[N];
Mat lap_x;
Mat lap_y;
// kernel for boosting intensity of modified laplacian
float kernel[3][3] = {{1,1,1}, {1,1,1}, {1,1,1}};
Mat boostingFilter = Mat(3, 3, CV_32FC1, kernel);
//Load the image stack and convert it to grayscale.
//TODO !
//Loaded images in unint8. Consider changing them to float during grayscale
//Conversion for arithmetic precision.
for(int i = 0; i < N; i++){
char buffer[50];
sprintf(buffer,"align/img%d.jpg",i+1);
imageStack[i] = imread(buffer);
if(imageStack[i].cols > 1000 || imageStack[i].rows > 1000){
float scale = 1.0/(max(imageStack[i].rows,imageStack[i].cols)/1000+1);
Mat resizedImg;
resize(imageStack[i],resizedImg,Size(0,0),scale,scale,CV_INTER_AREA);
imageStack[i] = resizedImg;
}
if(!imageStack[i].data){
cerr << "Could not open or find the image" << endl;
exit(0);
}
if(i>0){
Mat imgPrev;
Mat imgNext;
alignToPrevImage(imageStack[i-1],imageStack[i],imgPrev,imgNext);
imageStack[i-1] = imgPrev;
imageStack[i] = imgNext;
/*
namedWindow("img1",CV_WINDOW_NORMAL);
imshow("img1",imgPrev);
namedWindow("img2",CV_WINDOW_NORMAL);
imshow("img2",imgNext);
cvWaitKey(0);
*/
}
}
for(int i = 0; i < N; i++){
char buffer[50];
sprintf(buffer,"align/img%d.jpg",i+1);
int rows;
int cols;
rows=focal_Measure[0].rows;
cols=focal_Measure[0].cols;
//Create a new Gray scale image
Mat grayImg;// = toGray(imageStack[i] );
Mat grayImgFloat;
cvtColor(imageStack[i],grayImg,CV_BGR2GRAY,1);
grayImg.convertTo(grayImgFloat,CV_32FC1,1/255.0);
grayStack[i] = grayImgFloat;
namedWindow(buffer,WINDOW_AUTOSIZE);
imshow(buffer,grayImgFloat);
waitKey(0);
//sid
Mat gray_image;
//cvtColor( imageStack[i], gray_image, CV_BGR2GRAY );
//grayStack[i]=gray_image;
lap_x=lap_dir(grayStack[i],0);
lap_y=lap_dir(grayStack[i],1);
lap_x=abs(lap_x);
lap_y=abs(lap_y);
Mat modLaplacian;
addWeighted(lap_x,1,lap_y,1,0.0,modLaplacian);
// commented out-- Sid
// Size ksize(9,9);
// float sigma = 10.0;
// Mat modLapSmooth;
// GaussianBlur(modLaplacian,modLapSmooth,ksize,sigma);
//siddhartha - 2-May
//locally boosting all pixel intensities based on a 3X3 neighborhood
Mat boosted;
filter2D(modLaplacian, boosted, -1, boostingFilter);
//averaging values of the focal measure: average filter preferred ouver gaussian filter as gaussian does not resolve the issue of noisy patches
Size ksize(19,19);
Mat modLapSmooth;
boxFilter(boosted,modLapSmooth,-1,ksize);
focal_Measure[i]=modLapSmooth;
continue;
namedWindow(buffer,WINDOW_AUTOSIZE);
imshow(buffer,modLapSmooth);
waitKey(0);
}
for(int i = 0; false && i < N ; i++){
int u = 100;
int v = 200;
cout<< focal_Measure[i].at<float>(u,v)<<endl;
}
int rows;
int cols;
rows=focal_Measure[0].rows;
cols=focal_Measure[0].cols;
cout<<focal_Measure[0].rows<<endl;
cout<<focal_Measure[0].cols<<endl;
Mat focusMap= Mat::zeros( rows, cols, CV_8UC1);
int maxK;
double maxVal;
double tempVal;
for(int y = 0; y < rows; y++)
{
for(int x = 0; x < cols; x++)
{
//Iterate over all the images on the stack and get the one in focus.
maxK=0;
maxVal=focal_Measure[0].at<float>(y,x);
for(int k =1; k< N; k++)
{
tempVal=focal_Measure[k].at<float>(y,x);
// cout<<tempVal<<endl;
if(tempVal>maxVal)
{
maxK=k;
maxVal=tempVal;
}
}
focusMap.at<uchar>(y,x)=maxK; //TODO take out this scale factor. For visialization and debug
}
}
//commenting out- Sid ; taken care of smoothing by forming modLapsmooth
//Focus does not change rapidly among objects.
//Smooth it!
// Size ksize(9,9);
// float sigma = 12.0;
// Mat focusMapSmooth;
// GaussianBlur(focusMap,focusMapSmooth,ksize,sigma);
namedWindow("focusMap",WINDOW_AUTOSIZE);
imshow("focusMap",focusMap);
waitKey(0);
namedWindow("result",WINDOW_AUTOSIZE);
setMouseCallback("result", CallBackFunc, NULL);
imshow("result",imageStack[0]);
int previos=0;
int current=0;
int step;
int i;
//press ctrl+ mouse click to exit
while(1)
{
while (x_coord == -1 && y_coord == -1) cvWaitKey(100);
if(x_coord==0)
return 0;
//cout<<"coords found: "<<endl<<x_coord<<endl<<y_coord<<endl;
current=focusMap.at<uchar>(y_coord,x_coord);
step=current-previos;
if(step<1)
{
for(i=previos; i>=current;i--)
{
imshow("result",imageStack[i]);
cvWaitKey(2);
}
}
else
{
for(i=previos; i<=current; i++)
{
imshow("result",imageStack[i]);
cvWaitKey(2);
}
}
//cout<<endl<<step<<endl;
previos=current;
x_coord=-1;
y_coord =-1;
while (x_coord == -1 && y_coord == -1) cvWaitKey(100);
}
return 0;
}