實現圖形旋轉程序!matlab語言!

function rotate(Image,Angle)
%Image為位圖數據


%X,Y為其行列數
[X,Y]=size(Image);


%原圖顯示
imshow(Image);


%計算四個角點的新坐標,確定旋轉后的顯示區域
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


%顯示
figure;
imshow((ImageNew)/255)


登錄后免費查看全文
立即登錄
App下載
技術鄰APP
工程師必備
  • 項目客服
  • 培訓客服
  • 平臺客服

TOP

3