W3-图像几何变换


疏漏错误在所难免,有问题请一定向我反馈
疏漏错误在所难免,有问题请一定向我反馈
疏漏错误在所难免,有问题请一定向我反馈


图像放缩

src = imread('tape.png');
S=size(src);
figure;
imshow(src);
title('原图');
k=0.6;
imag1 = imresize(src,[S(1) S(2)]*k);
figure;
imshow(imag1);
title('图像缩小为原来0.6倍');
k1=0.6;
k2=0.4;
imag2=imresize(src,[S(1)*k1 S(2)*k2]);
figure;
imshow(imag2);
title('高度缩小为原来0.6倍,宽度缩小为原来的0.4倍');
k=1.4;
imag3=imresize(src,[S(1) S(2)]*k);
figure;
imshow(imag3);
title('宽度和高度放大为1.4倍的图像');

图像平移

src = imread('lighthouse.png');
figure;
subplot(1,2,1);
imshow(src);
title('原图');
T=translate(strel(1),[300 300]);
subplot(1,2,2);
imag1=imdilate(src,T,'full');
imshow(imag1);
title('显示移动后全部图像');

图像镜像

src = imread('lighthouse.png');
figure;
subplot(2,2,1);
imshow(src);
title('原图');
S=size(src);
T1=maketform('affine',[-1 0 0;0 1 0;S(2) 0 1]);
imag1=imtransform(src,T1);
subplot(2,2,2);
imshow(imag1);
title('垂直镜像');
T2=maketform('affine',[1 0 0;0 -1 0;0 S(1) 1]);
imag2=imtransform(src,T2);
subplot(2,2,3);
imshow(imag2);
title('水平镜像');

图像旋转

src = imread('lighthouse.png');
figure;
subplot(1,2,1);
imshow(src);
title('原图');
S=size(src);
theta=pi/3;
T1=maketform('affine',[cos(theta) -sin(theta) 0;sin(theta) cos(theta) 0;0 0 1]);
imag1=imtransform(src,T1);
subplot(1,2,2);
imshow(imag1);
title('旋转后的图像');

图像绕点旋转

src = imread('lighthouse.png');
figure;
subplot(2,1,1);
imshow(src);
title('原图');
T=translate(strel(1),[-66 -66]);
imag1=imdilate(src,T);
imag2=imdilate(src,T,'full');
theta=pi/3;
T1=maketform('affine',[cos(theta) -sin(theta) 0;sin(theta) cos(theta) 0;0 0 1]);
imag3=imtransform(imag2,T1);
T=translate(strel(1),[66 66]);
imag4=imdilate(imag3,T,'full');
subplot(2,1,2);
imshow(imag4);
title('围绕(66,66)旋转60°图像');


理论上来说写到这就算完成了。



分界线

分界线



下面是迷之操作,遍历像素实现上面变换



嘟嘟嘟嘟嘟嘟嘟嘟嘟嘟嘟嘟嘟嘟嘟嘟嘟嘟嘟

biu biu biu biu biu biu biu biu biu biu biu



图像放缩

function output = myscale(src,w_cof,h_cof)
[h w d] = size(src);
w_cof=w_cof;
h_cof=h_cof;
image = zeros(ceil(h_cof*h),ceil(w_cof*w),d);
[nh nw nd]=size(image);
for y=1:nh
    for x=1:nw
        y0=y*h/nh;
        x0=x*w/nw;
        u=x0-floor(x0);
        v=y0-floor(y0);
        if x0<1  x0=1; end
        if x0>=w x0=w; end
        if y0<1  y0=1; end
        if y0>h  y0=h;end
        for dim=1:d
        image(y,x,dim)=src(floor(y0),floor(x0),dim)*(1-u)*(1-v)+src(floor(y0),ceil(x0),dim)*(1-u)*v+src(ceil(y0),floor(x0),dim)*u*(1-v)+src(ceil(y0),ceil(x0),dim)*u*v;
        end
    end
end
output=uint8(image);
end
src = imread('tape.png');
figure;
imshow(src);
image=myscale(src,1.5,1.5);
figure;
imshow(image);
image1=myscale(src,0.5,0.5);
figure;
imshow(image1);

图像平移

function [output1,output2] = mymove(src,x_dist,y_dist)
[h w dim] = size(src);
h_dist=y_dist;
w_dist=x_dist;

image1=zeros(h,w,dim);
image2=zeros(h+abs(h_dist),w+abs(w_dist),dim);
T_Move=[1 0 w_dist;0 1 h_dist;0 0 1];

if(x_dist < 0)
    x_symbol=-1;
else
    x_symbol=0;
end

if(y_dist < 0)
    y_symbol=-1;
else
    y_symbol=0;
end

for y0=1:h
    for x0=1:w
        cordinate=T_Move*[x0;y0;1];
        if cordinate(2)<1  cordinate(2)=1; end
        if cordinate(2)>h  cordinate(2)=h; end
        if cordinate(1)<1  cordinate(1)=1; end
        if cordinate(1)>w  cordinate(1)=w;end
        for d=1:dim
           image1(floor(cordinate(2)),floor(cordinate(1)),d)=src(y0,x0,d);
        end
        cordinate=T_Move*[x0;y0;1];
        for d=1:dim
        image2(floor(cordinate(2))+y_symbol*y_dist,floor(cordinate(1))+x_symbol*x_dist,d)=src(y0,x0,d);
        end
    end
end

output1=uint8(image1);
output2=uint8(image2);
end

src = imread('lighthouse.png');
figure;
subplot(2,2,1);
imshow(src);
[im1 im2]=mymove(src,200,150);
subplot(2,2,2);
imshow(im1);
subplot(2,2,3);
imshow(im2);

图像镜像

src=imread('lighthouse.png');
[h w dim] = size(src);
image1=zeros(h,w,dim);
image2=zeros(h,w,dim);
T_YMirror=[-1 0 w;0 1 0;0 0 1];
T_XMirror=[1 0 0;0 -1 h;0 0 1];
for y0=1:h
    for x0=1:w
        cordinate=T_YMirror*[x0;y0;1];
        for d=1:dim
        image1(round(cordinate(2)),round(cordinate(1))+1,d)=src(y0,x0,d);
        end
        cordinate=T_XMirror*[x0;y0;1];
         for d=1:dim
        image2(round(cordinate(2))+1,round(cordinate(1)),d)=src(y0,x0,d);
         end
    end
end
figure;
subplot(2,2,1);
imshow(src);
title('原图像');
subplot(2,2,2);
imshow(uint8(image1));
title('水平镜像');
subplot(2,2,3);
imshow(uint8(image2));
title('垂直镜像');

图像旋转

src=imread('lighthouse.png');
[h w dim] = size(src);
theta=pi/6;

T_rorate = [cos(theta) -sin(theta) 0;sin(theta) cos(theta) 0;0 0 1];
image1=zeros(h,w,dim);

for y=1-h/2:h/2
    for x=1-w/2:w/2
        cordinate=T_rorate*[x;y;1];
        cordinate(1)=cordinate(1)+w/2;
        cordinate(2)=cordinate(2)+h/2;
        if(cordinate(2)>=1 && cordinate(2)<=h  && cordinate(1)>=1 && cordinate(1)<=w)
          for d=1:dim
             image1(round(y)+h/2,round(x)+w/2,d)=src(floor(cordinate(2)),floor(cordinate(1)),d);
          end
        end
    end
end

nw=w*cos(theta)+h*sin(theta);
nh=w*sin(theta)+h*cos(theta);
image2=zeros(ceil(nh),ceil(nw),dim);

for y=1-nh/2:nh/2
    for x=1-nw/2:nw/2
        cordinate=T_rorate*[x;y;1];
        cordinate(1)=cordinate(1)+w/2;
        cordinate(2)=cordinate(2)+h/2;
        if(cordinate(2)>=1 && cordinate(2)<=h  && cordinate(1)>=1 && cordinate(1)<=w)
          for d=1:dim
             image2(round(y+nh/2),round(x+nw/2),d)=src(floor(cordinate(2)),floor(cordinate(1)),d);
          end
        end
    end
end

figure;
subplot(2,2,1);
imshow(src);
subplot(2,2,2);
imshow(uint8(image1));
subplot(2,2,3);
imshow(uint8(image2));
             

图像绕点旋转

function [o1,o2] = myrortate(src,angle)

[h w dim] = size(src);
theta=angle;
T_rorate = [cos(theta) -sin(theta) 0;sin(theta) cos(theta) 0;0 0 1];
image1=zeros(h,w,dim);

for y=1-h/2:h/2
    for x=1-w/2:w/2
        cordinate=T_rorate*[x;y;1];
        cordinate(1)=cordinate(1)+w/2;
        cordinate(2)=cordinate(2)+h/2;
        if(cordinate(2)>=1 && cordinate(2)<=h  && cordinate(1)>=1 && cordinate(1)<=w)
          for d=1:dim
             image1(round(y)+h/2,round(x)+w/2,d)=src(floor(cordinate(2)),floor(cordinate(1)),d);
          end
        end
    end
end

nw=w*cos(theta)+h*sin(theta);
nh=w*sin(theta)+h*cos(theta);
image2=zeros(ceil(nh),ceil(nw),dim);

for y=1-nh/2:nh/2
    for x=1-nw/2:nw/2
        cordinate=T_rorate*[x;y;1];
        cordinate(1)=cordinate(1)+w/2;
        cordinate(2)=cordinate(2)+h/2;
        if(cordinate(2)>=1 && cordinate(2)<=h  && cordinate(1)>=1 && cordinate(1)<=w)
          for d=1:dim
             image2(round(y+nh/2),round(x+nw/2),d)=src(floor(cordinate(2)),floor(cordinate(1)),d);
          end
        end
    end
end
o1=image1;
o2=image2;
end
src = imread('lighthouse.png');
figure;
[im1 im2]=mymove(src,-66,-66);
[im3 im4]=myrortate(im2,pi/3);
[im5 im6]=mymove(im4,66,66);
subplot(1,2,1);
imshow(src);
subplot(1,2,2);
imshow(im6);
  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值