1.實驗目的:
1) 瞭解MATLAB軟件平臺的基本操作。
2) 學會應用GUI輸入輸出圖像,能提取圖像的直方圖信息,並能對圖像進行直方圖均衡。
3) 基於MATLAB平臺實現在GUI內圖像間的交互處理。
2.實驗內容:
1) 編寫MATLAB程序,完成圖像的輸入,並將該圖像的直方圖信息輸出並保存。
2) 對上述圖像進行直方圖均衡處理,將得到的新圖像與原圖進行對比展示。
3) 在1)讀入的圖像中任意選取一個150*150大小的子區域,輸出並保存該子區域的灰度直方圖,要求直方圖的柱數(bin)爲32。
3.具體實驗:
構造簡單界面
1.圖像的輸入
function pushbutton1_Callback(hObject, eventdata, handles)
[filename,pathname] = uigetfile;
im = imread([pathname filename]);
axes(handles.axes1);
imshow(im)
handles.im = im;
guidata(hObject,handles)
- 直方圖均衡處理
function pushbutton2_Callback(hObject, eventdata, handles)
im2=handles.im;
ws=str2num(get(handles.edit1,'string'));
pos=(get(handles.axes1,'currentpoint'));
posx=floor(pos(1,2));
posy=floor(pos(1,1));
localimage=im2(posx-ws:posx+ws,posy-ws:posy+ws);
axes(handles.axes2)
imshow(localimage)
handles.localimage=localimage;
guidata(hObject,handles)
- 輸出子區域的灰度直方圖的柱數(bin)爲32
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)
im3=handles.localimage;
bin=str2num(get(handles.edit2,'string'));
L=bin;
grap=256/bin;
I=ceil(im3/grap);
I(find(I==0))=1;
h=zeros(L,1);
s=zeros(L,1);
[r,c]=size(I);
n=r*c;
for i=1:r
for j=1:c
d=double(I(i,j));
s(d)=s(d)+1;
end
end
for i=1:L
h(i)=s(i)/n;
end
a = h(1:L/bin:L);
b = s(1:L/bin:L);
axes(handles.axes3);
bar(1:(L/bin):L,a);
4.實驗總結
通過本實驗,瞭解了MATLAB軟件的基本操作,編寫程序完成了圖像的輸入,以及對輸入的圖像進行一系列處理,例如,輸出圖像的直方圖信息、對圖像進行直方圖均衡處理以及在圖像中選取子區域,輸出子區域的灰度直方圖,並且規定了直方圖的柱數爲確定值。