常见的数字图像的几何变换包括平移、镜像、旋转、缩放和错切等,是图像处理软件必须具备的操作手段。本次实验的目的是要求掌握各种变换的基本原理和方法,能够运用Matlab编写程序,实现这些变换。其中,由于变化产生的空穴、马赛克和锯齿现象,希望能够采用适当的插值算法,消除它们的影响,改善图像质量
I=imread('****.jpg');
dltI=20;
dltJ=30
[m,n]=size(I);
G=uint8(zeros(m,n));%没有扩大画布
for i=1:m
for j=1:n
if(((i-dltI>0) && (i-dltI<=m)) && ((j-dltJ>0 )&&( j-dltJ<=n)))
G(i,j) = I(i-dltI,j-dltJ);
end
end
end
Ge=uint8(zeros(m+dltI,n+dltJ));%扩大画布
for i=1:(m+dltI)
for j=1:(n+dltJ)
if(((i-dltI>0) && (i-dltI<=m)) && ((j-dltJ>0 )&&( j-dltJ<=n)))
Ge(i,j) = I(i-dltI,j-dltJ);
end
end
end
subplot(1,3,1);imshow(I); title("原图像");
subplot(1,3,2);imshow(G); title("无扩大画布平移图像");
subplot(1,3,3);imshow(Ge); title("扩大画布平移图像");
I=imread('gull.jpg');
[m,n]=size(I);
Ih=uint8(zeros(m,n));%水平镜像
for i=1:m
for j=1:n
Ih(i,m-j+1)=I(i,j);
end
end
Iv=uint8(zeros(m,n));%垂直镜像
for i=1:m
for j=1:n
Iv(m-i+1,j)=I(i,j);
end
end
subplot(1,3,1);imshow(I); title("原图像");
subplot(1,3,2);imshow(Ih); title("水平镜像图像");
subplot(1,3,3);imshow(Iv); title("垂直镜像图像");
F=imread('gull.jpg');
[m,n]=size(F);
I=zeros(m,n);J=zeros(m,n); %坐标矩阵
for i=1:m
I(i,:)=i;
end
for j=1:n
J(:,j)=j;
end
II=I.*cos(pi/6)-J.*sin(pi/6);%逆时针旋转30度
JJ=I.*sin(pi/6)+J.*cos(pi/6);
II=int16(II);JJ=int16(JJ); %取整
mini=min(min(II));%计算画布大小
maxi=max(max(II));
II = II+(1-mini);
minj=min(min(JJ));
maxj=max(max(JJ));
JJ = JJ+(1-minj);
maxII=max(max(II));
maxJJ=max(max(JJ));
G=uint8(zeros(maxII,maxJJ));%生成新图像
for i=1:m
for j=1:n
G(II(i,j),JJ(i,j)) = F(i,j);
end
end
subplot(1,2,1);imshow(F);
subplot(1,2,2);imshow(G);
F=imread('gull.jpg');
[m,n]=size(F);
I=zeros(m,n);J=zeros(m,n); %坐标矩阵
for i=1:m
I(i,:)=i;
end
for j=1:n
J(:,j)=j;
end
II=I.*cos(pi/6)-J.*sin(pi/6);%逆时针旋转30度
JJ=I.*sin(pi/6)+J.*cos(pi/6);
II=int16(II);JJ=int16(JJ); %取整
%计算画布大小
mini=min(min(II));
maxi=max(max(II));
II = II+(1-mini);
minj=min(min(JJ));
maxj=max(max(JJ));
JJ = JJ+(1-minj);
maxII=max(max(II));
maxJJ=max(max(JJ));
%生成新图像
G=uint8(zeros(maxII,maxJJ));
for i=1:m
for j=1:n
G(II(i,j),JJ(i,j)) = F(i,j);
end
end
%用最近邻插值法消除空穴
Gr=G;
[gm,gn]=size(Gr);
for i=1:gm
for j=1:gn
gi=(i-(1-mini));
gj=(j-(1-minj));
invi=int16(gi*cos(-pi/6)-gj*sin(-pi/6)); %逆时针旋转30度
invj=int16(gi*sin(-pi/6)+gj*cos(-pi/6));
%判断是否为背景
if(invi>0 && invi<=m && invj>0 && invj<=n)
if(Gr(i,j)==0)
if(j
F=imread('gull.jpg');
[mf,nf]=size(F);
%按比例缩小
dlt=2;
G1=uint8(zeros(int16(mf/dlt),int16(nf/dlt)));
[mg1,ng1]=size(G1);
for i=1:mg1
for j=1:ng1
fi=int16(dlt*i);
fj=int16(dlt*j);
if(fi>mf) fi=mf;end
if(fj>nf) fj=nf;end
G1(i,j)=F(fi,fj);
end
end
figure;imshow(F);
figure;imshow(G1);
%不按比例缩小
dlti=2;
dltj=3;
G2=uint8(zeros(int16(mf/dlti),int16(nf/dltj)));
[mg2,ng2]=size(G2);
for i=1:mg2
for j=1:ng2
fi=int16(dlti*i);
fj=int16(dltj*j);
if(fi>mf) fi=mf;end
if(fj>nf) fj=nf;end
G2(i,j)=F(fi,fj);
end
end
figure;imshow(G2);
F=imread('gull.jpg');
[mf,nf]=size(F);
dlt=0.5;
G1=uint8(zeros(int16(mf/dlt),int16(nf/dlt)));
[mg1,ng1]=size(G1);
for i=1:mg1
for j=1:ng1
fi=int16(dlt*i);
fj=int16(dlt*j);
if(fi>mf)
fi=mf;
end
if(fj>nf)
fj=nf;
end
G1(i,j)=F(fi,fj);
end
end
figure;imshow(F);
figure;imshow(G1);
dlti=5/6;
dltj=0.4;
G2=uint8(zeros(int16(mf/dlti),int16(nf/dltj)));
[mg2,ng2]=size(G2);
for i=1:mg2
for j=1:ng2
fi=int16(dlti*i);
fj=int16(dltj*j);
if(fi<1) fi=1;end
if(fj<1) fj=1;end
if(fi>mf) fi=mf;end
if(fj>nf) fj=nf;end
G2(i,j)=F(fi,fj);
end
end
figure;imshow(G2);
clear;
F=[1 4 7;2 5 8;3 6 9];
[m,n]=size(F);
G=zeros(4,8);
for i=1:4
x=i/1.2;
fi=int8(floor(x));
for j=1:8
y=j/2.5;
fj=int8(floor(y));
if(fi<1 && fj<1) A=double([F(1,1),F(1,1);F(1,1),F(1,1)]);end
if(fi<1 && fj>=1 && fj=n) A=double([F(1,n),F(1,n);F(1,n),F(1,n)]);end
if(fi=1 && fj<1) A=double([F(fi,1),F(fi,1);F(fi+1,1),F(fi+1,1)]);end
if(fi=1 && fj>=1 && fj=1 && fj>=n) A=double([F(fi,n),F(fi,n);F(fi+1,n),F(fi+1,n)]);end
if(fi>=m && fj<1) A=double([F(m,1),F(m,1);F(m,1),F(m,1)]);end
if(fi>=m && fj>=1 && fj=m && fj>=n) A=double([F(m,n),F(m,n);F(m,n),F(m,n)]);end
fi=double(fi);fj=double(fj);
wx=[abs(1-abs(x-fi)),abs(1-abs(1+fi-x))];
wy=[abs(1-abs(y-fj));abs(1-abs(1+fj-y))];
G(i,j)=int8(wx*A*wy);
end
end
F=imread('gull.jpg');
[mf,nf]=size(F);
%不按比例放大
dlti=5/6;
dltj=0.4;
G=uint8(zeros(int16(mf/dlti),int16(nf/dltj)));
[mg,ng]=size(G);
for i=1:mg
x=i*dlti;
fi=floor(x);
for j=1:ng
y=j*dltj;
fj=floor(y);
if(fi<1 && fj<1) A=double([F(1,1),F(1,1);F(1,1),F(1,1)]);end
if(fi<1 && fj>=1 && fj=nf) A=double([F(1,nf),F(1,nf);F(1,nf),F(1,nf)]);end
if(fi=1 && fj<1) A=double([F(fi,1),F(fi,1);F(fi+1,1),F(fi+1,1)]);end
if(fi=1 && fj>=1 && fj=1 && fj>=nf) A=double([F(fi,nf),F(fi,nf);F(fi+1,nf),F(fi+1,nf)]);end
if(fi>=mf && fj<1) A=double([F(mf,1),F(mf,1);F(mf,1),F(mf,1)]);end
if(fi>=mf && fj>=1 && fj=mf && fj>=nf) A=double([F(mf,nf),F(m,n);F(mf,nf),F(mf,nf)]);end
fi=double(fi);fj=double(fj);
wx=[abs(1-abs(x-fi)),abs(1-abs(1+fi-x))];
wy=[abs(1-abs(y-fj));abs(1-abs(1+fj-y))];
G(i,j)=uint8(wx*A*wy);
end
end
figure;imshow(F);
figure;imshow(G);
clear;
F=imread('gull.jpg');
[mf,nf]=size(F);
angle = 10*pi/180;
%水平错切
Gh=uint8(zeros(int16(mf+nf*tan(angle)),nf));
for i=1:mf
for j=1:nf
Gh(int16(i+j*tan(angle)),j) = F(i,j);
end
end
%垂直错切
Gv=uint8(zeros(mf,int16(nf+mf*tan(angle))));
for i=1:mf
for j=1:nf
Gv(i,int16(j+i*tan(angle))) = F(i,j);
end
end
figure;imshow(F);
figure;imshow(Gh);
figure;imshow(Gv);
代码文件可以从主页发布里面找
万水千山都是情,路过点个赞行不行!
希望看见的都可以帮忙点个赞!拜托了!
觉得还可以的动动手指帮忙点个赞!呜呜~