简体   繁体   中英

OpenCV stereo matching inverted output

I am trying to get stereo matching working with artificial depth images. The matching seems to come out good (no occlusions) but inverted( black = close, white = far)

int main()
{
    Mat img1, img2, g1, g2;
    Mat disp, disp8;
    img1 = imread("W:/GoogleDrive/UDK/Croped_left/4.png");
    img2 = imread("W:/GoogleDrive/UDK/Croped_left/1.png");

    cvtColor(img1, g1, CV_BGR2GRAY);
    cvtColor(img2, g2, CV_BGR2GRAY);

    StereoBM sbm;
    sbm.state->SADWindowSize = 9;
    sbm.state->numberOfDisparities = 16;
    sbm.state->preFilterSize = 5;
    sbm.state->preFilterCap = 61;
    sbm.state->minDisparity = -39;
    sbm.state->textureThreshold = 507;
    sbm.state->uniquenessRatio = 0;
    sbm.state->speckleWindowSize = 0;
    sbm.state->speckleRange = 8;
    sbm.state->disp12MaxDiff = 1;
    sbm(g1, g2, disp);

    normalize(disp, disp8, 0, 255, CV_MINMAX, CV_8U);

    imshow("left", img1);
    imshow("right", img2);
    imshow("disp", disp8);

    waitKey(0);

    return(0);
}

These are the images I am using 4.png and 1.png

And the output I get is this: 在此处输入图片说明

Am I doing something wrong? Thanks

I suppose you confused left and right. 4.png should be right/img2 and 1.png left/img1. (The picture with the object to the right is as seen from the left camera and vice versa.)

Well I did a work around using the suggestion by Dainius Šaltenis by inverting the image using the bitwise not operator in opencv and the removing all the pure white pixels.

//Bitwise_not to invert the images
bitwise_not(disp8, disp8);

//Loop through the images find all white pixels and replace with black
for (int i = 0; i < disp8.rows; i++)
    for (int j = 0; j < disp8.cols; j++)
        if (disp8.at<uchar>(i, j) > 254)
            disp8.at<uchar>(i, j) = 0;

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM