我正在尝试制作视差图。我看到了 opencv 'stereo_match.cpp' 提供的示例代码,我写了以下代码。但是当我在校正和重新映射后显示左右图像时,图像是黑色的。谁能告诉我我哪里做错了?
int main(int argc, char* argv[])
{
Mat img1, img2, g1, g2;
Mat disp, disp8;
char* method ="SGBM";
float scale = 1.f; // don't know why
//img1 = imread(argv[1]);
//img2 = imread(argv[2]);
img1=imread("l1.jpg");
img2=imread("r1.jpg");
cvtColor(img1, g1, CV_BGR2GRAY);
cvtColor(img2, g2, CV_BGR2GRAY);
Size img_size = img1.size();
Rect roi1, roi2;
Mat Q;
/*reading parameters of ectrinssic & intrinssic file*/
const char* intrinsic_filename="intrinsics";
Mat img1r, img2r;
if( intrinsic_filename )
{
FileStorage fs("intrinsics.yml", cv::FileStorage::READ);
if(!fs.isOpened())
{
printf("Failed to open file %s\n");
return -1;
}
Mat M1, D1, M2, D2;
fs["M1"] >> M1;
fs["D1"] >> D1;
fs["M2"] >> M2;
fs["D2"] >> D2;
M1 *= scale;
M2 *= scale;
fs.open("extrinsics.yml", cv::FileStorage::READ);
if(!fs.isOpened())
{
printf("Failed to open file %s\n");
return -1;
}
Mat R, T, R1, P1, R2, P2;
fs["R"] >> R;
fs["T"] >> T;
stereoRectify( M1, D1, M2, D2, img_size, R, T, R1, R2, P1, P2, Q, CALIB_ZERO_DISPARITY, -1, img_size, &roi1, &roi2 );
Mat map11, map12, map21, map22;
initUndistortRectifyMap(M1, D1, R1, P1, img_size, CV_16SC2, map11, map12);
initUndistortRectifyMap(M2, D2, R2, P2, img_size, CV_16SC2, map21, map22);
remap(img1, img1r, map11, map12, INTER_LINEAR);
remap(img2, img2r, map21, map22, INTER_LINEAR);
// img1 = img1r;
// img2 = img2r;
imshow("left1", img1r);
imshow("left2", img2r);
}
}
} ![原左图][3]
最佳答案
你没有在 cvtColor(...) 上切换输入/输出参数:
cvtColor(img1, g1, CV_BGR2GRAY);
cvtColor(img2, g2, CV_BGR2GRAY);
g1 和 g2 似乎是输入图像的灰度版本,但在后面的代码中从未使用过它们。
你为什么不反过来试试呢?:
g1=imread("l1.jpg");
g2=imread("r1.jpg");
cvtColor(g1, img1, CV_BGR2GRAY);
cvtColor(g2, img2, CV_BGR2GRAY);
关于c++ - 视差图 opencv,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20347273/