如何在MATLAB中对图像进行水平平移?

6
我有一张图片,已经转换为双倍矩阵。我想要水平移动它,但是我不确定该如何操作。我尝试去改变位置,但是结果只是改变了颜色。
我是否可以通过引用像素位置来添加一个常数以执行移位呢?
4个回答

8

使用图像处理工具箱,您可以应用空间变换

img = imread('pout.tif');
T = maketform('affine', [1 0 0; 0 1 0; 50 100 1]);   %# represents translation
img2 = imtransform(img, T, ...
    'XData',[1 size(img,2)], 'YData',[1 size(img,1)]);
subplot(121), imshow(img), axis on
subplot(122), imshow(img2), axis on

affine translation


2
Amro,[1 0 0; 0 1 0; 50 100 1]代表什么?这是一个变换矩阵吗?它是按像素还是其他度量单位移动图像? - user2192778
@user2192778:它代表一个仅包含平移分量的仿射变换矩阵:http://www.mathworks.com/help/images/performing-general-2-d-spatial-transformations.html#f12-31782。它是以默认空间坐标系表示的;有关详细说明,请参见此页面:http://www.mathworks.com/help/images/image-coordinate-systems.html。 - Amro

5
您可以使用circshift进行循环移位,im = circshift(im, [vShift hShift])

非整数版本(我猜)也可以通过取最近的两个移位并进行插值来实现? - jeff

2

假设你的图像是矩阵A,并且想要将x列向左包装:

A = [A(x+1:end,:) A(1:x,:)];

0

如果您没有工具箱,但仍想进行亚像素移位,则可以使用此函数。它会生成一个在x和y方向上单个像素移位的滤波器,并将该滤波器应用于图像。

function [IM] = shiftIM(IM,shift)
S = max(ceil(abs(shift(:))));
S=S*2+3;
filt = zeros(S);
shift = shift + (S+1)/2;
filt(floor(shift(1)),floor(shift(2)))=(1-rem(shift(1),1))*(1-rem(shift(2),1));
filt(floor(shift(1))+1,floor(shift(2)))=rem(shift(1),1)*(1-rem(shift(2),1));
filt(floor(shift(1)),floor(shift(2))+1)=(1-rem(shift(1),1))*rem(shift(2),1);
filt(floor(shift(1))+1,floor(shift(2)+1))=rem(shift(1),1)*rem(shift(2),1);
IM=conv2(IM, filt, 'same');
end

%to test
IM = imread('peppers.png');IM = mean(double(IM),3);
for ct = 0:0.2:10
    imagesc(shiftIM(IM,[ct/3,ct]))
    pause(0.2)
end
%it should shift to the right in x and y

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接