matlib:图像旋转-缩放
需求#
使用MATLAB尝试完成一个自定义的图像攻击软件,功能描述:
1)根据输入参数,完成旋转功能
2)根据输入参数,完成缩放功能
开始#
旋转#
参数:参数为正,顺时针旋转;参数为负,逆时针旋转
主要代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | %自定义旋转函数 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,则缩小图像
主要代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | % --- 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!
作者:Hang Shao
出处:https://www.cnblogs.com/pam-sh/p/14867808.html
版权:本作品采用「知识共享」许可协议进行许可。
声明:欢迎交流! 原文链接 ,如有问题,可邮件(mir_soh@163.com)咨询.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2020-06-09 linux:shell