zoukankan      html  css  js  c++  java
  • 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

     

    作者: Pam

    出处: https://www.cnblogs.com/pam-sh/>

    关于作者:网安在读

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(mir_soh@163.com)咨询.

  • 相关阅读:
    python3学习之匿名函数
    python3学习之装饰器
    Linux服务器管理神器-IPython
    Linux 安装python3.4
    Linux一些常用操作命令
    Java并发知识分享
    LINUX 学习笔记 账号与群组的管理
    用JAVA写查询一个字符串中是否包含另外一个字符串以及出现的次数
    jQuery性能优化
    jQuery实用工具函数
  • 原文地址:https://www.cnblogs.com/pam-sh/p/14867808.html
Copyright © 2011-2022 走看看