注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

斗天堂

douzsh还活着

 
 
 

日志

 
 

基于matlab的彩色图像皮肤区域分割算法与实现(2)  

2007-09-11 17:05:26|  分类: 图像识别 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

附录

附录1 主程序代码(部分注释省略)

function varargout = Face_detection(varargin)

% FACE_DETECTION M-file for Face_detection.fig

% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Face_detection

% Last Modified by GUIDE v2.5 18-Jul-2007 17:14:34

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name',       mfilename, ...

                   'gui_Singleton',  gui_Singleton, ...

                   'gui_OpeningFcn', @Face_detection_OpeningFcn, ...

                   'gui_OutputFcn',  @Face_detection_OutputFcn, ...

                   'gui_LayoutFcn',  [] , ...

                   'gui_Callback',   []);

if nargin & isstr(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 Face_detection is made visible.

function Face_detection_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 Face_detection (see VARARGIN)

% Choose default command line output for Face_detection

handles.output = hObject;

% Update handles structure

guidata(hObject, handles);

% UIWAIT makes Face_detection wait for user response (see UIRESUME)

% uiwait(handles.figure1);

%%%%以下代码设置按钮和菜单的状态激活还是非激活

set (handles.btnfilter,'Enable','off');

set (handles.btnycbcr,'Enable','off');

set (handles.btnsimi,'Enable','off');

set (handles.btnbw2,'Enable','off');

set (handles.btnresult,'Enable','off');

set (handles.hist_menu,'Enable','off');

set (handles.rgbhist_menu,'Enable','off');

% --- Outputs from this function are returned to the command line.

function varargout = Face_detection_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;

 

%按钮Load回调函数

% --- Executes on button press in btnload.

function btnload_Callback(hObject, eventdata, handles)

% hObject    handle to btnload (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

global filename

global x

[picname,picpath,filterindex]=uigetfile(...

    {['*.jpg;*.tiff;*.gif;*.bmp;*.png;*.hdf;*.pcx;',....

    '*.xwd;*.ico;*.cur;*.ras;*.pbm;*.pgm;*.ppm'],....

    ['All Image Files (*.jpg,*.tiff,*.gif,',....

    '*.bmp,*.png,*.hdf,*.pcx,*.xwd,*.ico,',....

    '*.cur,*.ras,*.pbm,*.pgm,*.ppm)'];....

    '*.jpg','JPEG Image(*.jpg)'; ...

    '*.tiff','TIFF Image(*.tiff)'; ...

    '*.gif','GIF Image(*.gif)'; ...

    '*.bmp','BMP Image(*.bmp)'; ...

    '*.png','PNG Image(*.png)'; ...

    '*.hdf','RASTER Image(*.hdf)';....

    '*.pcx','PCX Image(*.pcx)';....

    '*.xwd','PIXMAPS Image(*.xwd)';....

    '*.ico','ICO Image(*.ico)';....

    '*.cur','CUR Image(*.cur)';....

    '*.ras','RAS Image(*.ras)';....

    '*.pbm','PBM Image(*.pbm)';....

    '*.pgm','PGM Image(*.pgm)';....

    '*.ppm','PPM Image(*.ppm)'},....

    '请选择要处理的图像');

if isequal(picname,0) | isequal(picpath,0)

    msgbox({'没有选择文件.';'请选择一个图片文件.'},'Error','modal');

    return;  

end %if

filename=strcat(picpath,picname);

x= imread(filename);

axes(handles.axes1);

imshow(x);

set (handles.btnfilter,'Enable','on');

set (handles.hist_menu,'Enable','off');

set (handles.btnycbcr,'Enable','off');

set (handles.btnsimi,'Enable','off');

set (handles.btnbw2,'Enable','off');

set (handles.btnresult,'Enable','off');

set (handles.rgbhist_menu,'Enable','on');

%按钮Filter回调函数

% --- Executes on button press in btnfilter.

function btnfilter_Callback(hObject, eventdata, handles)

% hObject    handle to btnfilter (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

global x

fR=x(:,:,1);

fG=x(:,:,2);

fB=x(:,:,3);

f=1/9*ones(3);%低通滤波器滤除高频噪声

filtered_fR=imfilter(fR,f);

filtered_fG=imfilter(fG,f);

filtered_fB=imfilter(fB,f);

x_filtered=cat(3,filtered_fR,filtered_fG,filtered_fB);

axes(handles.axes2);

imshow(x_filtered);

set (handles.btnycbcr,'Enable','on');

set (handles.btnsimi,'Enable','off');

set (handles.btnbw2,'Enable','off');

set (handles.btnresult,'Enable','off');

%按钮YCbCr回调函数

% --- Executes on button press in btnycbcr.

function btnycbcr_Callback(hObject, eventdata, handles)

% hObject    handle to btnycbcr (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

global filename

global x

global I

I=rgb2ycbcr(x);

axes(handles.axes3);

imshow(I);

set (handles.hist_menu,'Enable','on');

set (handles.btnsimi,'Enable','on');

set (handles.btnbw2,'Enable','off');

set (handles.btnresult,'Enable','off');

%按钮BW2回调函数

% --- Executes on button press in btnbw2.

function btnbw2_Callback(hObject, eventdata, handles)

% hObject    handle to btnbw2 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

global filename

global p

global I

global a

global b

global x

global s

if filename==0;

    errordlg('You Should Load Image File Fisrt','Warning...');

    return

end 

[a,b,c]=size(I); %得到图像的像素点个数

cb=double(I(:,:,2));

cr=double(I(:,:,3));

for i=1:a

    for j=1:b

        w=[cb(i,j),cr(i,j)];

        m=[117.4316 148.5599];

        n=[260.1301 12.1430;12.1430 150.4574]; %前面建立肤色模型得到的结论

 p(i,j)=exp((-0.5)*(w-m)*inv(n)*(w-m)');

        if (p(i,j)<0.5) p(i,j)=0;x(i,j,1)=0;x(i,j,2)=0;x(i,j,3)=0;

        else p(i,j)=1;

        end

    end

end

%previousp = zeros(i,j);

%  changelist = [];

%for threshold = 0.55:-0.1:0.05

 %  p2 = zeros(i,j);

  % p2(find(p>threshold)) = 1;

   %change = sum(sum(p2 - previousp));

  % changelist = [changelist change];

  % previousp = p2;

  %end

% Finding the optimal threshold

%[C, U] = min(changelist);

%optimalThreshold = (7-U)*0.1

%p2 = zeros(i,j);

%p2(find(p>optimalThreshold)) = 1;

for i=1:a

    for j=1:b

       q(i,j)=double(255*p(i,j));

   end

end

s=medfilt2(q);

%以下代码为图像的形态学处理

%利用开闭运算的方法消除噪声

se=strel('square',3);

s=imopen(s,se);

s=imclose(s,se);

%填孔处理

s=imfill(s,'holes');

%重构法

fe=imerode(s,ones(8,7));

fo=imopen(s,ones(8,7));

s=imreconstruct(fe,s);

axes(handles.axes5);

imshow(s);

set (handles.btnresult,'Enable','on');

%按钮ShowResult回调函数

% --- Executes on button press in btnresult.

function btnresult_Callback(hObject, eventdata, handles)

% hObject    handle to btnresult (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

global filename

global x

global I

global a

global b

global p

if filename==0;

    errordlg('You Should Load Image File Fisrt','Warning...');

    return

end 

[a,b,c]=size(I);

cb=double(I(:,:,2));

cr=double(I(:,:,3));

for i=1:a

    for j=1:b

        w=[cb(i,j),cr(i,j)];

        m=[117.4316 148.5599];

        n=[260.1301 12.1430;12.1430 150.4574];

 p(i,j)=exp((-0.5)*(w-m)*inv(n)*(w-m)');

    if (p(i,j)<0.5) p(i,j)=0;x(i,j,1)=0;x(i,j,2)=0;x(i,j,3)=0;

        else p(i,j)=1;

end

end

end

axes(handles.axes6);

imshow(x);

%似然图回调函数

% --- Executes on button press in btnsimi.

function btnsimi_Callback(hObject, eventdata, handles)

% hObject    handle to btnsimi (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

global I

global a

global b

global filename

if filename==0;

    errordlg('You Should Load Image File Fisrt','Warning...');

    return

end 

[a,b,c]=size(I);

cb=double(I(:,:,2));

cr=double(I(:,:,3));

for i=1:a

    for j=1:b

        w=[cb(i,j),cr(i,j)];

        m=[117.4316 148.5599];

        n=[260.1301 12.1430;12.1430 150.4574];

        p(i,j)=exp((-0.5)*(w-m)*inv(n)*(w-m)');

    end

end

Q=p(:);   

J=p/max(Q);  %每一点像素的肤色似然度除以最大肤色似然度所得到的值,作为该像素点的灰度值

lpf=1/9*ones(3); %低通滤波器滤波处理

J=filter2(lpf,J);

axes(handles.axes4);

imshow(J);

set (handles.btnbw2,'Enable','on');

set (handles.btnresult,'Enable','off');

%Exit按钮回调函数

% --- Executes on button press in btnexit.

function btnexit_Callback(hObject, eventdata, handles)

% hObject    handle to btnexit (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

selection = questdlg(['Exit ' get(handles.figure1,'Name') '?'],...

                     ['Exit ' get(handles.figure1,'Name') '...'],...

                     'Yes','No','Yes');

if strcmp(selection,'No')

    return;

end

delete(handles.figure1)

% --------------------------------------------------------------------

function help_menu_Callback(hObject, eventdata, handles)

% hObject    handle to help_menu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

 

% --------------------------------------------------------------------

function local_menu_Callback(hObject, eventdata, handles)

% hObject    handle to local_menu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

open('readme.txt'); %打开帮助文档

 

% --------------------------------------------------------------------

function web_menu_Callback(hObject, eventdata, handles)

% hObject    handle to web_menu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

helpwin;

 

% --------------------------------------------------------------------

function about_menu_Callback(hObject, eventdata, handles)

% hObject    handle to about_menu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

 msgbox({'陈茂源 .';'中国地质大学武汉信息工程学院.';'E-mail:waseea@163.com';'博客:http://hi.baidu.com/Returner007';

     '永不止步'},'关于作者','modal');

 

% --------------------------------------------------------------------

function View_menu_Callback(hObject, eventdata, handles)

% hObject    handle to View_menu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

 

% --------------------------------------------------------------------

function hist_menu_Callback(hObject, eventdata, handles)

% hObject    handle to hist_menu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

global I

fY=double(I(:,:,1));

fCb=double(I(:,:,2));

fCr=double(I(:,:,3));

figure(2),title('YCbCr直方图'),

subplot(1,3,1),hist(fY),subplot(1,3,2),hist(fCb),subplot(1,3,3),hist(fCr);

 

% --------------------------------------------------------------------

function file_menu_Callback(hObject, eventdata, handles)

% hObject    handle to file_menu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

 

% --------------------------------------------------------------------

function open_menu_Callback(hObject, eventdata, handles)

% hObject    handle to open_menu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

 

% --------------------------------------------------------------------

function exit_menu_Callback(hObject, eventdata, handles)

% hObject    handle to exit_menu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

selection = questdlg(['Exit ' get(handles.figure1,'Name') '?'],...

                     ['Exit ' get(handles.figure1,'Name') '...'],...

                     'Yes','No','Yes');

if strcmp(selection,'No')

    return;

end

delete(handles.figure1)

 

% --- Executes on button press in btndetect.

function btndetect_Callback(hObject, eventdata, handles)

% hObject    handle to btndetect (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

%x=imread('test01.jpg');

global x

global filename

x= imread(filename);

if (size(x,3)>1)%if RGB image make gray scale

    try

        x=rgb2gray(x);%image toolbox dependent

    catch

        x=sum(double(x),3)/3;%if no image toolbox do simple sum

    end

end

x=double(x);%make sure the input is double format

[output,count,m,svec]=facefind(x);%full scan

axes(handles.axes7);

imagesc(x), colormap(gray)%show image

plotbox(output,[],1)%plot the detections as red squares

%plotsize(x,m)%plot minmum and maximum face size to detect as green squares in top left corner

 

% --------------------------------------------------------------------

function colomod_menu_Callback(hObject, eventdata, handles)

% hObject    handle to colomod_menu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

selection=questdlg('请问你是要显示.fig文件还是.jpg文件?','请选择','.fig','.jpg','.fig');

switch selection

    case '.fig'

        open('hist.fig');

    case '.jpg'

        hit=imread('hist.jpg');

        figure(1),imshow(hit);

end

 

% --------------------------------------------------------------------

function Skinmod_menu_Callback(hObject, eventdata, handles)

% hObject    handle to Skinmod_menu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

selection=questdlg('请问你是要显示.fig文件还是.jpg文件?','请选择','.fig','.jpg','.fig');

switch selection

    case '.fig'

        open('gaussian.fig');

    case '.jpg'

        gau=imread('gaussian.jpg');

        figure(1),imshow(gau);

end

% --------------------------------------------------------------------

function rgbhist_menu_Callback(hObject, eventdata, handles)

% hObject    handle to rgbhist_menu (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

global filename

global x

fR=double(x(:,:,1));

fG=double(x(:,:,2));

fB=double(x(:,:,3));

figure(1),title('RGB直方图'),

subplot(1,3,1),hist(fR),subplot(1,3,2),hist(fG),subplot(1,3,3),hist(fB);

  评论这张
 
阅读(697)| 评论(4)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017