matlab练习程序(图像水平/竖直移动)

cl;
delta_x=67; %要求为整数,水平移动的偏移量,正为向右,负为向左
delta_y=-89; %要求为整数,竖直移动的偏移量,正为向下,负为向上
img=imread('Corner.png'); %这里v为原图像的高度,u为原图像的宽度
imshow(img); %这里y为变换后图像的高度,x为变换后图像的宽度
[v u]=size(img);

imgn=zeros(v+abs(delta_y),u+abs(delta_x));
rot=[1 0 0;0 1 0;delta_y delta_x 1];
inv_rot=inv(rot);
pix1=[1 1 1]*rot; %pix1(1),pix1(2)分别为变换后图像的左上角的y,x
pix4=[v u 1]*rot; %pix4(1),pix4(2)分别为变换后图像的右下角的y,x

%%向右下方移动
if delta_x>=0 && delta_y>=0
for y=pix1(1):pix4(1)
for x=pix1(2):pix4(2)
pix=[y x 1]*inv_rot;
if pix(1)>=0.5 && pix(2)>=0.5 && pix(1)<=v && pix(2)<=u
imgn(y,x)=img(round(pix(1)),round(pix(2)));
end
end
end
end

%%向左下方移动
if delta_x<0 && delta_y>=0
for y=pix1(1):pix4(1)
for x=pix1(2):pix4(2)
pix=[y x 1]*inv_rot;
if pix(1)>=0.5 && pix(2)>=0.5 && pix(1)<=v && pix(2)<=u
imgn(y,x-delta_x)=img(round(pix(1)),round(pix(2)));
end
end
end
end

%%向左上方移动
if delta_x<0 && delta_y<0
for y=pix1(1):pix4(1)
for x=pix1(2):pix4(2)
pix=[y x 1]*inv_rot;
if pix(1)>=0.5 && pix(2)>=0.5 && pix(1)<=v && pix(2)<=u
imgn(y-delta_y,x-delta_x)=img(round(pix(1)),round(pix(2)));
end
end
end
end

%%向右上方移动
if delta_x>=0 && delta_y<0
for y=pix1(1):pix4(1)
for x=pix1(2):pix4(2)
pix=[y x 1]*inv_rot;
if pix(1)>=0.5 && pix(2)>=0.5 && pix(1)<=v && pix(2)<=u
imgn(y-delta_y,x)=img(round(pix(1)),round(pix(2)));
end
end
end
end

figure,imshow(uint8(imgn))
posted @ 2012-03-08 14:00  Dsp Tian  阅读(4130)  评论(0编辑  收藏  举报