文档库 最新最全的文档下载
当前位置:文档库 › 代码及注释

代码及注释

function varargout = FR_Processed_histogram(varargin)
% FR_PROCESSED_HISTOGRAM MATLAB code for FR_Processed_histogram.fig
% FR_PROCESSED_HISTOGRAM, by itself, creates a new FR_PROCESSED_HISTOGRAM or raises the existing
% singleton*.
%
% H = FR_PROCESSED_HISTOGRAM returns the handle to a new FR_PROCESSED_HISTOGRAM or the handle to
% the existing singleton*.
%
% FR_PROCESSED_HISTOGRAM('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FR_PROCESSED_HISTOGRAM.M with the given input arguments.
%
% FR_PROCESSED_HISTOGRAM('Property','Value',...) creates a new FR_PROCESSED_HISTOGRAM or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before FR_Processed_histogram_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to FR_Processed_histogram_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 FR_Processed_histogram

% Last Modified by GUIDE v2.5 13-May-2016 10:38:32

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @FR_Processed_histogram_OpeningFcn, ...
'gui_OutputFcn', @FR_Processed_histogram_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
% End initialization code - DO NOT EDIT


% --- Executes just before FR_Processed_histogram is made visible.
function FR_Processed_histogram_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to FR_Processed_histogram (see VARARGIN)

% Choose default command line output for FR_Processed_histogram
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes FR_Processed_histogram wait for user response (see UIRESUME)
% uiwait(handles.FR_Processed_histogram);
global total_sub train_img sub_img max_hist_level bin_num form_bin_num;

total_sub=40; %共40组图片
train_img=200; %每组取5张训练图片,一共有200张
sub_img=10; %每组有10张图片
max_hist_level=256; %图片的分辨率,即16*16;
bin_num=9; %将每组10张

图片从0到9排序,最大的序号为9
form_bin_num=29;

% --- Outputs from this function are returned to the command line.
function varargout = FR_Processed_histogram_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;

% --- Executes on button press in Training.
function Training_Callback(hObject, eventdata, handles)
% hObject handle to Training (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

global train_processed_bin;
global total_sub train_img sub_img max_hist_level bin_num form_bin_num;

train_processed_bin(form_bin_num,train_img)=0;
K=1; %待训练的图片的序号
train_hist_img=zeros(max_hist_level,train_img); %初始化训练图片的图像矩阵
for Z=1:1:total_sub
for X=1:2:sub_img
I=imread(strcat('s',int2str(Z),'\',int2str(X),'.pgm')); %读入所有组的所有张的图片,创建图像矩阵
[rows cols]=size(I); %得到图像矩阵的行、列数
for i=1:1:rows
for j=1:1:cols
if(I(i,j)==0) %当扫描完该图片的图像矩阵的各个像素后,回到下一个周期的扫描像素起点
train_hist_img(max_hist_level,K)=train_hist_img(max_hist_level,K)+1; %遍历不同直方图
else
train_hist_img(I(i,j),K)=train_hist_img(I(i,j),K)+1; %遍历同一个直方图的像素点
end
end
end
K=K+1;
end
end

[r c]=size(train_hist_img); %获取训练图的图像尺寸
sum=0;

for i=1:1:c
K=1;
for j=1:1:r
if((mod(j,bin_num))==0) %对灰度取整判断
sum=sum+train_hist_img(j,i); %对各像素进行灰度统计
train_processed_bin(K,i)=sum/bin_num;%进行灰度映射转换
K=K+1;
sum=0;
else
sum=sum+train_hist_img(j,i);
end
end
train_processed_bin(K,i)=sum/bin_num;%计算新的像素值
end
display('Training Done')
save 'train' train_processed_bin; %存储训练模型

% --- Executes on button press in Testing.
function Testing_Callback(hObject, eventdata, handles)
% hObject handle to Testing (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global train_img max_hist_level bin_num form_bin_num;
global train_processed_bin;
global filename pathname I
load 'train'%加载训练模型
test_hist_img(max_hist_level) = 0;
test_processed_bin(form_bin_num) = 0;%初始化测试直方图


[rows cols] = size(I);

for i=1:1:rows
for j=1:1:cols
if( I(i,j) == 0 )
test_hist_img(max_hist_level) = test_hist_img(max_hist_level) + 1;
els

e
test_hist_img(I(i,j)) = test_hist_img(I(i,j)) + 1;
end
end
end
%计算测试直方图的每个灰度级出现的次数
[r c] = size(test_hist_img);
sum = 0;

K = 1;
for j=1:1:c
if( (mod(j,bin_num)) == 0 )
sum = sum + test_hist_img(j);
test_processed_bin(K) = sum/bin_num;
K = K + 1;
sum = 0;
else
sum = sum + test_hist_img(j);
end
end
%累计归一化的直方图
test_processed_bin(K) = sum/bin_num;
%计算新的像素值
sum = 0;
K = 1;

for y=1:1:train_img
for z=1:1:form_bin_num
sum = sum + abs( test_processed_bin(z) - train_processed_bin(z,y) ); %累计训练直方图与测试直方图的每个灰度级的误差
end
img_bin_hist_sum(K,1) = sum;%存储灰度级的训练准确率
sum = 0;
K = K + 1;
end %对每张测试图片重复以上操作

[temp M] = min(img_bin_hist_sum);%选择误差最小的训练模型,赋予其相应编号
M = ceil(M/5);%将M值转换成图片所在组的编号
getString_start=strfind(pathname,'s');
getString_start=getString_start(end)+1;
getString_end=strfind(pathname,'\');
getString_end=getString_end(end)-1;
subjectindex=str2num(pathname(getString_start:getString_end));
%获取到选择的图片所在组编号
if (subjectindex == M)%如果选择结果与训练结果相同
axes (handles.axes2)
%image no: 5 is shown for visualization purpose
imshow(imread(STRCAT('s',num2str(M),'\1.pgm')))
msgbox ( 'Correctly Recognized');%如果与图片库的图片有最相似的,则显示出所在组的第一张图片
else
display ([ 'Error==> Testing Image of Subject >>' num2str(subjectindex) ' matches with the image of subject >> ' num2str(M)])
axes (handles.axes2)
%image no: 5 is shown for visualization purpose
imshow(imread(STRCAT('s',num2str(M),'\8.pgm'))) %如果没有匹配上则显示编号为8图片
msgbox ( 'Incorrectly Recognized');
end
subjectindex
display('Testing Done')


function box_Callback(hObject, eventdata, handles)
% hObject handle to box (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 box as text
% str2double(get(hObject,'String')) returns contents of box as a double

%--------------------------------------------------------------------------
% --- Executes during object creation, after setting all properties.
function box_CreateFcn(hObject, eventdata, handles)
% hObject handle to box (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles no

t 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



% --- Executes on button press in Input_Image.
function Input_Image_Callback(hObject, eventdata, handles)
% hObject handle to Input_Image (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global filename pathname I
[filename pathname] = uigetfile('*.pgm','人脸识别');
axes(handles.axes1)
imgpath=strcat(pathname,filename);

I = imread(imgpath);
imshow(I)


% --- Executes during object creation, after setting all properties.
function axes2_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes2 (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 axes2


% --- Executes during object creation, after setting all properties.


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (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 axes1


% --- Executes on button press in detection.
function detection_Callback(hObject, eventdata, handles)
% hObject handle to detection (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global I J
J=I;
BW=im2bw(J);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% minimisation of background portion

[n1 n2]=size(BW);
r=floor(n1/10);
c=floor(n2/10);
x1=1;x2=r;
s=r*c;

for i=1:10
y1=1;y2=c;
for j=1:10
if (y2<=c | y2>=9*c) | (x1==1 | x2==r*10)
loc=find(BW(x1:x2, y1:y2)==0);
[o p]=size(loc);
pr=o*100/s;
if pr<=100
BW(x1:x2, y1:y2)=0;
r1=x1;r2=x2;s1=y1;s2=y2;
pr1=0;
end

end
y1=y1+c;
y2=y2+c;
end

x1=x1+r;
x2=x2+r;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% detection of face object

L = bwlabel(BW,8);
BB = regionprops(L, 'BoundingBox');
BB1=struct2cell(BB);
BB2=cell2mat(BB1);

[s1 s2]=size(BB2);
mx=0;
for k=3:4:s2-1
p=BB2(1,k)*BB2(1,k+1);
if p>mx & (BB2(1,k)/BB2(1,k+1))<1.8
mx=p;
j=k;
end
end
axes (handles.axes3)
imshow(J)

rectangle('Position',[BB2(1,j-2),BB2(1,j-1),BB2(1,j),BB2(1,j+1)],'EdgeColor','r' )
msgbox

( 'Face Detection');

% --- 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


% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[y,Fs,bits] = wavread('dl.wav'); %获取音乐数据
ao = analogoutput('winsound'); %建立硬件对象
addchannel(ao,[1 2]); %创建声音输出通道
set(ao,'SampleRate',Fs) %设置采样率
data1 =y(:,1); %双声道
data2 =y(:,2);
putdata(ao,[data1 data2]); %往声卡堆音乐数据
%输出音乐数据
setappdata(handles.FR_Processed_histogram,'ao',ao);

start(ao);



% --------------------------------------------------------------------
function pause_Callback(hObject, eventdata, handles)
% hObject handle to pause (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
ao=getappdata(handles.FR_Processed_histogram,'ao');
stop(ao);


% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function openfile_Callback(hObject, eventdata, handles)
% hObject handle to openfile (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;*.pgm;*.jpeg','image files(*.bmp;*.jpg;*.pgm;*.jpeg)';...
'*.*','all file (*.*)'},...
'pick an image');
if isequal (filename,0)|| isequal (pathname,0)
return ;
end
axes(handles.axes_src);
fpath=[pathname filename];
imgsrc=imread(fpath);
setappdata(handles.FR_Processed_histogram,'imgsrc',imgsrc);
imshow(imgsrc);




% --------------------------------------------------------------------
function save_Callback(hObject, eventdata, handles)
% hObject handle to

save (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename,pathname]=uiputfile({'*.bmp','BMP files';'*.pgm','PGM files';'*.jpg','JPG files';},'Pick an Image');
if isequal (filename,0)|| isequal (pathname,0)
return ;
else
fpath=fullfile(pathname,filename);
end
w=getappdata(handles.FR_Processed_histogram,'imgsrc');
imwrite(w,fpath);

相关文档