/
stereo.c
1345 lines (1102 loc) · 45.6 KB
/
stereo.c
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
--------------------------------------------------------------------------------------------------------------------
------- STEREOSCOPY DISTANCE MEASUREMENT ---------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
Please, send me an email if you use or modify this program, just to let me know if this program is useful to anybody or how did you improve it :) You can also send me an email to tell me how lame it is ! :)
TLDR; I don't give a damn to anything you can do using this code. It would just be nice to quote where the original code comes from.
--------------------------------------------------------------------------------
"THE NO-ALCOHOL BEER-WARE LICENSE" (Revision 42):
Phyks (webmaster@phyks.me) wrote this file. As long as you retain this notice you
can do whatever you want with this stuff (and you can also do whatever you want
with this stuff without retaining it, but that's not cool...). If we meet some
day, and you think this stuff is worth it, you can buy me a beer soda
in return.
Phyks
---------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
This program computes the mean distance between a stereoscopic camera and an object.
We assume both cameras have the same specs (which are the diameter of the camera's field stop and the focal length).
The code is commented but for more details about the algorithm, please refer to the following article :
http://docs.lib.purdue.edu/cgi/viewcontent.cgi?article=1064&context=ecetr
The distance estimation is just an application of the algorithm described in the previous article. To compute the
displacement vector between the two images, we study the distances between subsets of the images. This is just like if
we had a mask that we could place over the left image and then, we could search the best matching part of the right image.
We are in a three dimensional space (RGB) so we just compute the euclidian distances between each corresponding pixels of
the subsets and then, we compute the mean distance between the two subsets.
Another option is to use FFT transform and phase correlation algorithm. More information cn be found here :
http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html and http://en.wikipedia.org/wiki/Phase_correlation
The sobel argument makes the program perform a Sobel edge detection first.
Notes :
- We use the openCV library to easily load and go through the images. OpenCV is a C library for computer vision licensed
under a BSD License (http://opensource.org/licenses/bsd-license.php)
- We use FFTW library to perform FFT. FFTW is a C library to compute FFT licensed under the GNU General Public License (GPL, see http://www.fftw.org/doc/License-and-Copyright.html)
- We compute both X and Y displacements but we'll focus on X displacements to determine the mean distance of the object
as the cameras are supposed to be on the same horizontal plane (Y displacements = vertical displacements are negligible)
- X corresponds to the abscissa and Y to the ordinate of the pixel in the image. They both goes from 0 to ...
Pixels are numeroted this way :
(0,0) (1,0) (2,0) ... (Width,0)
(1,0) (1,1) (2,1) ... (Width,1)
... ... ... ... ...
(0,Height) ... (Width, Height)
--------------------------------------------------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <opencv2/core/core_c.h>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc_c.h>
#include <math.h>
#include <getopt.h>
#include <time.h>
#include <pthread.h>
#include <fftw3.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// Structure to store the coordinates of the best matching subsets
struct minXY_struct
{
float min;
int X;
int Y;
};
struct data_find_common
{
const IplImage* img2;
const uchar* p1;
int widthStep1;
int X_1;
int Y_1;
int subset_size;
int X_start;
int Y_start;
int X_end;
int Y_end;
struct minXY_struct* temp;
};
/*
--------------------------------------------------------------------------------------------------------------------
------- Find common ------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
This function takes 10 parameters :
- The image to search in
- The pointer to the pixel in the left-hand corner of the subset we are working on (and its human-readable
coordinates X_1 and Y_1)
- The size of the subsets we consider
- The coordinates of the part of the image to search in
This function will search the best matching subset in the right image to the current subset in the left image.
It will go through the image img2 (aka the right image) and for each pixel, will compute the mean RGB distance between :
- The subset we are working on in the left image
- A subset of the same size in img2 with the current pixel as the left-hand corner
As the images have been opened in color mode, we are working in a three dimensional space (RGB) and we just compute the
euclidian distance pixel to pixel using the colors as components. Then, we compute the mean of this distance over the subset.
This function returns a structure with the X and Y coordinates of the left-hand corner of the best matching subset.
--------------------------------------------------------------------------------------------------------------------
*/
static void *find_common(void *data)
{
struct data_find_common *data_args = data;
//Variables to explore the right image
uchar *p2, *line2;
//And the subsets
int i = 0, j = 0, k = 0;
//To store the distance between the two subsets (distance) and the distances pixel to pixel (distance_temp)
float distance = 0, distance_temp = 0;
//Human readables coordinates corresponding to p2
int X_temp = data_args->X_start, Y_temp = data_args->Y_start;
//The structure it will return. We start with a maximal distance in the left-hand corner of the image
struct minXY_struct minXY={255, 0, 0};
//We explore the image line by line from left to right
//Note : we only explore an area with a height equal to the quarter of th total height of the image because the vertical displacement is supposed to be close to 0
for (line2 = (uchar*) data_args->img2->imageData + data_args->Y_start*data_args->img2->widthStep;
line2 <= (uchar*) data_args->img2->imageData + data_args->Y_end*data_args->img2->widthStep;
line2 += data_args->img2->widthStep)
{
for (p2 = line2 + data_args->X_start*data_args->img2->nChannels; p2 <= line2 + data_args->X_end*data_args->img2->nChannels; p2 += data_args->img2->nChannels)
{
distance = 0;
//For each pixels, we calculate the mean distance for the subset_size pixel box around it
for(i = 0; i<data_args->subset_size; i++) //i = iterate through the pixels in a line
{
for(j = 0; j<data_args->subset_size; j++) //j = iterate through the lines
{
distance_temp = 0;
for(k = 0; k < data_args->img2->nChannels; k++) //k = iterate through the channels
{
distance_temp += pow(*(p2 + i*data_args->img2->nChannels + k + j*data_args->img2->widthStep) - *(data_args->p1 + i*data_args->img2->nChannels + k + j*data_args->widthStep1),2); //Works because img1 and img2 have the same number of channels
}
distance += sqrt(distance_temp);
}
}
distance /= pow(data_args->subset_size,2); //Distance is the mean distance over the subset
//And if it is better than the previous min, we store the new coordinates
//We prefer a smaller distance
// But at equal distance, we prefer the closer pixels and the minimal vertical displacement (as it is supposed to be 0 because cameras are on a same horizontal plane)
if(distance < minXY.min || (distance == minXY.min && (sqrt(pow(X_temp-data_args->X_1, 2) + pow(Y_temp-data_args->Y_1, 2)) < sqrt(pow(minXY.X-data_args->X_1, 2) + pow(minXY.Y-data_args->Y_1, 2)) || abs(Y_temp - data_args->Y_1) < abs(minXY.Y - data_args->Y_1))))
{
minXY.X = X_temp;
minXY.Y = Y_temp;
minXY.min = distance;
}
X_temp++;
}
X_temp = data_args->X_start;
Y_temp++;
}
//Store the informations (coordinates and minimum) in a structure
*data_args->temp = minXY;
return NULL;
}
/*
--------------------------------------------------------------------------------------------------------------------
------- Compute mean distance --------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
This function takes 6 parameters :
- The abscissas of the pixels in the left and in the right image
- The width of the two images
- The distance between the cameras and the angle theta computed previously
We compute the mean distance of the given object by the algorithm described in the article above and return it or
return -1 if there was an error.
--------------------------------------------------------------------------------------------------------------------
*/
float compute_mean_distance(int X_1, int X_2, int widthL, int widthR, float theta, float deltaX)
{
float alpha_1, alpha_2;
//If the object is located between two cameras
if(X_1 > widthL / 2 && X_2 < widthR / 2)
{
alpha_1 = atan((X_1 - (widthL / 2))*tan(theta)/(widthL / 2));
alpha_2 = atan(((widthR / 2) - X_2)*tan(theta)/(widthR / 2));
return tan(M_PI/2 - alpha_1)*tan(M_PI/2 - alpha_2)*deltaX / (tan(M_PI/2 - alpha_1) + tan(M_PI/2 - alpha_2));
}
//If the object is on the left side of both cameras
if(X_1 < widthL / 2 && X_2 < widthR / 2)
{
alpha_1 = atan(((widthL / 2) - X_1)*tan(theta)/(widthL / 2));
alpha_2 = atan(((widthR / 2) - X_2)*tan(theta)/(widthR / 2));
return sin(M_PI/2 - alpha_1)*sin(M_PI/2 - alpha_2)*deltaX / sin(alpha_2 - alpha_1);
}
//If the object is on the right side of both cameras
if(X_1 > widthL / 2 && X_2 > widthR / 2)
{
alpha_1 = atan((X_1 - (widthL / 2))*tan(theta)/(widthL / 2));
alpha_2 = atan((X_2 - (widthR / 2))*tan(theta)/(widthR / 2));
return sin(M_PI/2 - alpha_1)*sin(M_PI/2 - alpha_2)*deltaX / sin(alpha_1 - alpha_2);
}
//If the object is exactly in front of the left camera
if(X_1 == widthL / 2)
{
alpha_2 = atan(((widthR / 2) - X_2)*tan(theta)/(widthR / 2));
return tan(M_PI/2 - alpha_2)*deltaX;
}
//If the object is exactly in front of the right camera
if(X_2 == widthR / 2)
{
alpha_1 = atan((X_1 - (widthL / 2))*tan(theta)/(widthL / 2));
return tan(M_PI/2 - alpha_1)*deltaX;
}
return -1;
}
/*
--------------------------------------------------------------------------------------------------------------------
------- Print help message -----------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
*/
void print_help(FILE* stream)
{
fprintf(stream, "\nThis program computes the mean distance between a stereoscopic camera and an object. We assume both cameras have the same specs (which are the diameter of the camera's field stop and the focal length).\n");
fprintf(stream, "\nFor more details about the algorithm, please refer to the following article : http://docs.lib.purdue.edu/cgi/viewcontent.cgi?article=1064&context=ecetr\n");
fprintf(stream, "\nThe distance estimation is just an application of the algorithm described in the previous article. To compute the displacement vector between the two images, we study the distances between subsets of the images. This is just like ifwe had a mask that we could place over the left image and then, we could search the best matching part of the right image.\n");
fprintf(stream, "\nNotes : \n");
fprintf(stream, "- We use the openCV library to easily load and go through the images. OpenCV is a C library for computer vision licensed under a BSD License (http://opensource.org/licenses/bsd-license.php)\n");
fprintf(stream, "- We compute both X and Y displacements but we'll focus on X displacements to determine the mean distance of the object as the cameras are supposed to be on the same horizontal plane (Y displacements = vertical displacements are negligible)\n");
fprintf(stream, "- X corresponds to the abscissa and Y to the ordinate of the pixel in the image. They both goes from 0 to ...\n");
fprintf(stream, "\nPixels are numeroted this way :\n");
fprintf(stream, "(0,0)\t(1,0)\t(2,0)\t...\t(Width,0)\n");
fprintf(stream, "(1,0)\t(1,1)\t(2,1)\t...\t(Width,1)\n");
fprintf(stream, " ...\t ...\t ...\t...\t ...\n");
fprintf(stream, "(0,Height)\t ...\t\t(Width, Height)\n");
fprintf(stream, "\n======================================\n");
fprintf(stream, "\n Usage : \n");
fprintf(stream, "\t -l (--left) \t Path to the left image\n");
fprintf(stream, "\t -r (--right) \t Path to the right image\n");
fprintf(stream, "\t -D (--diameter) (float) Diameter of the camera's field stop\n");
fprintf(stream, "\t -f (--focal) \t (float) Focal length of the camera\n");
fprintf(stream, "\t -d (--delta) \t (float) Distance between the two cameras\n");
fprintf(stream, "\t -s (--size) \t [Optional] (int) Size of the subsets used, default is 50\n");
fprintf(stream, "\t -x (--abscissa) [Optional] (int) Abscissa of the pixel to compute the mean distance\n");
fprintf(stream, "\t -y (--ordinate) [Optional] (int) Ordinate of the pixel to compute the mean distance\n");
fprintf(stream, "\t -c (--choose) \t [Optional] To display the image and select which pixel you want to work with (same as -x and -y but useful if you don't know the coordinates of the pixel)\n");
fprintf(stream, "\t -o (--output) \t [Optional] Path to a file to store the output\n");
fprintf(stream, "\t -S (--sobel) \t [Optional] Run a Sobel edges detection algorithm on both left and right images first to work only on edges. This option won't have any effect if working in single pixel mode.\n");
fprintf(stream, "\t -F (--fft) \t [Optional] Use cross-correlation and fft based method (faster but a little bit less accurate).\n");
fprintf(stream, "\t -H (--hamming) \t [Optional] Use an hamming window to improve FFT (but slightly slower).\n");
fprintf(stream, "\t -h (--help) \t Display this error message\n");
}
/*
--------------------------------------------------------------------------------------------------------------------
------- Min -----------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
Return the min of a and b
*/
int minimum(int a, int b)
{
if(a < b)
return a;
else
return b;
}
/*
--------------------------------------------------------------------------------------------------------------------
------- Max --------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
Return the max of a and b
*/
int maximum(int a, int b)
{
if(a > b)
return a;
else
return b;
}
/*
--------------------------------------------------------------------------------------------------------------------
------- Mouse Handler ----------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
Handle the left button click and get the coordinates of the current pixel
*/
void mouseHandler(int event, int x, int y, int flags, void* param)
{
if(event == CV_EVENT_LBUTTONDOWN) //If left click
{
int** input = param;
*input[0] = x; //Get the current coordinates
*input[1] = y;
cvDestroyAllWindows(); //And close the window
}
}
/*
--------------------------------------------------------------------------------------------------------------------
------- Find_FFT ---------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
Find a template (tpl) in an image (src)
Note : template and src must be the same size
*/
struct Peak
{
CvPoint pt;
double maxval;
};
/*
Old version using OpenCV
---------------------------------
struct Peak Find_FFT(IplImage* src, IplImage* tpl)
{
CvSize imgSize = cvSize(src->width, src->height); //Size of the src image
CvMat tmp;
//src and tpl must be the same size
assert(src->width == tpl->width);
assert(src->height == tpl->height);
// Allocate floating point frames used for DFT (real, imaginary and complex)
IplImage* realInput = cvCreateImage( imgSize, IPL_DEPTH_64F, 1 );
IplImage* imaginaryInput = cvCreateImage( imgSize, IPL_DEPTH_64F, 1 );
IplImage* complexInput = cvCreateImage( imgSize, IPL_DEPTH_64F, 2 );
//Find best size for DFT
int nDFTHeight= cvGetOptimalDFTSize( imgSize.height );
int nDFTWidth= cvGetOptimalDFTSize( imgSize.width );
CvSize dftSize = cvSize(nDFTWidth, nDFTHeight);
//Images that will store DFT
CvMat* src_DFT = cvCreateMat( nDFTHeight, nDFTWidth, CV_64FC2 );
CvMat* tpl_DFT = cvCreateMat( nDFTHeight, nDFTWidth, CV_64FC2 );
//Images used to compute modulus
IplImage* imageRe = cvCreateImage( dftSize, IPL_DEPTH_64F, 1 );
IplImage* imageIm = cvCreateImage( dftSize, IPL_DEPTH_64F, 1 );
IplImage* imageImMag = cvCreateImage( dftSize, IPL_DEPTH_64F, 1 );
IplImage* imageMag = cvCreateImage( dftSize, IPL_DEPTH_64F, 1 );
// Processing of src
cvScale(src,realInput,1.0,0); //Convert it to CV_32F (float)
cvZero(imaginaryInput);
cvMerge(realInput,imaginaryInput,NULL,NULL,complexInput);
cvGetSubRect(src_DFT,&tmp,cvRect(0,0,src->width,src->height));
cvCopy(complexInput,&tmp,NULL);
if (src_DFT->cols>src->width)
{
cvGetSubRect(src_DFT,&tmp,cvRect(src->width,0,src_DFT->cols-src->width,src->height));
cvZero(&tmp);
}
cvDFT(src_DFT,src_DFT,CV_DXT_FORWARD,complexInput->height); //Process DFT
// Processing of tpl
cvScale(tpl,realInput,1.0,0);
cvMerge(realInput,imaginaryInput,NULL,NULL,complexInput);
cvGetSubRect(tpl_DFT,&tmp,cvRect(0,0,tpl->width,tpl->height));
cvCopy(complexInput,&tmp,NULL);
if (tpl_DFT->cols>tpl->width)
{
cvGetSubRect(tpl_DFT,&tmp,cvRect(tpl->width,0,tpl_DFT->cols-tpl->width,tpl->height));
cvZero( &tmp );
}
cvDFT(tpl_DFT,tpl_DFT,CV_DXT_FORWARD,complexInput->height);
// Multiply spectrums of the scene and the model (use CV_DXT_MUL_CONJ to get correlation instead of convolution)
cvMulSpectrums(src_DFT,tpl_DFT,src_DFT,CV_DXT_MUL_CONJ);
// Split Fourier in real and imaginary parts
cvSplit(src_DFT,imageRe,imageIm,0,0);
// Compute the magnitude of the spectrum components: Mag = sqrt(Re^2 + Im^2)
cvPow( imageRe, imageMag, 2.0 );
cvPow( imageIm, imageImMag, 2.0 );
cvAdd( imageMag, imageImMag, imageMag, NULL );
cvPow( imageMag, imageMag, 0.5 );
// Normalize correlation (Divide real and imaginary components by magnitude)
cvDiv(imageRe,imageMag,imageRe,1.0);
cvDiv(imageIm,imageMag,imageIm,1.0);
cvMerge(imageRe,imageIm,NULL,NULL,src_DFT);
//Inverse dft
cvDFT( src_DFT, src_DFT, CV_DXT_INVERSE_SCALE, complexInput->height );
cvSplit( src_DFT, imageRe, imageIm, 0, 0 );
//Find the peak (greatest magnitude)
double minval = 0.0;
double maxval = 0.0;
CvPoint minloc;
CvPoint maxloc;
cvMinMaxLoc(imageRe,&minval,&maxval,&minloc,&maxloc,NULL);
int x=maxloc.x; // log range
//if (x>(imageRe->width/2))
// x = x-imageRe->width; // positive or negative values
int y=maxloc.y; // angle
//if (y>(imageRe->height/2))
// y = y-imageRe->height; // positive or negative values
struct Peak pk;
pk.maxval= maxval;
pk.pt=cvPoint(x,y);
cvReleaseImage(&realInput);
cvReleaseImage(&imaginaryInput);
cvReleaseImage(&complexInput);
cvReleaseImage(&imageRe);
cvReleaseImage(&imageIm);
cvReleaseImage(&imageImMag);
cvReleaseImage(&imageMag);
cvReleaseMat(&src_DFT);
cvReleaseMat(&tpl_DFT);
return pk;
}
-----------------------------------------*/
/* New version, using FFTW (multithreaded)
------------------------------------------
*/
struct Peak Find_FFT(IplImage* src, IplImage* tpl, int hamming)
{
int i, j, k = 0;
double tmp; //To store the modulus temporarily
//src and tpl must be the same size
assert(src->width == tpl->width);
assert(src->height == tpl->height);
// Get image properties
int width = src->width;
int height = src->height;
int step = src->widthStep;
int fft_size = width * height;
fftw_init_threads(); //Initialize FFTW for multithreading with a max number of 2 threads (more is not efficient)
fftw_plan_with_nthreads(2);
//Allocate arrays for FFT of src and tpl
fftw_complex *src_spatial = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
fftw_complex *src_freq = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
fftw_complex *tpl_spatial = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
fftw_complex *tpl_freq = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
fftw_complex *res_spatial = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height ); //Result = Cross correlation
fftw_complex *res_freq = ( fftw_complex* )fftw_malloc( sizeof( fftw_complex ) * width * height );
// Setup pointers to images
uchar *src_data = (uchar*) src->imageData;
uchar *tpl_data = (uchar*) tpl->imageData;
// Fill the structure that will be used by fftw
for(i = 0; i < height; i++)
{
for(j = 0 ; j < width ; j++, k++)
{
src_spatial[k][0] = (double) src_data[i * step + j];
src_spatial[k][1] = 0.0;
tpl_spatial[k][0] = (double) tpl_data[i * step + j];
tpl_spatial[k][1] = 0.0;
}
}
// Hamming window to improve FFT (but slightly slower to compute)
if(hamming == 1)
{
double omega = 2.0*M_PI/(fft_size-1);
double A= 0.54;
double B= 0.46;
for(i=0,k=0;i<height;i++)
{
for(j=0;j<width;j++,k++)
{
src_spatial[k][0]= (src_spatial[k][0])*(A-B*cos(omega*k));
tpl_spatial[k][0]= (tpl_spatial[k][0])*(A-B*cos(omega*k));
}
}
}
// Setup FFTW plans
fftw_plan plan_src = fftw_plan_dft_2d(height, width, src_spatial, src_freq, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_plan plan_tpl = fftw_plan_dft_2d(height, width, tpl_spatial, tpl_freq, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_plan plan_res = fftw_plan_dft_2d(height, width, res_freq, res_spatial, FFTW_BACKWARD, FFTW_ESTIMATE);
// Execute the FFT of the images
fftw_execute(plan_src);
fftw_execute(plan_tpl);
// Compute the cross-correlation
for(i = 0; i < fft_size ; i++ )
{
res_freq[i][0] = tpl_freq[i][0] * src_freq[i][0] + tpl_freq[i][1] * src_freq[i][1];
res_freq[i][1] = tpl_freq[i][0] * src_freq[i][1] - tpl_freq[i][1] * src_freq[i][0];
tmp = sqrt(pow(res_freq[i][0], 2.0) + pow(res_freq[i][1], 2.0));
res_freq[i][0] /= tmp;
res_freq[i][1] /= tmp;
}
// Get the phase correlation array = compute inverse fft
fftw_execute(plan_res);
// Find the peak
struct Peak pk;
IplImage* peak_find = cvCreateImage(cvSize(tpl->width,tpl->height ), IPL_DEPTH_64F, 1);
double *peak_find_data = (double*) peak_find->imageData;
for( i = 0 ; i < fft_size ; i++ )
{
peak_find_data[i] = res_spatial[i][0] / (double) fft_size;
}
CvPoint minloc, maxloc;
double minval, maxval;
cvMinMaxLoc(peak_find, &minval, &maxval, &minloc, &maxloc, 0);
pk.pt = maxloc;
pk.maxval = maxval;
// Clear memory
fftw_destroy_plan(plan_src);
fftw_destroy_plan(plan_tpl);
fftw_destroy_plan(plan_res);
fftw_free(src_spatial);
fftw_free(tpl_spatial);
fftw_free(src_freq);
fftw_free(tpl_freq);
fftw_free(res_spatial);
fftw_free(res_freq);
cvReleaseImage(&peak_find);
fftw_cleanup_threads(); //Cleanup everything else related to FFTW
return pk;
}
/*
--------------------------------------------------------------------------------------------------------------------
------- Main function ----------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
*/
int main (int argc, char* argv[])
{
//---------------------------
//Initialization of variables
//---------------------------
//To handle the options
const char* const short_options = "hl:r:D:f:d:s:x:y:o:cSFH";
const struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "left", 1, NULL, 'l' },
{ "right", 1, NULL, 'r' },
{ "diameter", 1, NULL, 'D' },
{ "focal", 1, NULL, 'f' },
{ "delta", 1, NULL, 'd' },
{ "size", 1, NULL, 's' },
{ "abscissa", 1, NULL, 'x' },
{ "ordinate", 1, NULL, 'y' },
{ "output", 1, NULL, 'o' },
{ "choose", 0, NULL, 'c' },
{ "sobel", 0, NULL, 'S' },
{"fft", 0, NULL, 'F'},
{"hamming", 0, NULL, 'H'},
{ NULL, 0, NULL, 0 }
};
int next_option = 0;
//Threads
pthread_t thread1, thread2, thread3, thread4;
struct data_find_common data1, data2, data3, data4;
//To store the images
IplImage* imgL = NULL;
IplImage* imgR = NULL;
uchar *p1, *line1;
//Max X and Y for the images (to avoid computing it many times)
int X_maxL, Y_maxL, X_maxR, Y_maxR;
//Path to images
const char* src_pathL = NULL; //Const ?
const char* src_pathR = NULL;
//Matrices
int array_size = 0; //Size of the matrices we'll create
int **displacements; //To store the displacements for every subset
float *mean_distance;
//To store the parameters
float D = 0, f = 0, deltaX = 0;
int X_input = -1, Y_input = -1, c = 0, sobel = 0, fft = 0, hamming = 0;
int CVLOAD = CV_LOAD_IMAGE_COLOR;
FILE *output_file = stdout; //By default, the output will be stored in stdout
const char* output = NULL;
//The size of the subset, 50 pixels by default.
int subset_size = 50;
//theta = half angle of view of the cameras, alphas cf. article
float theta;
//To iterate and compute the mean distances
int X_1, Y_1, X_2;
int j=0, key, char_temp;
float min;
CvMat tmp, tmp2;
//Temporary structures to store what find_common returns
struct minXY_struct temp1={255, 0, 0};
struct minXY_struct temp2={255, 0, 0};
struct minXY_struct temp3={255, 0, 0};
struct minXY_struct temp4={255, 0, 0};
//Images for sobel
IplImage* imgL_gray = NULL;
IplImage* imgL_Sobelx = NULL;
IplImage* imgL_Sobely = NULL;
IplImage* imgL_Sobel = NULL;
IplImage* imgR_gray = NULL;
IplImage* imgR_Sobelx = NULL;
IplImage* imgR_Sobely = NULL;
IplImage* imgR_Sobel = NULL;
//Image for FFT
IplImage* template = NULL;
//Time
clock_t start=clock();
//----------------------------------------------------------
//Store the options and the parameters in corresponding vars
//----------------------------------------------------------
do {
next_option = getopt_long(argc, argv, short_options, long_options, NULL);
switch(next_option)
{
case 'h':
print_help(stdout);
return EXIT_SUCCESS;
case 'l':
src_pathL = optarg;
break;
case 'r':
src_pathR = optarg;
break;
case 'D':
D = atof(optarg);
break;
case 'f':
f = atof(optarg);
break;
case 'd':
deltaX = atof(optarg);
break;
case 's':
subset_size = atoi(optarg);
break;
case 'x':
X_input = atoi(optarg);
break;
case 'y':
Y_input = atoi(optarg);
break;
case 'o':
output = optarg;
break;
case 'c':
c = 1;
break;
case 'S':
sobel = 1;
break;
case 'F':
fft = 1;
CVLOAD = CV_LOAD_IMAGE_GRAYSCALE;
break;
case 'H':
hamming = 1;
break;
case -1: // End of arguments list
break;
default: // Unexpected behavior
return EXIT_FAILURE;
}
} while(next_option != -1);
//If not enough arguments -> error + help message
if(src_pathL == NULL || src_pathR == NULL || D == 0 || f == 0 || deltaX == 0)
{
fprintf(stderr, "Not enough arguments. You must provide the paths to the two images, the diameter, the focal length and the distance between the two cameras. Please refer to the help message below for more details.\n");
fprintf(stderr, "======================================\n");
fprintf(stderr, "Help :\n");
print_help(stderr);
return EXIT_FAILURE;
}
//If the output must be written in a file, open it
if(output != NULL)
{
output_file = fopen(output, "w");
}
//------------------------------------------------
//Load the images and define the needed parameters
//------------------------------------------------
//We load the images in color mode even if they are greyscale to avoid problems with images with different color modes for the two images
if (!(imgL = cvLoadImage (src_pathL, CVLOAD)))
{
fprintf (stderr, "couldn't open image file: %s\n",src_pathL);
return EXIT_FAILURE;
}
if (!(imgR = cvLoadImage (src_pathR, CVLOAD)))
{
fprintf (stderr, "couldn't open image file: %s\n", src_pathR);
return EXIT_FAILURE;
}
//Check that the images have 3 channels and 8 bits depth (to store values in char)
if(CVLOAD == CV_LOAD_IMAGE_COLOR)
{
assert (imgL->depth == IPL_DEPTH_8U && imgL->nChannels == 3);
assert (imgR->depth == IPL_DEPTH_8U && imgR->nChannels == 3);
}
else
{
assert (imgL->depth == IPL_DEPTH_8U && imgL->nChannels == 1);
assert (imgR->depth == IPL_DEPTH_8U && imgR->nChannels == 1);
}
fprintf(output_file, "Now working with %d*%d subset. \n", subset_size, subset_size);
theta = atan(D/(2*f));
//Last coordinates we can study (due to the size of the subset)
X_maxL = imgL->width - subset_size;
Y_maxL = imgL->imageSize/imgL->widthStep - subset_size;
X_maxR = imgR->width - subset_size;
Y_maxR = imgR->imageSize/imgR->widthStep - subset_size;
//-------------------------------------------------------------------------------------------
//Test that there's something (interesting) to do, ie that subset_size < imgSize / 2 and same for Y
//-------------------------------------------------------------------------------------------
if(floor(imgL->width/2) < subset_size || floor((imgL->imageSize / imgL->widthStep) / 2) < subset_size)
{
fprintf(stderr, "Error : subset is greater than the half of the image. Please choose a smaller subset size.\n");
cvReleaseImage(&imgL);
cvReleaseImage(&imgR);
return EXIT_FAILURE;
}
//---------------------------------------------------------
//Get the coordinates of the pixels if argument "-c" passed
//---------------------------------------------------------
while(c != 0)
{
int *input[2] = {&X_input, &Y_input};
cvNamedWindow("Select the pixel to work with", 1); //Create a window, display the image and add the mouse handler
cvSetMouseCallback("Select the pixel to work with", mouseHandler, (void*) &input);
cvShowImage("Select the pixel to work with", imgL);
cvWaitKey(0);
printf("Now working with the pixel : (%d,%d).\n", X_input, Y_input);
printf("Is it ok ? [y/N] \n");
do
{
key = fgetc(stdin);
while (char_temp != '\n' && char_temp != EOF)
{
char_temp = getchar();
}
}while(key != 89 && key != 121 && key != 110 && key != 78 && key != 10);
if(key == 89 || key == 121 || key == 10)
{
c = 0;
}
}
//-----------------------------------------------
//Applicate an edge detection algorithm if needed
//-----------------------------------------------
if(sobel != 0 && X_input < 0 && Y_input < 0)
{
//First, apply a gaussian blur
cvSmooth(imgL, imgL, CV_GAUSSIAN, 3, 3, 0, 0);
cvSmooth(imgR, imgR, CV_GAUSSIAN, 3, 3, 0, 0);
//Convert images to greyscale
imgL_gray = cvCreateImage(cvGetSize(imgL),imgL->depth,1);
imgL_Sobelx = cvCreateImage(cvGetSize(imgL_gray),imgL_gray->depth,1);
imgL_Sobely = cvCreateImage(cvGetSize(imgL_gray),imgL_gray->depth,1);
imgL_Sobel = cvCreateImage(cvGetSize(imgL_gray),imgL_gray->depth,1);
imgR_gray = cvCreateImage(cvGetSize(imgL),imgL->depth,1);
imgR_Sobelx = cvCreateImage(cvGetSize(imgL_gray),imgL_gray->depth,1);
imgR_Sobely = cvCreateImage(cvGetSize(imgL_gray),imgL_gray->depth,1);
imgR_Sobel = cvCreateImage(cvGetSize(imgL_gray),imgL_gray->depth,1);
cvCvtColor(imgL, imgL_gray, CV_RGB2GRAY);
cvCvtColor(imgR, imgR_gray, CV_RGB2GRAY);
//Gradient X
cvSobel(imgL_gray, imgL_Sobelx, 1, 0, 3);
cvSobel(imgR_gray, imgR_Sobelx, 1, 0, 3);
//Gradient Y
cvSobel(imgL_gray, imgL_Sobely, 0, 1, 3);
cvSobel(imgR_gray, imgR_Sobely, 0, 1, 3);
//Add the two images
cvAdd(imgL_Sobelx, imgL_Sobely, imgL_Sobel, NULL);
cvAdd(imgR_Sobelx, imgR_Sobely, imgR_Sobel, NULL);
//Show the images
printf("The images after edge detection will be shown in a new window.\n");
printf("Press ENTER to continue");
while(getchar() != '\n'){}
cvStartWindowThread();
cvNamedWindow("Edge detection of the left image", CV_WINDOW_AUTOSIZE );
cvShowImage("Edge detection of the left image", imgL_Sobel);
cvWaitKey(0);
cvDestroyWindow("Edge detection of the left image");
cvNamedWindow("Edge detection of the right image", CV_WINDOW_AUTOSIZE );
cvShowImage("Edge detection of the right image", imgR_Sobel);
cvWaitKey(0);
cvDestroyWindow("Edge detection of the right image");
cvReleaseImage(&imgL_Sobelx);
cvReleaseImage(&imgL_Sobely);
cvReleaseImage(&imgR_Sobelx);
cvReleaseImage(&imgR_Sobely);
assert (imgL_Sobel->depth == IPL_DEPTH_8U && imgL_Sobel->nChannels == 1);
assert (imgR_Sobel->depth == IPL_DEPTH_8U && imgR_Sobel->nChannels == 1);
}
//-------------------------------------------------------------------------------------------
//Definition of 2 vectors X and Y to store the coordinates of the displacement between images
//-------------------------------------------------------------------------------------------
if(X_input >= 0 && Y_input >= 0)
{
if(X_input <= X_maxL && Y_input <= Y_maxL)
{
fprintf(output_file, "Working with the %dx%d pixel\n", X_input, Y_input);
array_size = 1; //If we enter a specific pixel, matrices become float
}
else //If x or y is too large, inform the user
{
fprintf(stderr, "Error : x or y is out of the image. Please note that x and y must be such as x<%d and y <%d\n", X_maxL, Y_maxL);
cvReleaseImage(&imgL);
cvReleaseImage(&imgR);
return EXIT_FAILURE;
}
}
else
{
array_size = floor(imgL->imageSize / imgL->widthStep / subset_size) * floor(imgL->width / subset_size);
}
//Allocate memory for all the matrices (and initialize them)
if((displacements = malloc(sizeof(*displacements) * 2)) == NULL)
{
perror("malloc:");
cvReleaseImage(&imgL);
cvReleaseImage(&imgR);
if(sobel != 0)
{
cvReleaseImage(&imgL_Sobel);
cvReleaseImage(&imgR_Sobel);
}
return EXIT_FAILURE;
}
if((displacements[0] = malloc(sizeof(**displacements) * array_size)) == NULL)
{
perror("malloc:");
cvReleaseImage(&imgL);
cvReleaseImage(&imgR);
if(sobel != 0)
{
cvReleaseImage(&imgL_Sobel);
cvReleaseImage(&imgR_Sobel);
}
return EXIT_FAILURE;
}
if((displacements[1] = malloc(sizeof(**displacements) * array_size)) == NULL)
{
perror("malloc:");
cvReleaseImage(&imgL);
cvReleaseImage(&imgR);
if(sobel != 0)
{
cvReleaseImage(&imgL_Sobel);
cvReleaseImage(&imgR_Sobel);
}
return EXIT_FAILURE;
}
if((mean_distance = malloc(sizeof(*mean_distance) * array_size)) == NULL)
{
perror("malloc:");
cvReleaseImage(&imgL);
cvReleaseImage(&imgR);
free(displacements);
if(sobel != 0)
{
cvReleaseImage(&imgL_Sobel);
cvReleaseImage(&imgR_Sobel);