% --- Executes on button press in Loadbutton.
function Loadbutton_Callback(hObject, eventdata, handles)
% hObject handle to Loadbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename,pathname]=uigetfile({'*.*';'*.bmp';'*.jpg';'*.tif';'*.jpg'},'选择图像');
if isequal(filename,0)||isequal(pathname,0)
errordlg('您还没有选取图片!!','温馨提示');%如果没有输入,则创建错误对话框
return;
end
image=[pathname,filename];%合成路径+文件名
im=imread(image);%读取图像
set(handles.axes1,'HandleVisibility','ON');%打开坐标,方便操作
axes(handles.axes1);%使用图像,操作在坐标1
imshow(im);%在坐标axes1显示原图像
title('原始图像');
RGB = imread(image);
handles.RGB = RGB;
handles.RGB2 = RGB;
handles.fileLoaded = 1;
handles.fileLoaded2 = 0;
im=double(im);
avg_im=mean(im);%计算均值
avg_imc=num2str(avg_im);%转换数据类型(以下同理)
var_im=var(im(:));
var_imc=num2str(var_im);
max_im=max(im(:));
max_imc=num2str(max_im);
min_im=min(im(:));
min_imc=num2str(min_im);
set(handles.text2,'String','均值');
set(handles.text3,'String','方差');
set(handles.text4,'String','最大值');
set(handles.text5,'String','最小值');
set(handles.mean_staticText,'String',avg_imc);
set(handles.var_staticText,'String',var_imc);
set(handles.maximum_staticText,'String',max_imc);
set(handles.minimum_staticText,'String',min_imc);
GUIdata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function text2_CreateFcn(hObject, eventdata, handles)
% hObject handle to text2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in Conv.
function Conv_Callback(hObject, eventdata, handles)
% hObject handle to Conv (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if (handles.fileLoaded == 1)
Gray=rgb2gray(handles.RGB);
axes(handles.axes1);imshow(Gray);title('原灰度图像');
A=double(Gray);
H=[1 0 -1;1 0 -1;1 0 -1];
Conv=conv2(A,H,'same');
axes(handles.axes2);imshow(Conv);title('滤波处理后');
GUIdata(hObject, handles);
else
H = msgbox('No primary file has been loaded!','Error','error');
end
% --- Executes on button press in Color2Gray.
function Color2Gray_Callback(hObject, eventdata, handles)
% hObject handle to Color2Gray (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if (handles.fileLoaded==1)
Gray = rgb2gray(handles.RGB);
axes(handles.axes2); imshow(Gray);title('灰度图像');
handles.fileLoaded2 = 1;
set(handles.mean_staticText,'String',' ');
set(handles.text2,'String',' ');
set(handles.var_staticText,'String',' ');
set(handles.text3,'String',' ');
set(handles.maximum_staticText,'String',' ');
set(handles.text4,'String',' ');
set(handles.minimum_staticText,'String',' ');
set(handles.text5,'String',' ');
GUIdata(hObject,handles);
end
% --- Executes on button press in Hist.
function Hist_Callback(hObject, eventdata, handles)
% hObject handle to Hist (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.axes1,'Visible','off'); set(handles.axes2,'Visible','off');
set(handles.axeshist1,'Visible','off'); set(handles.axeshist2,'Visible','off');
if (handles.fileLoaded == 1)
Gray = rgb2gray(handles.RGB);
[height,width] = size(Gray);
axes(handles.axes1); imshow(Gray);title('原灰度图');
axes(handles.axeshist1); imhist(Gray);
s = zeros(1,256);%统计各灰度数目,共256个灰度级
for i = 1:height
for j = 1: width
s(Gray(i,j) + 1) = s(Gray(i,j) + 1) + 1;%对应灰度值像素点数量增加一
end
end
%计算灰度分布密度
p = zeros(1,256);
for i = 1:256
p(i) = s(i) / (height * width * 1.0);
end
%计算灰度分布密度
p = zeros(1,256);
for i = 1:256
p(i) = s(i) / (height * width * 1.0);
end
%计算累计直方图分布
c = zeros(1,256);
c(1) = p(1);
for i = 2:256
c(i) = c(i - 1) + p(i);
end
%累计分布取整,将其数值归一化为1~256
c = uint8(255 .* c + 0.5);
%对图像进行均衡化
for i = 1:height
for j = 1: width
Gray(i,j) = c(Gray(i,j)+1);
end
end
axes(handles.axes2); imshow(Gray);title('直方图均衡化');
axes(handles.axeshist2); imhist(Gray);
GUIdata(hObject,handles);
end
% --- Executes on button press in Log.
function Log_Callback(hObject, eventdata, handles)
% hObject handle to Log (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.axes1,'Visible','off'); set(handles.axes2,'Visible','off');
set(handles.axeshist1,'Visible','off'); set(handles.axeshist2,'Visible','off');
if (handles.fileLoaded == 1)
Gray = rgb2gray(handles.RGB);
axes(handles.axes1); imshow(Gray);title('原灰度图');
Gray=double(Gray);
newpic=log(1+Gray);
axes(handles.axes2);
imshow(newpic,[]);
title('对数变换后');
GUIdata(hObject,handles);
end
% --- 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)
% --- Executes during object creation, after setting all properties.
function mean_staticText_CreateFcn(hObject, eventdata, handles)
% hObject handle to mean_staticText (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
function axes3_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes3
以上代码。。