Update to 2.0.0 tree from current Fremantle build
[opencv] / tests / cv / src / astereocorrespondencegc.cpp
1 #include "cvtest.h"
2
3 #if 0
4
5 #define debug //enables showing images.
6 #define CONSOLEOUTPUT //enables printing rms error and percentage of bad pixels to console.
7
8 //#define LOAD //enables skipping computing disparities and load them from images.
9 //#define SAVEIMAGES //enables saving computed disparity and red-marked disparity images.
10
11 void MarkPixel(const IplImage* markedDisparity, const int h, const int w)
12 {
13         uchar* data = (uchar*)&markedDisparity->imageData[h*markedDisparity->widthStep + w*3];
14         data[0] = 0;
15         data[1] = 0;
16         data[2] = 255;
17 }
18
19 int CalculateErrors(const IplImage* disparity,const IplImage* groundTruth, IplImage* markedDisparity,
20                                           double &rms_error, double &percentage_of_bad_pixels, 
21                                           const int maxDisparity CV_DEFAULT(16), const int eval_ignore_border CV_DEFAULT(10))
22 {
23         if (disparity->width != groundTruth->width)
24                 return CvTS::FAIL_INVALID_TEST_DATA;
25         if (disparity->height != groundTruth->height)
26                 return CvTS::FAIL_INVALID_TEST_DATA;
27
28         const double eval_bad_thresh = 1.0;
29
30         char* DC = disparity->imageData;
31         char* DT = groundTruth->imageData;
32
33         double currSum = 0;
34         unsigned int bad_pixels_counter=0;
35         
36         double diff=0;
37
38         int w = disparity->width;
39         int h = disparity->height;
40         unsigned int numPixels = w*h;
41         
42         for(int i=eval_ignore_border; i<h-eval_ignore_border; i++)
43                 for(int j=eval_ignore_border; j<w-eval_ignore_border; j++)
44                 {
45                         diff = (double)abs(DC[i*disparity->widthStep+j] - DT[i*groundTruth->widthStep+j])/(double)maxDisparity;
46                         currSum += diff*diff;
47
48                         if ( diff > eval_bad_thresh )
49                         {
50                                 bad_pixels_counter++;           
51                                 MarkPixel(markedDisparity, i, j);
52                         }
53                 }
54
55         currSum /=(double)numPixels;
56         rms_error = sqrt(currSum);
57
58         percentage_of_bad_pixels = (double)bad_pixels_counter/(double)numPixels * 100;
59
60         return 0;
61 }
62
63 class CV_StereoCorrespondenceTestGC : public CvTest
64 {
65 public:
66     CV_StereoCorrespondenceTestGC();
67 protected:
68     void run(int);
69 };
70
71         
72 CV_StereoCorrespondenceTestGC::CV_StereoCorrespondenceTestGC():
73 CvTest( "stereo-gc", "cvFindStereoCorrespondenceGC" )
74 {
75     support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE;
76 }
77
78 /* ///////////////////// stereo_correspondece_test ///////////////////////// */
79 void CV_StereoCorrespondenceTestGC::run( int )
80 {
81         int code = CvTS::OK;
82
83         const double rms_error_thresh = 1000.0; 
84         const double percentage_of_bad_pixels_thresh = 90.0; 
85
86         double rms_error[2];
87         double percentage_of_bad_pixels[2];
88
89         /* test parameters */
90     char   filepath[1000];
91     char   filename[1000];
92         //char   extension[5];
93
94         IplImage* left ;
95         IplImage* right;
96         IplImage* disparity_left;
97         IplImage* disparity_right;
98         IplImage* groundTruthLeft;
99         IplImage* groundTruthRight;
100
101     sprintf( filepath, "%sstereocorrespondence/", ts->get_data_path() );
102     sprintf( filename, "%sstereocorrespondence_list.txt", filepath );
103
104         FILE* f = fopen(filename,"r");
105         int numImages=0;
106         fscanf(f,"%d\n",&numImages);
107
108         for(int i=0; i<numImages; i++)
109         {
110                 /*Load left and right image from the storage*/
111                 char dataName[100];
112                 int maxDisparity=0;
113                 
114                 fscanf(f,"%s %d\n",dataName,&maxDisparity);
115                 sprintf(filename,"%s%sL.png",filepath,dataName);
116                 left =  cvLoadImage(filename,0);
117                 sprintf(filename,"%s%sR.png",filepath,dataName);
118                 right = cvLoadImage(filename,0);
119
120                 if (!left || !right)
121                 {
122                         ts->printf( CvTS::LOG, "Left or right image doesn't exist" );
123                         code = CvTS::FAIL_MISSING_TEST_DATA;
124                         goto _exit_;
125                 }               
126                 if ((cvGetSize(left).height != cvGetSize(right).height) 
127                         || ((cvGetSize(left).width != cvGetSize(right).width)))
128                 {
129                         ts->printf( CvTS::LOG, "Left and right image sizes aren't equal" );
130                         code = CvTS::FAIL_MISSING_TEST_DATA;
131                         goto _exit_;
132                 }
133
134                 sprintf(filename,"%s%s_gtL.png",filepath,dataName);
135                 groundTruthLeft = cvLoadImage(filename,0);              
136                 sprintf(filename,"%s%s_gtR.png",filepath,dataName);
137                 groundTruthRight = cvLoadImage(filename,0);
138                 
139                 if (!groundTruthLeft && !groundTruthRight)
140                 {
141                         ts->printf( CvTS::LOG, "Left and right ground truth images don't exist" );
142                         code = CvTS::FAIL_MISSING_TEST_DATA;
143                         goto _exit_;
144                 }       
145
146                 for(int i=0; i<2; i++)
147                 {
148                         IplImage*& groundTruth = (i == 0) ? groundTruthLeft : groundTruthRight;
149                         if (groundTruth)
150                                 if (groundTruth->nChannels != 1)
151                                 {
152                                         IplImage* tmp = groundTruth;
153                                         groundTruth = cvCreateImage(cvGetSize(left),IPL_DEPTH_8U,1);
154                                         cvCvtColor(tmp, groundTruth,CV_BGR2GRAY);
155                                 }
156                 }
157
158                 /*Find disparity map for current image pair*/
159 #ifndef LOAD
160                 disparity_left = cvCreateImage( cvGetSize(left), IPL_DEPTH_32S, 1 );
161                 disparity_right = cvCreateImage( cvGetSize(left), IPL_DEPTH_32S, 1 );
162                 
163                 CvStereoGCState* state = cvCreateStereoGCState(maxDisparity, 2);
164                 cvFindStereoCorrespondenceGC( left, right,
165                                   disparity_left, disparity_right, state);
166
167                 double scale = 256/maxDisparity ;
168                 if (!strcmp(dataName,"sawtooth") || !strcmp(dataName,"map") || !strcmp(dataName,"poster")
169                 || !strcmp(dataName,"bull") || !strcmp(dataName,"barn1") || !strcmp(dataName,"barn2"))
170                         scale = 8.0;
171                 
172                 IplImage* temp;
173                 temp = disparity_left;
174                 disparity_left = cvCreateImage(cvGetSize(temp), IPL_DEPTH_8U,1);
175                 cvConvertScale(temp, disparity_left, -scale);
176                 temp = disparity_right;
177                 disparity_right = cvCreateImage(cvGetSize(temp), IPL_DEPTH_8U,1);
178                 cvConvertScale(temp, disparity_right, scale );
179 #endif
180 #ifdef LOAD
181                 disparity_left;
182                 disparity_right;
183                 sprintf(filename,"%s%s_dLgc.png",filepath,dataName);
184                 disparity_left = cvLoadImage(filename,0);
185                 sprintf(filename,"%s%s_dRgc.png",filepath,dataName);
186                 disparity_right = cvLoadImage(filename,0);
187 #endif
188 #ifdef debug
189                 cvNamedWindow("disparity_left");
190                 cvNamedWindow("disparity_right");
191                 cvNamedWindow("ground_truth_left");
192                 cvNamedWindow("ground_truth_right");
193
194                 cvShowImage("disparity_left",disparity_left);
195                 cvShowImage("disparity_right",disparity_right);
196                 cvShowImage("ground_truth_left", groundTruthLeft);
197                 cvShowImage("ground_truth_right", groundTruthRight);
198 #endif
199
200                 /*Calculate RMS error and percentage of bad pixels*/
201                 IplImage* markedDisparity_left = cvCreateImage(cvGetSize(left), IPL_DEPTH_8U, 3);
202                 IplImage* markedDisparity_right = cvCreateImage(cvGetSize(left), IPL_DEPTH_8U, 3);
203                 cvCvtColor(disparity_left,markedDisparity_left,CV_GRAY2RGB);
204                 cvCvtColor(disparity_right,markedDisparity_right,CV_GRAY2RGB);
205                 
206                 int eval_ignore_border = 10;
207                 if (strcmp(dataName,"tsukuba") == 0)
208                         eval_ignore_border = 18;
209
210                 /*Left*/
211         int retcode[2] = {0,0};
212                 if (groundTruthLeft)
213                         retcode[0] = CalculateErrors(disparity_left,groundTruthLeft, markedDisparity_left,
214                                                                                 rms_error[0], percentage_of_bad_pixels[0], maxDisparity, eval_ignore_border);
215                 /*Right*/
216                 if (groundTruthRight)
217                         retcode[1] = CalculateErrors(disparity_right,groundTruthRight, markedDisparity_right,
218                                                                                 rms_error[1], percentage_of_bad_pixels[1], maxDisparity, eval_ignore_border);
219
220 #ifdef SAVEIMAGES
221 #ifndef LOAD    
222                 sprintf(filename,"%s%s_dLgc.png",filepath,dataName);
223                 cvSaveImage(filename,disparity_left);
224                 sprintf(filename,"%s%s_dRgc.png",filepath,dataName);
225                 cvSaveImage(filename,disparity_right);
226
227                 sprintf(filename,"%s%s_mdLgc.png",filepath,dataName);
228                 cvSaveImage(filename,markedDisparity_left);
229                 sprintf(filename,"%s%s_mdRgc.png",filepath,dataName);
230                 cvSaveImage(filename,markedDisparity_right);
231 #endif
232 #endif
233 #ifdef debug
234                 cvNamedWindow("markedDisparity_left");
235                 cvNamedWindow("markedDisparity_right");
236                 cvShowImage("markedDisparity_left",markedDisparity_left);
237                 cvShowImage("markedDisparity_right",markedDisparity_right);
238                 cvWaitKey(1000);
239 #endif
240                 if (retcode[0])
241                 {
242                         ts->printf(CvTS::LOG,"Calculation error");
243                         code = retcode[0];
244                         //goto _exit_;
245                 }
246                 if (retcode[1])
247                 {
248                         ts->printf(CvTS::LOG,"Calculation error");
249                         code = retcode[1];
250                         //goto _exit_;
251                 }
252 #ifdef CONSOLEOUTPUT
253                 printf("\n%s\n",dataName);
254                 if (groundTruthLeft)
255                         printf("L rms error = %f\npercentage of bad pixels = %f\n",
256                                         rms_error[0], percentage_of_bad_pixels[0]);
257                 if(groundTruthRight)
258                         printf("R rms error = %f\npercentage of bad pixels = %f\n",
259                                         rms_error[1], percentage_of_bad_pixels[1]);
260 #endif
261                 for(int i=0; i<2; i++)
262                 {
263                         IplImage* groundTruth = (i == 0) ? groundTruthLeft : groundTruthRight;
264                         if (groundTruth)
265                         {
266                                 if (rms_error[i] > rms_error_thresh)
267                                 {
268                                         ts->printf( CvTS::LOG, "Big RMS error" );
269                                         code = CvTS::FAIL_BAD_ACCURACY;
270                                         //goto _exit_;
271                                 }
272                                 if (percentage_of_bad_pixels[i] > percentage_of_bad_pixels_thresh)
273                                 {
274                                         ts->printf( CvTS::LOG, "Big percentage of bad pixels" );
275                                         code = CvTS::FAIL_BAD_ACCURACY;
276                                         //goto _exit_;
277                                 }
278                         }
279                 }
280         }
281 _exit_:
282                 cvReleaseImage(&left);
283                 cvReleaseImage(&right);
284                 cvReleaseImage(&disparity_left);
285                 cvReleaseImage(&disparity_right);
286                 cvReleaseImage(&groundTruthLeft);
287                 cvReleaseImage(&groundTruthRight);
288 #ifndef LOAD
289                 //cvReleaseStereoCorrespondenceGCState(&stereoMatcher);
290 #endif
291         if( code < 0 )
292         ts->set_failed_test_info( code );
293 }
294
295 CV_StereoCorrespondenceTestGC stereo_correspondece_test_gc;
296
297 #endif