2

我的问题是我的视差图中的颜色是向后的。因为距离较远的东西比靠近相机的东西要轻。

我尝试了很多东西(即 convertTo、convertScaleAbs 以及其中的各种值组合等),但似乎无法让视差图中的颜色反转(即正常 - 靠近的东西比远离的东西更轻) .

我需要一些帮助才能做到这一点。

另外,出于好奇,我怎样才能将视差图的颜色空间更改为我在网上看到的 MATLAB 中的彩色空间?

这是我的代码以及pastebin。http://pastebin.com/E3vVN6UU

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <iostream>
#include <string>

using namespace cv;
using namespace std;

void show(const char* windowname, Mat image)
{
    namedWindow(windowname, CV_WINDOW_AUTOSIZE);
    imshow(windowname, image);
}

int main()
{ 
    Mat image1, image2;

    Mat camMat1 = (Mat_<double>(3,3) << 793.1338, 0, 337.2309, 0, 792.0555, 256.9991, 0, 0, 1);
    Mat camMat2 = (Mat_<double>(3,3) << 799.1271, 0, 319.8581, 0, 797.2460, 243.4638, 0, 0, 1);
    Mat dispCoeffs1 = (Mat_<double>(1,5) << 0.0033, -0.1320, -0.0019, 0.0026, 0);
    Mat dispCoeffs2 = (Mat_<double>(1,5) << -0.0109, -0.0188, -0.0014, -0.0055, 0);
    Mat RotMat = (Mat_<double>(3,3) << 0.9998, -0.0023, 0.0221, 0.0022, 1, 0.0031, -0.0221, -0.0031, 0.9998);
    Mat TransMat = (Mat_<double>(3,1) << 374.2306, -1.8319, 5.5745);

    //Rectify 
    Mat R1, R2, P1, P2, Q;
    stereoRectify(camMat1, dispCoeffs1, camMat2, dispCoeffs2, Size(640,480), RotMat, TransMat, R1, R2, P1, P2, Q, CV_CALIB_ZERO_DISPARITY, 1, Size(640,480));

    //Define the mapping to the done
    Mat rx1, ry1;
    Mat rx2, ry2;
    initUndistortRectifyMap(camMat1, dispCoeffs1, R1, P1, Size(640,480), CV_16SC2, rx1, ry1);
    initUndistortRectifyMap(camMat2, dispCoeffs2, R2, P2, Size(640,480), CV_16SC2, rx2, ry2);

    //SET THE BM STATE VARIABLES BEGIN - DONE GLOBALLY
    StereoBM bm;
    bm.state->preFilterSize = 31;
    bm.state->preFilterCap = 63;
    bm.state->SADWindowSize = 9;
    bm.state->minDisparity = -128;
    //bm.state->disp12MaxDiff = 2;
    bm.state->numberOfDisparities = 128;
    bm.state->textureThreshold = 50;
    bm.state->uniquenessRatio = 15;
    bm.state->speckleWindowSize = 100;
    bm.state->speckleRange = 16;
    //SET THE BM STATE VARIABLES END

    VideoCapture cap3 = VideoCapture(0);
    VideoCapture cap4 = VideoCapture(1);

    //cap3.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    //cap3.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
    //cap4.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    //cap4.set(CV_CAP_PROP_FRAME_HEIGHT, 240);

    cap3 >> image1;
    cap4 >> image2;
    Size imageSize = image1.size();

    Mat gray_image1;
    Mat gray_image2;
    Mat frame1r;
    //frame1r.create(image1.size(), CV_8U);
    Mat frame2r;
    //frame2r.create(image2.size(), CV_8U);
    Mat frame1rf;
    Mat frame2rf;
    //Mat disp(image1.size(), CV_16S);
    //Mat vdisp(image1.size(), CV_8U);
    Mat disp, vdisp;
    //Mat image3d(image1.size(), CV_32FC3);
    Mat image3d;
    Mat rectified_pair;
    rectified_pair.create(imageSize.height, (imageSize.width)*2, CV_8UC3);

    //Actually do the mapping -- based on the mapping definition

    while(1)
    {
        bm.state->preFilterSize = 31;
        bm.state->preFilterCap = 63;
        bm.state->SADWindowSize = 21;
        bm.state->minDisparity = -128;
        //bm.state->disp12MaxDiff = 2;
        bm.state->numberOfDisparities = 64;
        bm.state->textureThreshold = 20;
        bm.state->uniquenessRatio = 10;
        bm.state->speckleWindowSize = 100;
            bm.state->speckleRange = 32;

        cvtColor(image1, gray_image1, CV_BGR2GRAY);
        cvtColor(image2, gray_image2, CV_BGR2GRAY);
        remap(gray_image1, frame1r, rx1, ry1, CV_INTER_LINEAR);
        remap(gray_image2, frame2r, rx2, ry2, CV_INTER_LINEAR);
        bm(frame1r, frame2r, disp);
        normalize(disp, vdisp, 0, 255, NORM_MINMAX, CV_8U);
        //convertScaleAbs(vdisp, vdisp, 1, 0);
        disp.convertTo(vdisp, CV_8U, 255/(64*16.));
        show("disparity", vdisp);
        //reprojectImageTo3D(disp, image3d, Q, true);
        //show("depth map", image3d);

        //display image side by side for rectified window
        //copy frame1r to the left side
        cvtColor(frame1r, frame1rf, CV_GRAY2BGR);
        frame1rf.copyTo(rectified_pair(Rect(0,0,imageSize.width, imageSize.height)));
        //copy frame2r to the right side
        cvtColor(frame2r, frame2rf, CV_GRAY2BGR);
        frame2rf.copyTo(rectified_pair(Rect(imageSize.width,0,imageSize.width, imageSize.height)));
        for(int i=0; i<imageSize.height; i+=32)
            line(rectified_pair, Point(0,i), Point((imageSize.width)*2, i), CV_RGB(0,255,0));
        show("rectified", rectified_pair);

        cap3 >> image1;
        cap4 >> image2;
        if(waitKey(15) == 27)
            break;
    }

    return 0;
}
4

2 回答 2

1

我没有使用立体声对,但使用 Kinect 获得相同的结果 - 远 = 亮,近 = 暗 要改变这一点,我使用了以下内容:

  double min, max;
  minMaxLoc(depthImage, &min, &max);

  depthImage.convertTo(rImage, CV_8U, -255.0/max, 255);
于 2013-02-16T16:45:52.380 回答
0

我遇到了同样的问题,然后我尝试交换左右图像,它奏效了!现在我得到了正确的图像。

于 2013-11-12T16:22:00.123 回答