Matlab中的图像校正算法

时间:2014-09-09 13:24:52

标签: matlab computer-vision matlab-cvst stereo-3d stereoscopy

我最近发现了一篇关于两个立体图像对的图像校正的有趣文章。我喜欢这个算法,因为它非常紧凑,而且根据文章的建议它做了正确的事情。在两张图像上实现了matlab版本后,我没有得到正确的校正图像。我得到的图像是黑色的,左边和下面的线都有像素。在图像中,原始图像中也有一些灰色像素,但只有一只手满了。我发布了下面的matlab代码,以及文章的链接,以及我为一个图像得到的结果示例(对于另一个图像,它是相同的)

这是文章A compact algorithm for rectification of stereo pairs的链接。

使用初始图像和结果的屏幕截图如下: screen shot

初始图像是以下两个(这样您不必搜索另一个立体声对):stereo image 1 here stereo image two here

function [T1,T2,Pn1,Pn2] = rectify(Po1,Po2)

% RECTIFY: compute rectification matrices 

% factorize old PPMs
[A1,R1,t1] = art(Po1);
[A2,R2,t2] = art(Po2);

 % optical centers (unchanged)
c1 = - inv(Po1(:,1:3))*Po1(:,4);
c2 = - inv(Po2(:,1:3))*Po2(:,4);

% new x axis (= direction of the baseline)
 v1 = (c1-c2);
% new y axes (orthogonal to new x and old z)
v2 = cross(R1(3,:)',v1);
% new z axes (orthogonal to baseline and y)
v3 = cross(v1,v2);

% new extrinsic parameters 
R = [v1'/norm(v1)
   v2'/norm(v2)
   v3'/norm(v3)];
% translation is left unchanged

% new intrinsic parameters (arbitrary) 
A = (A1 + A2)./2;
A(1,2)=0; % no skew
A(1,3) = A(1,3) + 160;
% new projection matrices
Pn1 = A * [R -R*c1 ];
Pn2 = A * [R -R*c2 ];

% rectifying image transformation
T1 = Pn1(1:3,1:3)* inv(Po1(1:3,1:3));
T2 = Pn2(1:3,1:3)* inv(Po2(1:3,1:3));

function [A,R,t] = art(P)
% ART: factorize a PPM as  P=A*[R;t]
Q = inv(P(1:3, 1:3));
[U,B] = qr(Q);

R = inv(U);
t = B*P(1:3,4);
A = inv(B);
A = A ./A(3,3);

这是我称之为矫正功能的“主要”代码

img1 = imread('D:\imag1.png');
img2 = imread('D:\imag2.png');

im1 = rgb2gray(img1);
im2 = rgb2gray(img2);

im1 = im2double(im1);
im2 = im2double(im2);

figure; imshow(im1, 'border', 'tight')
figure; imshow(im2, 'border', 'tight')

%pair projection matrices obtained after the calibration P01,P02

a = double(9.765*(10^2))
b = double(5.790*(10^-1))
format bank;
Po1 = double([a 5.382*10 -2.398*(10^2) 3.875*(10^5); 
    9.849*10 9.333*(10^2) 1.574*(10^2) 2.428*(10^5);
    b 1.108*(10^(-1)) 8.077*(10^(-1)) 1.118*(10^3)]);
Po2 = [9.767*(10^2) 5.376*10 -2.400*(10^2) 4.003*(10^4);
    9.868*10 9.310*(10^2) 1.567*(10^2) 2.517*(10^5);
    5.766*(10^(-1)) 1.141*(10^(-1)) 8.089*(10^(-1)) 1.174*(10^3)];
[T1, T2, Pn1, Pn2] = rectify(Po1, Po2);

imnoua =  conv2(im1, T1);
imnoua2 = conv2(im2, T2);

fprintf('Imaginea noua e \n');

figure; imshow(imnoua, 'border', 'tight')
figure; imshow(imnoua2, 'border', 'tight')

感谢您的时间!

1 个答案:

答案 0 :(得分:2)

作为Shai saysT1T2是投影转换矩阵,而不是滤镜内核。您应该使用imwarp,而不是conv2

imnoua = imwarp(im1, projective2d(T1));
imnoua2 = imwarp(im2, projective2d(T2));

更好的是,使用计算机视觉系统工具箱中的rectifyStereoImages。看看这个example