(二)matlab数字图像处理实验-图像的几何变换

数字图像处理实验1-9点击下列链接有源码和链接:


上一节已经简单介绍guide的使用,我们直接用,创建了这么一个界面:


1、打开图片代码:

% --- Executes on button press in openFile.
function openFile_Callback(hObject, eventdata, handles)
[filename,pathname]=uigetfile({'*.jpg'},'选择图片');%文件选择
str=[pathname filename];%被选择的文件路径
[handles.I,handles.map]=imread(str);%读取图片
in_image=[handles.I,handles.map];
handles.simage=handles.I;
guidata(hObject,handles);%图像串行化
axes(handles.axessmall);%把显示范围限定在axessmall
imshow(in_image);%显示图片
axes(handles.axesbig);%把显示范围限定在axesbig
imshow(in_image);%显示图片
2、重置图片的代码:

% --- Executes on button press in reset_photo.
function reset_photo_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
handles.I=handles.simage;
guidata(hObject,handles);%图像串行化

3、上下左右平移的代码:

(1)我们先写一个move函数:

%move平移函数,I为图像,a和b组成一个平移向量
function J=move(I,a,b)
[M,N,G]=size(I);
I=im2double(I);
J=ones(M,N,G);
for i=1:M
    for j=1:N
        if((i+a)>=1&&(i+a)<=M&&(j+b)>=1&&(j+b)<=N);%这里可以写判断图像超出范围的代码
           J(i+a,j+b,:)=I(i,j,:);
        end
    end
end
(2)使用move函数

% --- Executes on button press in 向上平移.
function translationup_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
in_image=[handles.I,handles.map];
in_image=move(in_image,-10,0);
handles.I=in_image;
guidata(hObject,handles);%图像串行化
axes(handles.axesbig);%把显示范围限定在axesbig
imshow(in_image);%显示图片

% --- Executes on button press in 向下平移.
function translationdown_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
in_image=[handles.I,handles.map];
in_image=move(in_image,10,0);
handles.I=in_image;
guidata(hObject,handles);%图像串行化
axes(handles.axesbig);%把显示范围限定在axesbig
imshow(in_image);%显示图片


% --- Executes on button press in 向左平移.
function translationleft_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
in_image=[handles.I,handles.map];
in_image=move(in_image,0,-10);
handles.I=in_image;
guidata(hObject,handles);%图像串行化
axes(handles.axesbig);%把显示范围限定在axesbig
imshow(in_image);%显示图片


% --- Executes on button press in 向右平移.
function translationright_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
in_image=[handles.I,handles.map];
in_image=move(in_image,0,10);
handles.I=in_image;
guidata(hObject,handles);%图像串行化
axes(handles.axesbig);%把显示范围限定在axesbig
imshow(in_image);%显示图片

4、放大缩小代码: 通过imresize()函数对图像进行镜像转换

% --- Executes on button press in 放大.
function enlarge_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
in_image=[handles.I,handles.map];
in_image2=imresize(in_image,2);
handles.I=in_image2;
guidata(hObject,handles);%图像串行化
figure,imshow(in_image);
figure,imshow(in_image2);


% --- Executes on button press in 缩小.
function narrow_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
in_image=[handles.I,handles.map];
in_image2=imresize(in_image,0.5);
handles.I=in_image2;
guidata(hObject,handles);%图像串行化
figure,imshow(in_image);
figure,imshow(in_image2);

5、镜像代码:通过mirror()函数对图像进行镜像转换

(1)我们需要先写一个mirror函数

function OutImage=mirror(InImage,n)
%mirror函数实现图像镜像变换功能
%参数n为1时,实现水平镜像变换
%参数n为2时,实现垂直镜像变换
%参数n为3时,实现水平垂直镜像变换
I=InImage;
[M,N,G]=size(I);%获取输入图像I的大小
J=I;  %初始化新图像矩阵全为1,大小与输入图像相
if (n==1)
    for i=1:M
        for j=1:N
            J(i,j,:)=I(M-i+1,j,:);%n=1,水平镜像
        end
    end;
elseif (n==2)
     for i=1:M
        for j=1:N
            J(i,j,:)=I(i,N-j+1,:);%n=2,垂直镜像
        end
     end    
elseif (n==3)
     for i=1:M
        for j=1:N
            J(i,j,:)=I(M-i+1,N-j+1,:);%n=3,水平垂直镜像
        end
     end
else
    error('参数n输入不正确,n取值1、2、3')%n输入错误时提示
end
    OutImage=J;
(2)使用mirror函数

% --- Executes on button press in 镜像.
function mirror_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
in_image=[handles.I,handles.map];
in_image2=mirror(in_image,1);% 镜像函数是一个比较古老的函数,虽然现在还可以用,但是这个函数已经被matlab淘汰了
handles.I=in_image2;
guidata(hObject,handles);%图像串行化
figure,
subplot(1,2,1),imshow(in_image);
subplot(1,2,2),imshow(in_image2);
6、图像的转置:

(1)我们需要先写一个转置函数

function J=transp(I)
%I表示输入的原始图像
%J表示经过转置以后的图像
[M,N,G]=size(I);%获取输入图像I的大小
I=im2double(I); %将图像数据类型转换成双精度
J=ones(N,M,G);  %初始化新图像矩阵全为1,大小与输入图像相同
for i=1:M
    for j=1:N
      J(j,i,:)=I(i,j,:);%进行图像转置    
    end
end

(2)使用转置函数

% --- Executes on button press in overturn.
function overturn_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
in_image=[handles.I,handles.map];
in_image2=transp(in_image);
handles.I=in_image2;
guidata(hObject,handles);%图像串行化
figure,
subplot(1,2,1),imshow(in_image);
subplot(1,2,2),imshow(in_image2);
7、图像的旋转:

% --- Executes on button press in 顺时针旋转.
function rotateCW_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
in_image=[handles.I,handles.map];
in_image2=imrotate(in_image,-20,'nearest','crop');%顺时针逆时针只是第二个参数的符号不同
handles.I=in_image2;
guidata(hObject,handles);%图像串行化
figure,
subplot(1,2,1),imshow(in_image);
subplot(1,2,2),imshow(in_image2);


% --- Executes on button press in 逆时针旋转.
function rotateACW_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
in_image=[handles.I,handles.map];
in_image2=imrotate(in_image,20,'nearest','crop');%顺时针逆时针只是第二个参数的符号不同
handles.I=in_image2;
guidata(hObject,handles);%图像串行化
figure,
subplot(1,2,1),imshow(in_image);
subplot(1,2,2),imshow(in_image2);

8、图像截取某一部分:(这里是可以通过捕捉鼠标移动的方式来做一个简单截图软件的)

% --- Executes on button press in 图像的剪切.
function cut_Callback(hObject, eventdata, handles)
handles=guidata(hObject);
in_image=[handles.I,handles.map];
rect=[0 300 200 300];           %定义剪切区域
in_image2=imcrop(in_image,rect);              %进行图像剪切
set(0,'defaultFigurePosition',[100,100,1000,500]);%修改图形图像位置的默认设置
set(0,'defaultFigureColor',[1 1 1])%修改图形背景颜色的设置
axes(handles.axesbig);%把显示范围限定在axesbig
imshow(in_image2);%显示图片







  • 4
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值