global T
axes(handles.axes2); T=getimage;
x=imcrop(handles.img); %截图 imshow(x); handles.img=x;
guidata(hObject,handles);
4.5 图像转化为灰度图像。
由于在matlab中较多的图像处理函数支持对灰度图像进行处理,故对图像进行灰度转化十分必要。可利用rgb2gray(X)函数对其他图像进行灰度图像的转化。 转化实例如下:
实现程序段如下:
% --- Executes on button press in radiobutton16.
function radiobutton16_Callback(hObject, eventdata, handles) % hObject handle to radiobutton16 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton16 global T
axes(handles.axes2);
16 / 54
T=getimage;
x=rgb2gray(handles.img); %RGBí???×a???a?ò?èí??? imshow(x); handles.img=x;
guidata(hObject,handles);
4.6对图像进行放大和缩小整数倍的操作。
通过imresize(X,n,mode)函数对图像X进行放大或者缩小。N放大缩小倍数,mode为采用的方式。
通过处理后可发现保存的图片的比原图放大了(缩小了)。 实现的程序段如下:
function uipanel9_SelectionChangeFcn(hObject, eventdata, handles) % hObject handle to uipanel9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global T
str=get(hObject,'string'); axes(handles.axes2); switch str
case'最近邻插值' T=getimage;
prompt={'输入参数:'}; defans={'2'};
p=inputdlg(prompt,'input',1,defans); p1=str2num(p{1});
f=imresize(handles.img,p1,'nearest'); imshow(f); handles.img=f;
guidata(hObject,handles);
17 / 54
case'双线性插值' T=getimage;
prompt={'输入参数:'}; defans={'1'};
p=inputdlg(prompt,'input',1,defans); p1=str2num(p{1});
f=imresize(handles.img,p1,'bilinear'); imshow(f); handles.img=f;
guidata(hObject,handles); end
4.7图像直方图统计和直方图均衡。
(1)通过histeq(X)函数实现直方图均衡。
因为此函数只能对灰度图像进行直方图均衡。故应先将彩图转为灰度图像。
18 / 54
在上一步的基础上对第二幅图进行直方图均衡:
直方图均衡实现程序段如下:
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles) % hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global T
axes(handles.axes2); T=getimage;
h=histeq(handles.img); imshow(h); handles.img=h;
guidata(hObject,handles);
关键部分:通过 h=histeq(handles.img)进行直方图均衡 (2)直方图统计。通过利用imhist(X)函数来实现直方图统计。
19 / 54
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles) % hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) axes(handles.axes2);
x=imhist(handles.img); %直方图统计 x1=x(1:10:256); horz=1:10:256; bar(horz,x1);
axis([0 255 0 15000]);
set(handles.axes2,'xtick',0:50:255); set(handles.axes2,'ytick',0:2000:15000);
注意:横纵坐标的范围应选取适当,否则,统计图表有可能超出范围。 4.8加入各种噪声,并通过几种滤波算法实现去噪。
(1)加入噪声。通过imnoise(I,type,parameters)来加入各种噪声。
20 / 54
MATLAB与GUI图像处理



