1 简介
直方图均衡化( Histogram Equalization,HE)技术可以在图像增强[2]、光照补偿[3]、图像去雾[4-5]多个领域取得很好的效果,可见其应用范围广。
2 部分代码
function varargout = main(varargin)
% MAIN MATLAB code for main.fig
% MAIN, by itself, creates a new MAIN or raises the existing
% singleton*.
%
% H = MAIN returns the handle to a new MAIN or the handle to
% the existing singleton*.
%
% MAIN('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MAIN.M with the given input arguments.
%
% MAIN('Property','Value',...) creates a new MAIN or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before main_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to main_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help main
% Last Modified by GUIDE v2.5 19-Apr-2016 17:05:45
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui_OpeningFcn, ...
'gui_OutputFcn', @main_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% --- Outputs from this function are returned to the command line.
function varargout = main_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
%%GUI界面初始化
function gui_OpeningFcn(hObject, eventdata, handles, varargin)
axes( handles.axes1 )
imshow( 'GUI问号.jpg' ) ;
axes( handles.axes2 )
imshow( 'GUI问号.jpg' ) ;
axes( handles.axes3 )
imshow( 'GUI问号.jpg' ) ;
axes( handles.axes4 )
imshow( 'GUI问号.jpg' ) ;
handles.output = hObject;
set( handles.slider1 , 'Value' , 0.8 );
set( handles.edit1 , 'String' ,0.8 ) ;
guidata(hObject, handles);
%%打开测试图像
% --- Executes on button press in open_test_img.
function open_test_img_Callback(hObject, eventdata, handles)
% hObject handle to open_test_img (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global file %定义全局变量
[filename,pathname]=uigetfile({'*.jpg';'*.bmp';'*.gif';'*.png';'*.tif';'*.tga'},'选择测试图像');%跳出对话框
file=strcat(pathname,filename);%生成完成的图像路径和图像名
I=imread(file);%读取图像
axes(handles.axes1);%在第一个坐标系中显示打开的图片
imshow(I);
%%进行训练
% --- Executes on button press in start_test.
function start_test_Callback(hObject, eventdata, handles)
% hObject handle to start_test (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global filename wav_num mydir wav_path_list %全局变量
h = waitbar(0,'正在训练,请稍等...');
for j=1:wav_num
str= strcat(fullfile(mydir, wav_path_list(j).name));
I=imread(str);
[count1,I] = GetRgbHist(str);
Count{j}=count1;
II{j}=I;
waitbar( j/wav_num ) ;
end
save('Parametre.mat','Count','II');
close(h);
%%退出按钮
% --- Executes on button press in close.
function close_Callback(hObject, eventdata, handles)
% hObject handle to close (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close;
% --- Executes on button press in open_xunlian.
function open_xunlian_Callback(hObject, eventdata, handles)
% hObject handle to open_xunlian (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global filename wav_num mydir wav_path_list %全局变量
mydir=uigetdir('c:','请选择训练样本所在的目录');%跳出对话框
wav_path_list = dir(fullfile(mydir,'*.jpg'));
wav_num=length(wav_path_list);
filename={wav_path_list.name};
str = sprintf('该目录包含%d个图像文件',wav_num);
msgbox(str);
%%开始检测
% --- Executes on button press in start_jiance.
function start_jiance_Callback(hObject, eventdata, handles)
% hObject handle to start_jiance (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load 'Parametre.mat';
[a,b]=size(II);
global file
[count2,I2] = GetRgbHist(file);
%bbbbb=count2
for i=1:b
value(i) = imsimilar(Count{i},count2,2);
end
maxvalue=max(value);
[row col]=find(maxvalue==value);
axes(handles.axes2);
imshow(II{col});
axes(handles.axes3);
plot(Count{col});
hold on;
plot(count2,'r');
legend('测试图','近似图',2);
str = sprintf('图像直方图 相似测试 相似度为:%s %%',num2str(value(col)));
title(str);
A=(get(handles.slider1,'Value'))*100
if maxvalue>=A
axes(handles.axes4);
imshow('危险品.jpg');
[y,fs]=audioread('警报.mp3');
sound(y,fs);
else
axes(handles.axes4);
imshow('正常.png');
end
guidata( hObject , handles ) ;
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
temp_Invisible_w = get( hObject , 'Value' ) ;
set( handles.edit1 , 'String' , num2str(temp_Invisible_w) ) ;
handles.Invisible_w = temp_Invisible_w ;
guidata( hObject , handles ) ;
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
value0 = str2double( get( hObject , 'String' ) ) ;
if isnan(value0)
msgbox('要输入数字才行哦!');
set( hObject , 'String' , '0' );
set( handles.slider1 , 'Value' , 0);
elseif value0<0 | value0 >1
msgbox('只能数入0-1才行哦!');
set( hObject , 'String' , '0' );
set( handles.slider1 , 'Value' , value0 );
else
set( handles.slider1 , 'Value' , value0 );
handles.edit1 = value0 ;
guidata( hObject , handles ) ;
end
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
3 仿真结果
4 参考文献
[1]张懿, 刘旭, and 李海峰. "自适应图像直方图均衡算法." 浙江大学学报(工学版) 41.4(2007):630-633.
部分理论引用网络文献,若有侵权联系博主删除。
5 MATLAB代码与数据下载地址
见博客主页