2016-03-10 29 views
0

我想獲得立體匹配與人工深度圖像工作。 匹配似乎出來好(無遮擋),但反轉(黑色=關閉,白色=遠)OpenCV立體匹配反轉輸出

int main() 
{ 
    Mat img1, img2, g1, g2; 
    Mat disp, disp8; 
    img1 = imread("W:/GoogleDrive/UDK/Croped_left/4.png"); 
    img2 = imread("W:/GoogleDrive/UDK/Croped_left/1.png"); 

    cvtColor(img1, g1, CV_BGR2GRAY); 
    cvtColor(img2, g2, CV_BGR2GRAY); 

    StereoBM sbm; 
    sbm.state->SADWindowSize = 9; 
    sbm.state->numberOfDisparities = 16; 
    sbm.state->preFilterSize = 5; 
    sbm.state->preFilterCap = 61; 
    sbm.state->minDisparity = -39; 
    sbm.state->textureThreshold = 507; 
    sbm.state->uniquenessRatio = 0; 
    sbm.state->speckleWindowSize = 0; 
    sbm.state->speckleRange = 8; 
    sbm.state->disp12MaxDiff = 1; 
    sbm(g1, g2, disp); 

    normalize(disp, disp8, 0, 255, CV_MINMAX, CV_8U); 

    imshow("left", img1); 
    imshow("right", img2); 
    imshow("disp", disp8); 

    waitKey(0); 

    return(0); 
} 

這些都是我使用4.png1.png

圖像和輸出我得到的是這個:enter image description here

我做錯了什麼? 謝謝

+1

嘗試使用[bitwise_not()](http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#bitwise-not)函數,它反轉所有圖像上的字節(白色到黑色,黑色到白色等)。我從來沒有過立體影像的經驗,不確定它是否可能,但讀過它,它可以幫助你。我使用這個函數來反轉Mat圖像。 –

+0

**深度值不會反轉。黑色值具有**低**距離和**低**像素值,而像素具有**高**深度和**高**像素值!也許你只是混淆了條款? – anderas

+0

是的,深度確實得到了正確的計算,但它是以錯誤的方式表示的。 8位視差圖通常使用255來指示白色,其是靠近照相機的像素,並且0表示距離照相機最遠的黑色像素。 http://students.cec.wustl.edu/~jwaldron/559/project2/depthMaps/torus.jpg – user1031204

回答

0

那麼我做了一個工作,圍繞使用DainiusŠaltenis的建議,通過在opencv中使用按位非運算符來反轉圖像,去除所有純白色像素。

//Bitwise_not to invert the images 
bitwise_not(disp8, disp8); 

//Loop through the images find all white pixels and replace with black 
for (int i = 0; i < disp8.rows; i++) 
    for (int j = 0; j < disp8.cols; j++) 
     if (disp8.at<uchar>(i, j) > 254) 
      disp8.at<uchar>(i, j) = 0; 

enter image description here

0

我想你會左右混淆。 4.png應該是right/img2和1.png left/img1。 (右邊的物體的圖像從左邊的相機看,反之亦然)

+0

如果我以其他方式翻轉圖像,即使經過調整值。 – user1031204

+1

奇怪的是,它似乎爲我工作。無論如何,我很高興你找到了解決方案。 – jodis