7

我一直在尝试使用 OpenCV stereoRectifyUncalibrated 为一对图像校正和构建视差映射,但我没有得到很好的结果。我的代码是:

template<class T>
T convertNumber(string& number)
{
    istringstream ss(number);
    T t;
    ss >> t;
    return t;
}

void readPoints(vector<Point2f>& points, string filename)
{
    fstream filest(filename.c_str(), ios::in);
    string line;

    assert(filest != NULL);

    getline(filest, line);
    do{
        int posEsp = line.find_first_of(' ');
        string posX = line.substr(0, posEsp);
        string posY = line.substr(posEsp+1, line.size() - posEsp);

        float X = convertNumber<float>(posX);
        float Y = convertNumber<float>(posY);

        Point2f pnt = Point2f(X, Y);
        points.push_back(pnt);
        getline(filest, line);
    }while(!filest.eof());

    filest.close();
}

void drawKeypointSequence(Mat lFrame, Mat rFrame, vector<KeyPoint>& lKeyp, vector<KeyPoint>& rKeyp)
{
    namedWindow("prevFrame", WINDOW_AUTOSIZE);
    namedWindow("currFrame", WINDOW_AUTOSIZE);
    moveWindow("prevFrame", 0, 300);
    moveWindow("currFrame", 650, 300);
    Mat rFrameAux;
    rFrame.copyTo(rFrameAux);
    Mat lFrameAux;
    lFrame.copyTo(lFrameAux);

    int size = rKeyp.size();
    for(int i=0; i<size; i++)
    {
        vector<KeyPoint> drawRightKeyp;
        vector<KeyPoint> drawleftKeyp;

        drawRightKeyp.push_back(rKeyp[i]);
        drawleftKeyp.push_back(lKeyp[i]);

        cout << rKeyp[i].pt << " <<<>>> " << lKeyp[i].pt << endl;

        drawKeypoints(rFrameAux, drawRightKeyp, rFrameAux, Scalar::all(255), DrawMatchesFlags::DRAW_OVER_OUTIMG);
        drawKeypoints(lFrameAux, drawleftKeyp, lFrameAux, Scalar::all(255), DrawMatchesFlags::DRAW_OVER_OUTIMG);

        imshow("currFrame", rFrameAux);
        imshow("prevFrame", lFrameAux);
        waitKey(0);
    }
    imwrite("RightKeypFrame.jpg", rFrameAux);
    imwrite("LeftKeypFrame.jpg", lFrameAux);
}
int main(int argc, char* argv[])
{
    StereoBM stereo(StereoBM::BASIC_PRESET, 16*5, 21);
    double ndisp = 16*4;
    assert(argc == 5);
    string rightImgFilename(argv[1]);       // Right image (current frame)
    string leftImgFilename(argv[2]);        // Left image (previous frame)
    string rightPointsFilename(argv[3]);    // Right image points file
    string leftPointsFilename(argv[4]);     // Left image points file

    Mat rightFrame = imread(rightImgFilename.c_str(), 0);
    Mat leftFrame = imread(leftImgFilename.c_str(), 0);

    vector<Point2f> rightPoints;
    vector<Point2f> leftPoints;

    vector<KeyPoint> rightKeyp;
    vector<KeyPoint> leftKeyp;

    readPoints(rightPoints, rightPointsFilename);
    readPoints(leftPoints, leftPointsFilename);
    assert(rightPoints.size() == leftPoints.size());

    KeyPoint::convert(rightPoints, rightKeyp);
    KeyPoint::convert(leftPoints, leftKeyp);

    // Desenha os keypoints sequencialmente, de forma a testar a consistência do matching
    drawKeypointSequence(leftFrame, rightFrame, leftKeyp, rightKeyp);

    Mat fundMatrix = findFundamentalMat(leftPoints, rightPoints, CV_FM_8POINT);
    Mat homRight;
    Mat homLeft;
    Mat disp16 = Mat(rightFrame.rows, leftFrame.cols, CV_16S);
    Mat disp8 = Mat(rightFrame.rows, leftFrame.cols, CV_8UC1);
    stereoRectifyUncalibrated(leftPoints, rightPoints, fundMatrix, rightFrame.size(), homLeft, homRight);

    warpPerspective(rightFrame, rightFrame, homRight, rightFrame.size());
    warpPerspective(leftFrame, leftFrame, homLeft, leftFrame.size());


    namedWindow("currFrame", WINDOW_AUTOSIZE);
    namedWindow("prevFrame", WINDOW_AUTOSIZE);
    moveWindow("currFrame", 650, 300);
    moveWindow("prevFrame", 0, 300);
    imshow("currFrame", rightFrame);
    imshow("prevFrame", leftFrame);

    imwrite("RectfRight.jpg", rightFrame);
    imwrite("RectfLeft.jpg", leftFrame);

    waitKey(0);

    stereo(rightFrame, leftFrame, disp16, CV_16S);

    disp16.convertTo(disp8, CV_8UC1, 255/ndisp);
    FileStorage file("disp_map.xml", FileStorage::WRITE);
    file << "disparity" << disp8;
    file.release();
    imshow("disparity", disp8);
    imwrite("disparity.jpg", disp8);
    moveWindow("disparity", 0, 0);

    waitKey(0);
}

drawKeyPoint 序列是我直观地检查两个图像的点的一致性的方式。通过按顺序绘制它们的每个关键点,我可以确定图像 A 上的关键点 i 是图像 B 上的关键点 i。

我也尝试过使用 ndisp 参数,但没有太大帮助。

我尝试了以下一对图像:

左图

右图

得到以下纠正对:

修正左

纠正右

最后,下面的视差图

视差图

如您所见,这非常糟糕。我还使用以下 stereoRectifyUncalibrated 示例尝试了同一对图像:http: //programmingexamples.net/wiki/OpenCV/WishList/StereoRectifyUncalibrated和 opencv 教程代码示例中的 SBM_Sample.cpp 来构建视差图,并得到了非常相似的结果。

我正在使用 opencv 2.4

提前致谢!

4

2 回答 2

2

除了可能的校准问题外,您的图像显然缺少一些纹理,无法使立体块匹配正常工作。该算法将在平坦(非纹理)部分上看到许多歧义和太大的差异。

但是请注意,关键点似乎匹配得很好,所以即使校正输出看起来很奇怪,它也可能是正确的。

您可以针对来自Middlebury 立体页面的标准图像测试您的代码,以进行完整性检查。

于 2013-04-11T07:23:06.470 回答
0

我建议使用棋盘进行立体校准,或者使用棋盘拍摄多张照片并stereocalibrate.cpp在计算机上使用。我是说因为您使用的是stereorectifyuncalibrated,虽然该算法不需要知道相机的内在参数,但它在很大程度上取决于对极几何。因此,如果相机镜头有明显的畸变,最好在计算基本矩阵并调用此函数之前对其进行校正。例如,可以使用 分别估计立体摄像机每个头部的失真系数calibrateCamera()。然后,可以使用 校正图像undistort(),或者可以使用 校正点坐标undistortPoints()

于 2013-01-20T20:24:30.503 回答