matlib:图像旋转-缩放

Posted Pam

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlib:图像旋转-缩放相关的知识,希望对你有一定的参考价值。

需求

使用MATLAB尝试完成一个自定义的图像攻击软件,功能描述:
1)根据输入参数,完成旋转功能
2)根据输入参数,完成缩放功能

开始

旋转

参数:参数为正,顺时针旋转;参数为负,逆时针旋转

主要代码:

%自定义旋转函数
function [newimage]=rotate(img,degree)
%获取图片信息 注意三通道获取完 即定义三个变量
[m,n,dep]=size(img);
%计算出旋转之后,形成一个大矩形的长宽 可以看效果图
rm=round(m*abs(cosd(degree))+n*abs(sind(degree)));
rn=round(m*abs(sind(degree))+n*abs(cosd(degree)));
%定义一个新矩阵,三通道的,存储新图片的信息
newimage=zeros(rm,rn,dep);
%坐标变换 分三步 
m1=[1,0,0;0,1,0;-0.5*rm,-0.5*rn,1];
m2=[cosd(degree),sind(degree),0;-sind(degree),cosd(degree),0;0,0,1];
m3=[1,0,0;0,1,0;0.5*m,0.5*n,1];
%利用循环,对每一个像素点进行变换
for i=1:rm
    for j=1:rn
        tem=[i j 1];
        tem=tem*m1*m2*m3;
        x=tem(1,1);
        y=tem(1,2);
        x=round(x);
        y=round(y);
        if(x>0&&x<=m)&&(y>0&&y<=n)
        newimage(i,j,:)=img(x,y,:);
        end
        end
        end
% --- Executes on button press in pushbutton2.旋转
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% in_image=[handles.I,handles.map];
% t=imread();
a=str2double(get(handles.edit1,\'String\'));
global t1;
t1=rotate(handles.I,a);
% guidata(hObject,handles);
axes(handles.axes2);
imshow(uint8(t1));%显示图片
%imwrite(uint8(t1),\'roate_.bmp\');
% --- Executes on button press in pushbutton5.旋转保存
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global t1;
[FileName,PathName] = 
uiputfile({\'*.bmp\',\'Bitmap(*.bmp)\';\'*.jpg\',\'JPEG(*.jpg)\';...
                                             \'*.gif\',\'GIF(*.gif)\';...
                                             \'*.*\',  \'All Files (*.*)\'},...
                                             \'Save Picture\',\'Untitled\');
if FileName==0
      disp(\'保存失败\');
      return;
else
      %h=getframe(picture);%picture是GUI界面绘图的坐标系句柄
      imwrite(uint8(t1),[PathName,FileName]);
      %imwrite(uint8(t1),\'roate_.bmp\');
end

结果:

缩放

参数:参数大于1,放大图像;小于1,则缩小图像
主要代码:

% --- Executes on button press in pushbutton3.缩放
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
a=str2double(get(handles.edit1,\'String\'));
global output;
output=imresize(handles.I,a);
axes(handles.axes2);
imshow(output);%显示图片
%imwrite(output,\'scale_.bmp\');
% --- Executes on button press in pushbutton6.缩放保存
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global output;
[FileName,PathName] = 
uiputfile({\'*.bmp\',\'Bitmap(*.bmp)\';\'*.jpg\',\'JPEG(*.jpg)\';...
                                             \'*.gif\',\'GIF(*.gif)\';...
                                             \'*.*\',  \'All Files (*.*)\'},...
                                             \'Save Picture\',\'Untitled\');
if FileName==0
      disp(\'保存失败\');
      return;
else
      %h=getframe(picture);%picture是GUI界面绘图的坐标系句柄
      imwrite(output,[PathName,FileName]);
      %imwrite(uint8(t1),\'roate_.bmp\');
end 

结果:

源码见Github

 

以上是关于matlib:图像旋转-缩放的主要内容,如果未能解决你的问题,请参考以下文章

html5图像旋转缩放并上传

AS3 图像在内部旋转/缩放以适合精灵

如何在Android中对图像应用缩放、拖动和旋转[关闭]

带有提升缩放功能的光滑旋转木马

Python图像处理丨图像缩放旋转翻转与图像平移

图像识别入门3 图像的缩放和旋转