[英]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.