matlab旋转实现(最近邻值,双线性,三次卷积插值实现插值)

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

对图像进行旋转,使用最近邻插值法,双线性插值,三次卷积插值三种方法进行插值。

源码:

clc;clear all;close all;

Img=imread('test1.bmp');

Img=double(Img);

[h w]=size(Img);

alpha=pi/6; %逆时针旋转的角度

wnew=w*cos(alpha)+h*sin(alpha); %新图像的宽width

hnew=w*sin(alpha)+h*cos(alpha); %新图像的高heighth

wnew=ceil(wnew); %取整

hnew=ceil(hnew);

u0=w*sin(alpha); %平移量

T=[cos(alpha),sin(alpha);-sin(alpha),cos(alpha)]; %变换矩阵

Imgnew1=zeros(hnew,wnew);

Imgnew2=zeros(hnew,wnew);

Imgnew3=zeros(hnew,wnew);

for u=1:hnew %u和v是新图像坐标,变换到原图像坐标x和y中。

for v=1:wnew

tem=T*([u;v]-[u0;0]);

x=tem(1);

y=tem(2);

if x>=1 & x<=h & y>=1 & y<=w %若变换出的x和y在原图像范围内

x_low=floor(x);

x_up=ceil(x);

y_low=floor(y);

y_up=ceil(y);

if (x-x_low)<=(x_up-x) %采用最近点法,选取距离最近点的像素赋给新图像x=x_low;

else

x=x_up;

end

if (y-y_low)<=(y_up-y)

y=y_low;

else

y=y_up;

end

p1=Img(x_low,y_low); %双线性插值,p1到p4是(x,y)周围的四个点p2=Img(x_up,y_low);

p3=Img(x_low,y_low);

p4=Img(x_up,y_up);

s=x-x_low;

t=y-y_low;

Imgnew1(u,v)=Img(x,y);

Imgnew2(u,v)=(1-s)*(1-t)*p1+(1-s)*t*p3+(1-t)*s*p2+s*t*p4;

end

if x>=2 & x<=h-2 & y>=2 & y<=w-2 %若变换出的x和y在原图像范围内x_1=floor(x)-1;

x_2=floor(x);

x_3=floor(x)+1;

x_4=floor(x)+2;

y_1=floor(y)-1;

y_2=floor(y);

y_3=floor(y)+1;

y_4=floor(y)+2;

A=[sw(1+x-x_2),sw(x-x_2),sw(1-(x-x_2)),sw(2-(x-x_2))];

C=[sw(1+y-y_2),sw(y-y_2),sw(1-(y-y_2)),sw(2-(y-y_2))];

B=[ Img(x_1,y_1),Img(x_1,y_2),Img(x_1,y_3),Img(x_1,y_4);

Img(x_2,y_1),Img(x_2,y_2),Img(x_2,y_3),Img(x_2,y_4);

Img(x_3,y_1),Img(x_3,y_2),Img(x_3,y_3),Img(x_3,y_4);

Img(x_4,y_1),Img(x_4,y_2),Img(x_4,y_3),Img(x_4,y_4)];

Imgnew3(u,v)=A*B*C';

end

end

end

subplot(2,2,1),imshow(Img,[]),title('原图');

subplot(2,2,2),imshow(Imgnew1,[]),title('最近邻插值法'); subplot(2,2,3),imshow(Imgnew2,[]),title('双线性插值法'); subplot(2,2,4),imshow(Imgnew3,[]),title('三次卷积插值法');

相关文档
最新文档