matlab - 使用控制点扭曲图像

标签 matlab image-processing octave homography projective-geometry

我想根据从 here 中提取的方案使用控制点转换图像:

enter image description here

AB 包含源顶点和目标顶点的坐标。

我正在计算转换矩阵:

A = [51 228;  51 127; 191 127; 191 228];
B = [152 57; 219 191;  62 240;  92 109];
X = imread('rectangle.png');
info = imfinfo('rectangle.png');
T = cp2tform(A,B,'projective');

到这里它似乎正常工作,因为(使用归一化坐标)源顶点产生它的目标顶点:

H = T.tdata.T;
> [51 228 1]*H
ans =
  -248.2186   -93.0820    -1.6330
> [51 228 1]*H/ -1.6330
ans =
   152.0016    57.0006     1.0000

问题是 imtransform 产生了意想不到的结果:

Z = imtransform(X,T,'XData',[1 info.Width], 'YData',[1 info.Height]);
imwrite(Z,'projective.png');

Unexpected result

我如何使用 imtransform 来产生我预期的结果?:

enter image description here

是否有其他方法可以实现?

最佳答案

您必须“调整”控制点以适应您正在使用的图像的大小。我这样做的方法是计算 A 中控制点的角与源图像的角之间的仿射变换(最好使这些点按顺时针顺序排列)。

我应该指出的一件事是您的矩阵 A 中点的顺序与您显示的图片不匹配,所以我在下面的代码中修复了它...

这是估计单应性的代码(在 MATLAB 中测试):

% initial control points
A = [51 228;  51 127; 191 127; 191 228];
B = [152 57; 219 191;  62 240;  92 109];
A = circshift(A, [-1 0]);  % fix the order of points to match the picture

% input image
%I = imread('peppers.png');
I = im2uint8(checkerboard(32,5,7));
[h,w,~] = size(I);

% adapt control points to image size
% (basically we estimate an affine transform from 3 corner points)
aff = cp2tform(A(1:3,:), [1 1; w 1; w h], 'affine');
A = tformfwd(aff, A);
B = tformfwd(aff, B);

% estimate homography between A and B
T = cp2tform(B, A, 'projective');
T = fliptform(T);
H = T.tdata.Tinv

我得到:

>> H
H =
   -0.3268    0.6419   -0.0015
   -0.4871    0.4667    0.0009
  324.0851 -221.0565    1.0000

现在让我们想象一下这些点:

% check by transforming A points into B
%{
BB = [A ones(size(A,1),1)] * H;        % convert to homogeneous coords
BB = bsxfun(@rdivide, BB, BB(:,end));  % convert from homogeneous coords
%}
BB = tformfwd(T, A(:,1), A(:,2));
fprintf('error = %g\n', norm(B-BB));

% visually check by plotting control points and transformed A
figure(1)
subplot(121)
plot(A([1:end 1],1), A([1:end 1],2), '.-', 'MarkerSize',20, 'LineWidth',2)
line(BB([1:end 1],1), BB([1:end 1],2), 'Color','r', 'Marker','o')
text(A(:,1), A(:,2), num2str((1:4)','a%d'), ...
    'VerticalAlign','top', 'HorizontalAlign','left')
title('A'); legend({'A', 'A*H'}); axis equal ij
subplot(122)
plot(B([1:end 1],1), B([1:end 1],2), '.-', 'MarkerSize',20, 'LineWidth',2)
text(B(:,1), B(:,2), num2str((1:4)','b%d'), ...
    'VerticalAlign','top', 'HorizontalAlign','left')
title('B'); legend('B'); axis equal ij

control_points

最后我们可以对源图像应用转换:

% transform input image and show result
J = imtransform(I, T);
figure(2)
subplot(121), imshow(I), title('image')
subplot(122), imshow(J), title('warped')

warped_image

关于matlab - 使用控制点扭曲图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32266408/

相关文章:

matlab - 如何在 2D 中绘制网格?

octave - Octave 中有占位符变量吗?

c++ - 使用 sge 集群在 Matlab 下运行 C++ 代码?

MATLAB 函数语法

matlab - 如何在 Matlab 中绘制二维 FFT?

r - 使用 Magick(在 R 中)通过转换处理多个图像

python - 是否可以在 Octave 中使用 Python 模块?

matlab xlsread函数

java - 在 MATLAB 的 com.mathworks 内部获得帮助

python - 仅测量两个图像之间沿一个方向的位移