Opencv provides a function for binocular vision to detect distance information. We tried to use two cameras to obtain two sets of images for testing. When the background is simple and the target is small, the results are better. (They are the left image and the right image respectively, and the obtained depth image)
Using a long object to test the depth is ineffective. As follows:
The difficulty is that the two cameras, even in the same model and lens, have slight differences in the color of the images, resulting in poor performance.
However, using a camera to pan the left and right results in better results. As an image provided by opencv, the effect is better.
Code:
# Include "stdafx. H"
# Include <iostream>
# Include <string. h>
# Include <cxcore. h>
# Include <cv. h>
# Include <cvaux. h>
# Include # Include <fstream>
Iplimage * image = 0; // Original Image
Using namespace STD;
Int main (INT argc, char * argv [])
{
Iplimage * srcleft = cvloadimage ("left.jpg", 1 );
Iplimage * srcright = cvloadimage ("right.jpg", 1 );
Iplimage * leftimage = cvcreateimage (cvgetsize (srcleft), ipl_depth_8u, 1 );
Iplimage * rightimage = cvcreateimage (cvgetsize (srcright), ipl_depth_8u, 1 );
Iplimage * depthimage = cvcreateimage (cvgetsize (srcright), ipl_depth_8u, 1 );
Cvcvtcolor (srcleft, leftimage, cv_bgr2gray );
Cvcvtcolor (srcright, rightimage, cv_bgr2gray );
Cvfindstereocorresponsor (leftimage, rightimage, cv_disparity_birchfield, depthimage, 50, 15, 3, 6, 8, 15 );
Cvnamedwindow ("win1", 1 );
Cvnamedwindow ("win2", 1 );
Cvnamedwindow ("win3", 1 );
Cvnormalize (depthimage, depthimage, 0,255, cv_minmax, 0 );
For (;;)
{
Cvshowimage ("win1", depthimage );
Cvshowimage ("win2", srcleft );
Cvshowimage ("win3", srcright );
If (cvwaitkey (20) = 27) break;
}
Return 0;
}