openCV中的stereoRectify使用相机方向和平移无法给出正确的结果

时间:2019-07-10 17:26:08

标签: opencv photogrammetry

我有一个立体图像,其内部和外部方向为first camera Second camera,可以通过摄影测量软件(峰演变)summit evolution triangulation获得。

尽管我已正确应用了OpenCV文档中提到的步骤以及StackOverflow中给定的示例。但是我仍然无法获得校正后的图像,并且结果总是错误的。

尽管我尝试了不同的Alpha值-1、0和1,但结果仍然是错误的。 此外,对于相对方向,我尝试了不同的组合,因为cv :: composeRT给出了错误:

Rrel = R2 * tran(R1),Trel = T1-tran(R)* T2;

Rrel = R2 * tran(R1),Trel = T2-(R)* T1;

Rrel = R1 * tran(R2),Trel = T2-tran(R)* T1;和

Rrel = R2 * tran(R1),Trel = T1-T2;

如果有人可以帮助我解决这个问题,我将不胜感激,因为我已经花了一个多月的时间,仅出于信息目的,我还将OpenCV从3.4升级到4.1,但仍然没有用。

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<limits.h> 
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#define R 5 
#define C 5

const char* source_window = "Source image";
const char* corners_window = "Corners detected";
using namespace cv;
using namespace std;

//int minCostMAt(int cost[R][C], int m, int n);
using namespace std;
int main()
{



    ///////////camera parameters//////////////////////////////

    cv::Mat K1 = (cv::Mat_<double>(3, 3) << 16750, 0,8655 ,
        0, 16750, 5635,
        0, 0, 1);
    cv::Mat kk1 = cv::Mat_<double>::zeros(1, 5);
    cv::Mat K2 = (cv::Mat_<double>(3, 3) << 16750, 0,8655 ,
        0, 16750,5635 ,
        0, 0, 1);

    cv::Mat kk2 = cv::Mat_<double>::zeros(1, 5);


    ///////////////////rotation matrix for the images/////////
    cv::Mat RR1 = (cv::Mat_<double>(3, 3) << 
        -0.99999100324, -0.00227689086, -0.00357899578,
        0.00227693999, -0.99999740772, -0.00000965317,
        -0.00357896452, -0.00001780224, 0.99999359533

        );
    cv::Mat TT1 = (cv::Mat_<double>(3, 1) << 412130.090, 4000048.907, 2229.931);

    cv::Mat RR2 = (cv::Mat_<double>(3, 3) <<
        -0.99999300398, -0.00163328377, -0.00336517143,
        0.00163222195, -0.99999861728, 0.00031825616,
        -0.00336568658, 0.00031276123, 0.99999428715

        );
    cv::Mat TT2 = (cv::Mat_<double>(3, 1) << 412133.231, 4000524.367, 2230.390);

    ///////////////////////
    /*
    Mat RR1tran;// = RR2.inv();
    transpose(RR1, RR1tran);
    cv::Mat RRR = RR2 * RR1tran;
    cv::Mat RRRT;
    cv::Mat RT;
    transpose(RRR, RT);
    cv::Mat RT_temp = RT * TT2;
    RRRT = TT1 - RT_temp;
    */
    /////////////////////////////
    ///////////////////////

    Mat RR1tran;// = RR2.inv();
    transpose(RR1, RR1tran);
    cv::Mat RRR = RR2 * RR1tran;
    cv::Mat RRRT;
    cv::Mat RT;
    transpose(RRR, RT);
    cv::Mat RT_temp = RRR * TT1;
    //RRRT = TT2 - RT_temp;
    RRRT = TT1 - TT2;
    /////////////////////////////
    ///////////////////////
    /*
    Mat RR2tran;// = RR2.inv();
    transpose(RR2, RR2tran);
    cv::Mat RRR = RR1 * RR2tran;
    cv::Mat RRRT;
    cv::Mat RT;
    transpose(RRR, RT);
    cv::Mat RT_temp = RT * TT1;
    //RRRT = TT2 - RT_temp;
    //RRRT = TT2 - TT1;
*/

    /////////////////////////////


    cv::Size imgsize(17310, 11310);
    /////////////////////////

    Mat composedRvec = Mat_<float>(3, 3);// << 1, 0, 1, 1, 1, 1, 1, 1, 0);
                                         // cv::at composedRvec;
    Mat composedTvec = Mat_<float>(3, 1);// << 1, 0, 1, 1, 1, 1, 1, 1, 0);M
    cv::Mat compR;
    cv::Mat compT;
//  cv::composeRT(RR1, TT1, RR2, TT2, compR, compT);

    //////////////////////




    cv::Mat R1;
    cv::Mat R2;
    cv::Mat P1;
    cv::Mat P2;
    cv::Mat Q;
    cv::Rect RL;
    cv::Rect RR;
    cv::stereoRectify(K1, kk1, K2, kk2, imgsize, RRR, RRRT, R1, R2, P1, P2, Q, 1,0, imgsize, &RL, &RR);//the value of alpha with -1 is wgives bot 
                                                                                                         // cv::stereoRectify(K0, kk0, K1, kk1, imgsize, RRR, T, R1, R2, P1, P2, Q, 1, 0, imgsize, &RL, &RR);
                                                                                                         //cv::stereoRectify(K0, kk0, K1, kk1, imgsize, RRR, T, R1, R2, P1, P2, Q, 1, 1.0, imgsize, &RL, &RR);
                                                                                                         //cv::stereoRectify(K0, kk0, K1, kk1, imgsize, RRR, T, R1, R2, P1, P2, Q, 0, 1.0, imgsize, &RL, &RR);
    std::cout << "Results with OpenCV " << CV_VERSION_MAJOR << "." << CV_VERSION_MINOR << "." << CV_VERSION_REVISION << std::endl;
    std::cout << "R1 = " << R1 << std::endl;
    std::cout << "R2 = " << R2 << std::endl;
    std::cout << "P1 = " << P1 << std::endl;
    std::cout << "P2 = " << P2 << std::endl;
    std::cout << " Q = " << Q << std::endl;
    std::cout << "RL = " << RL << std::endl;
    std::cout << "RR = " << RR << std::endl;
    //////////stereorectify matrix/////////////////////////////////////////////////////////////////////////////

    cv::Mat img1 = cv::imread("E:\\12_0628.tif", 0);
    cv::Mat img2 = cv::imread("E:\\12_0629.tif", 0);


    Mat map1x, map1y, map2x, map2y;
    Mat imgdst1, imgdst2;


    initUndistortRectifyMap(K1, kk1, R1, P1, Size(img1.cols, img1.rows), CV_32FC1, map1x, map2x);
    initUndistortRectifyMap(K2, kk2, R2, P2, Size(img1.cols, img1.rows), CV_32FC1, map1y, map2y);
    cv::remap(img1, imgdst1, map1x, map2x, INTER_LANCZOS4);
    cv::remap(img2, imgdst2, map1y, map2y, INTER_LANCZOS4);

////////////////////////////////////
    imwrite("E:\\Disparity_imgL.jpg", imgdst1);
    imwrite("E:\\Disparity_imgR.jpg", imgdst2);
    cv::waitKey(0);
    system("pause");
}
```

0 个答案:

没有答案