當前位置:編程學習大全網 - 編程語言 - 怎麽用MATLAB掌握圖像平移、圖像旋轉和圖像縮放的基本原理與實現方法

怎麽用MATLAB掌握圖像平移、圖像旋轉和圖像縮放的基本原理與實現方法

%圖像平移(1)

F=imread('p2.bmp');

se = translate(strel(1), [0 20]);

%參數[0 20]可以修改,修改後平移距離對應改變

J = imdilate(F,se);

figure;

imshow(J,[]);title('右移後圖形');

%圖像平移(2)

function outImage=immove(inImage,Tx,Ty)

[m, n] = size(inImage);

Tx=fix(Tx);

Ty=fix(Ty);

%move x

if (Tx<0)

inImage=imcrop(inImage,[abs(Tx),1,m-abs(Tx),n]);

[m, n] = size(inImage);

Tx=0;

end

%move y

if (Ty<0)

inImage=imcrop(inImage,[1,abs(Ty),m,n-abs(Ty)]);

[m, n] = size(inImage);

Ty=0;

end

outImage = zeros(m+Ty, n+Tx);

outImage(1+Ty:m+Ty,1+Tx:n+Tx) = inImage;

%圖像旋轉

%X,Y為其行列數

Image=imread('02.jpg');

Image=rgb2gray(Image);

angle=30;

%角度任意的壹個數 表示30度

pai=3.14;

Angle=pai*angle/180;

%轉換壹下角度的表示方法。

[X,Y]=size(Image);

%原圖顯示

subplot(2,1,1);

imshow(Image);

title('原圖像');

%計算四個角點的新坐標,確定旋轉後的顯示區域

LeftTop(1,1)=-(Y-1)*sin(Angle);

LeftTop(1,2)=(Y-1)*cos(Angle);

LeftBottom(1,1)=0;

LeftBottom(1,2)=0;

RightTop(1,1)=(X-1)*cos(Angle)-(Y-1)*sin(Angle);

RightTop(1,2)=(X-1)*sin(Angle)+(Y-1)*cos(Angle);

RightBottom(1,1)=(X-1)*cos(Angle);

RightBottom(1,2)=(X-1)*sin(Angle);

%計算顯示區域的行列數

Xnew=max([LeftTop(1,1),LeftBottom(1,1),RightTop(1,1),RightBottom(1,1)])-min([LeftTop(1,1),LeftBottom(1,1),RightTop(1,1),RightBottom(1,1)]);

Ynew=max([LeftTop(1,2),LeftBottom(1,2),RightTop(1,2),RightBottom(1,2)])-min([LeftTop(1,2),LeftBottom(1,2),RightTop(1,2),RightBottom(1,2)]);

% 分配新顯示區域矩陣

ImageNew=zeros(round(Xnew),round(Ynew))+255;

%計算原圖像各像素的新坐標

for indexX=0:(X-1)

for indexY=0:(Y-1)

ImageNew(round(indexX*cos(Angle)-indexY*sin(Angle))+round(abs(min([LeftTop(1,1),LeftBottom(1,1),RightTop(1,1),RightBottom(1,1)])))+1,1+round(indexX*sin(Angle)+indexY*cos(Angle))+round(abs(min([LeftTop(1,2),LeftBottom(1,2),RightTop(1,2),RightBottom(1,2)]))))=Image(indexX+1,indexY+1);

end

end

%顯示

subplot(2,1,2);

imshow((ImageNew)/255)

promp=['旋轉角度為:' int2str(angle) '的圖像']

title(promp);

%圖像縮放

function y=resize(a,mul,type)

%****************************************************

%a:輸入圖像灰度值

%mul:縮放倍數

%type:1表示最鄰近法,2表示雙極性插值法

%畫出縮放後圖像並返回其灰度值

%****************************************************

[m,n]=size(a);

m1=m*mul;n1=n*mul;

%****************************************************

if type==1

for i=1:m1

for j=1:n1;

b(i,j)=a(round(i/mul),round(j/mul));

end

end

elseif type==2

for i=1:m1-1

for j=1:n1-1;

u0=i/mul;v0=j/mul;

u=round(u0);v=round(v0);

s=u0-u;t=v0-v;

b(i,j)=(a(u+1,v)-a(u,v))*s+(a(u,v+1)-a(u,v))*t+(a(u+1,v+1)+a(u,v)-a(u,v+1)-a(u+1,v))*s*t+a(u,v);

end

end

end

%*****************************************************

b=uint8(b);

imshow(b);

title('處理後圖像');

y=b;

  • 上一篇:計算機機器語言
  • 下一篇:桃核制作手串的全部過程
  • copyright 2024編程學習大全網