需求
使用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!