For details about the sad algorithm, see related image processing books.
This program was contributed by the excellent person of the opencv Chinese forum and thanked him for his work.
(For images required by the program, visit http://vision.middlebury.edu/stereo/data/scenes2003)
// Sum of absolute difference (SAD) </P> <p> # include <iostream> <br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <cv. h> <br/> # include <cxcore. h> <br/> # include <pighgui. h> <br/> # include <math. h> <br/> # include <ctime> </P> <p> using namespace STD; </P> <p> template <class T> class image <br/>{< br/> PRIVATE: <br/> iplimage * imgp; </P> <p> Public: <br/> image (iplimage * IMG = 0) {imgp = IMG;} <br/> ~ Image () {imgp = 0 ;}< br/> void operator = (iplimage * IMG) {imgp = IMG ;} <br/> inline T * operator [] (const int rowindx) <br/>{< br/> return (T *) (imgp-> imagedata + rowindx * imgp-> widthstep); <br/>}< br/> }; </P> <p> typedef struct <br/>{< br/> unsigned char B, G, R; <br/>} rgbpixel; </P> <p> typedef struct <br/>{< br/> float B, G, R; <br/>} rgbpixelfloat; </P> <p> typedef image <rgbpixel> rgbimage; <br/> typedef image <rgbpixelfloat> Rgbimagefloat; <br/> typedef image <unsigned char> bwimage; <br/> typedef image <float> bwimagefloat; </P> <p> // display an image in a new window with title to be given. <br/> void displayimagenewwindow (char * Title, cvarr * IMG) <br/> {<br/> cvnamedwindow (title, cv_window_autosize); <br/> cvshowimage (title, IMG); <br/>}</P> <p> int getmaxmin (double value [], int valuesize, int maxmin) <br/>{< br/> int Pos = 0; <br /> Int I = 0; <br/> double max1 =-1 ;//? -999999; <br/> double min1 = 999999; </P> <p> If (maxmin = 1) <br/> {<br/> // find Max <br/> for (I = 0; I <valuesize; I ++) <br/>{< br/> // find the index with the max value; <br/> If (value [I]> max1) <br/>{< br/> Pos = I; <br/> max1 = value [I]; <br/>}</P> <p> If (maxmin = 0) <br/> {<br/> // find Min <br/> for (I = 0; I <valuesize; I ++) <br/>{< br/> // find the index with the minimum value; <br/> If (value [I] <min1) <br/>{< br/> Pos = I; <br/> min1 = value [I]; <br/>}</P> <p> return Pos; <br/>}</P> <p> iplimage * generatedisparityimage (iplimage * greyleftimg32, iplimage * greyrightimg32, int windowsize, int DSR) <br/>{< br/> int offset = floor (double) windowsize/2); <br/> int Height = greyleftimg32-> height; <br/> int width = greyleftimg32-> width; <br/> double * localsad = new double [DSR]; </P> <p> int x = 0, y = 0, D = 0, m = 0; <br/> int n = windowsize; </P> <p> iplimage * winimg = cvcreateimage (cvsize (n, n),); // mysubimage (greyleftimg32, cvrect (, n, n); </P> <p> iplimage * disparity = cvcreateimage (cvsize (width, height), 8, 1); // or ipl_depth_8u <br/> bwimage imga (disparity); </P> <p> for (y = 0; y <peight; Y ++) <br/>{< br/> for (x = 0; x <width; X ++) <br/>{< br/> imga [y] [x] = 0; <br/>}</P> <p> cvscalar sum; <br/> // cvscalar S2; <br/> for (y = 0; y <peight-N; y ++) <br/> {<br/> // height-n <br/> for (x = 0; x <width-N; X ++) <br/>{< br/> // width-n <br/> cvsetimageroi (greyleftimg32, cvrect (X, Y, n, n )); <br/> D = 0; <br/> // initialise localsad <br/> for (m = 0; m <DSR; m ++) <br/> {<br/> localsad [m] = 0; <br/>}</P> <p> // start matching <br/> do {<br/> If (X-D> = 0) <br/>{< br/> cvsetimageroi (greyrightimg32, cvrect (X-D, Y, n, n )); <br/>}< br/> else <br/>{< br/> break; <br/>}</P> <p> cvabsdiff (greyleftimg32, greyrightimg32, winimg); // absolute difference <br/> sum = cvsum (winimg); // sum <br/> localsad [d] = sum. val [0]; // 0 means single channel </P> <p> cvresetimageroi (greyrightimg32); <br/> D ++; <br/>}while (d <= DSR ); </P> <p> // to find the best D and store <br/> imga [Y + offset] [x + offset] = getmaxmin (localsad, DSR, 0) * 16; // 0 means return minimum index <br/> cvresetimageroi (greyleftimg32); <br/>}// x <br/> If (Y % 10 = 0) <br/> cout <"ROW =" <Y <"of" <peight <Endl; <br/>}// Y </P> <p> cvreleaseimage (& winimg); <br/> // cvreleaseimage (& rightwinimg ); </P> <p> return disparity; <br/>}</P> <p> int main (INT argc, char * const argv []) <br/>{ <br/> cout <"sum of absolute difference (SAD) strereo vision" <Endl; </P> <p> // ************ image input ******************* ** // <br/> char * filename1 = "l.jpg "; // im2_cone.png <br/> iplimage * greyleftimg = cvloadimage (filename1, 0); <br/> char * filename2 = "r.jpg "; <br/> iplimage * greyrightimg = cvloadimage (filename2, 0); </P> <p> If (greyleftimg = NULL) {cout <"no valid image input. "<Endl; return 1 ;}< br/> If (greyrightimg = NULL) {cout <" no valid image input. "<Endl; return 1 ;}</P> <p> int width = greyleftimg-> width; <br/> int Height = greyleftimg-> height; </P> <p>/***************** 8u to 32f ************* * *******/<br/> iplimage * greyleftimg32 = cvcreateimage (cvsize (width, height),); // required <br/> iplimage * greyrightimg32 = cvcreateimage (cvsize (width, height),); <br/> cvconvertscale (greyleftimg, greyleftimg32, 1/255 .); <br/> cvconvertscale (greyrightimg, greyrightimg32, 1/255 .); // 1/255. equals to 1/255. 0 </P> <p> // ------------- obtain disparity image ---------------- <br/> time_t tstart, tend; <br/> tstart = time (0 ); <br/> int windowsize = 13, DSR = 20; // disparity search range <br/> iplimage * disparity32 = generatedisparityimage (greyleftimg32, greyrightimg32, windowsize, DSR ); <br/> tend = time (0); <br/> cout <"It took" <difftime (tend, tstart) <"second (s ). "<Endl; </P> <p> displayimagenewwindow (" dispairty image ", disparity32); <br/> displayimagenewwindow (" left image ", greyleftimg32 ); <br/> displayimagenewwindow ("right image", greyrightimg32); </P> <p> // cvsaveimage ("D:/opencv_stuff/sampleimages/disparitysad.jpg", disparity32 ); </P> <p> // ********* destroy window *************/<br/> cvwaitkey (0); <br/> cvreleaseimage (& images); <br/> cvreleaseimage (& greyrightimg32); <br/> cvreleaseimage (& greyleftimg); <br/> cvreleaseimage (& greyrightimg ); <br/> cvreleaseimage (& disparity32); <br/> cvdestroywindow ("left image"); <br/> cvdestroywindow ("right image "); <br/> cvdestroywindow ("dispairty image"); <br/> return 0; <br/>}</P> <p>