从单个移动相机拍摄的两张图像中获取视差图

时间:2018-03-13 09:30:13

标签: matlab image-processing

我正在使用单个移动相机拍摄两张照片。我试图从相同的极线上取下它们。

enter image description here

enter image description here

然后我在Matlab中计算差异。

I1 = imread('4.jpg');  %right image
I2 = imread('3.jpg');  %left image

figure
imshow(stereoAnaglyph(I1,I2));
title('Red-cyan composite view of the stereo images');

disparityRange = [-6 26];
disparityMap = disparity(rgb2gray(I1),rgb2gray(I2),'DisparityRange',disparityRange);

figure 
imshow(disparityMap,disparityRange);
title('Disparity Map');
colormap(gca,jet) 
colorbar

我的输出包含许多噪音。当我尝试使用tsukuba左右图像进行计算时,它会显示完美而平滑的视差图。为什么会这样?我是否错过了重要的一步?差异范围是否会影响?

enter image description here

1 个答案:

答案 0 :(得分:2)

如果您可以发布已实施的disparity功能,那将会有所帮助。 我建议传递一定数量的差异值e.g. 59 您可以尝试以下方法:

function disparity_map = disparity(I1, I2, disp_value)

shiftedWin = 5 %any odd number
windowSize = (shiftedWin-1)/2;

lImage = im2double(I1)
rImage = im2double(I2)
disp2 = uint8(imread(I1));

[lRow , lCol] = size(lImage);
DispImg = zeros(lRow,lCol);

for i = windowSize+1 :1: lRow-windowSize
    for j = windowSize+1 :1:  lCol-windowSize-disp_value

        previousScore = 9999;
        bestDisparity = 0;

        lReg = lImage(i-windowSize:i+windowSize,j-windowSize:j+windowSize);

        for cDisparity = 1:disp_value

                rReg = rImage(i-windowSize:i+windowSize,j+cDisparity-windowSize:j+windowSize+cDisparity); %% correlated shifted window

                %%SAD method%%
                tempScore = abs(rReg - lReg);
                curScore = sum(tempScore(:));        
                %lineSums = [lineSums, corrScore];

            if (previousScore > curScore)                
                previousScore = curScore;
                bestDisparity = cDisparity;
            end
        end

        DispImg(i,j) = bestDisparity;

    end
end

ilumImg = DispImg *4;
imagesc(ilumImg)
colorbar

diffImg = imabsdiff(uint8(ilumImg),disp2);
figure
imshow(diffImg,[])