繁体   English   中英

从手动创建的矩阵中进行OpenCV立体声校正

[英]OpenCV Stereo rectification from manually created matrices

我目前正在研究X射线图像的三维重建,因此我需要立体校正两个视图的图像,然后才能在epilines的帮助下匹配一些特征。 我正在使用OpenCV 2.4和C ++。

为此,我得到了一组X射线图像(锥形束X射线图像,没有真实摄像机,具有失真参数或真实焦距),一个来自前后视图(直接看胸部),一个从侧面看(从侧面看胸部)。 我知道一些参数,如我可以使用的虚拟焦距(两个视图相等),并且图像的分辨率为512x512px,因此两个视图的图像相机投影均为(255,255)。 我也知道相机是垂直的。 根据这些信息,我开发了一个旋转矩阵R和一个平移向量t(两者都是在Matlab的3d图中帮助验证的)。

问题:在OpenCV中,R和t实际上足以进行立体声整流,但整流后得到的图像是黑色的。 谷歌搜索引发了我在stereoRectify中的一个错误,但我怀疑这是错误,因为我可以运行OpenCV stereoRectification示例,它确实有效。 在Matlab中尝试立体声再现时,我至少可以看到一些失真的整流结果。

这是我的C ++代码:

float camera_matrix_ap_data[] = {1207*2.0, 0.0, 255.0,
                 0.0, 1207*2, 255.0,
                                 0.0, 0.0, 1.0};
cv::Mat camera_matrix_ap(3, 3, CV_64F, camera_matrix_ap_data);
float camera_matrix_lat_data[] = {1207*2, 0.0, 255.0,
                 0.0, 1207*2, 255.0,
                                 0.0, 0.0, 1.0};
 cv::Mat camera_matrix_lat(3, 3, CV_64F, camera_matrix_lat_data);

 ///
 /// @brief the distortion matrices
 ///
 cv::Mat distortion_ap(4, 1, CV_64F, 0.0);
 cv::Mat distortion_lat(4, 1, CV_64F, 0.0);

 ///
 /// @brief Translation and Rotation matrices
 ///
 float R_data[] = {0.0, 0.0, 1.0,
           0.0, 1.0, 0.0,
           -1.0, 0.0, 0.0};
 float T_data[] = {-(1207.0*2 + 255), 0.0, 1207.0*2 + 255};

 cv::Mat R(3, 3, CV_64F, R_data);
 cv::Mat T(3, 1, CV_64F, T_data);

 for (int i=1; i<=20; i++) {
std::stringstream filenameAP_tmp;
std::stringstream filenameLAT_tmp;
filenameAP_tmp << "imageAP"<< i <<".jpg";
filenameAP = filenameAP_tmp.str();
filenameLAT_tmp << "imageLAT"<< i <<".jpg";
filenameLAT = filenameLAT_tmp.str();

    rectimg_ap = cv::imread(filenameAP);
    rectimg_lat = cv::imread(filenameLAT);
    // Yes, these images are grayscale

    /// Experimental
    /// Stereo rectify both images
 cv::Mat R1(3, 3, CV_64F);
 cv::Mat R2(3, 3, CV_64F);
 cv::Mat P1(3, 4, CV_64F);
 cv::Mat P2(3, 4, CV_64F);
 cv::Mat Q(4, 4, CV_64F);
 cv::Rect validRoi[2];

// buggy?
cv::stereoRectify(camera_matrix_ap, distortion_ap, camera_matrix_lat, distortion_lat, rectimg_ap.size(), R, T, R1, R2, P1, P2, Q, CALIB_ZERO_DISPARITY, 1, rectimg_ap.size(), &validRoi[0], &validRoi[1] );


// Maps for AP View
cv::Mat map1x(rectimg_ap.size(), CV_32FC1, 255.0);
cv::Mat map2x(rectimg_ap.size(), CV_32FC1, 255.0);
// Maps for LAT View
cv::Mat map1y(rectimg_ap.size(), CV_32FC1, 255.0);
cv::Mat map2y(rectimg_ap.size(), CV_32FC1, 255.0);

cv::initUndistortRectifyMap(camera_matrix_ap, distortion_ap, R1, P1, rectimg_ap.size(), CV_32FC1, map1x, map1y);
cv::initUndistortRectifyMap(camera_matrix_lat, distortion_lat, R2, P2, rectimg_lat.size(), CV_32FC1, map2x, map2y);

cv::Mat tmp1, tmp2;
cv::remap(rectimg_ap, tmp1, map1x, map1y, INTER_LINEAR);
cv::remap(rectimg_lat, tmp2, map2x, map2y, INTER_LINEAR);

//findHomography(rectimg_ap, rectimg_lat, CV_RANSAC);

}

所以我想知道这个代码或我的矩阵有什么问题,因为重映射后的整流图像是完全黑色的。 OpenCV和Matlab之间的坐标系轴是否存在差异? 在我看来,在OpenCV中,z轴指向图像平面,对于Matlab来说也是如此。

如果有人可以帮助我,我会很高兴,我现在已经坚持了好几个星期。 非常感谢你!

尝试将“float”变量类型更改为“double”。 CV_64F对应于double,而不是float,因为它是8个字节(= 64位)。 我用我自己的矩阵值尝试了你的代码,这就是诀窍。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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