基于 Caffe 的人脸识别系统 V1.0
1
clc,clear;
I=imread('3.jpg'); %读图
g=pro2(I);
%光线补偿
fR=g(:,:,1); %RGB 三个通道
fG=g(:,:,2);
fB=g(:,:,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);
h=rgb2ycbcr(x_filtered); %转换到 YCbCr 空间
[a b c]=size(h);
cb=double(h(:,:,2)); %提取两个分量
cr=double(h(:,:,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); %低通滤波器滤波处理
p=filter2(lpf,J);
for i=1:a
%相似度小于 0.5 认为不是肤色,否则认为是
%除去非肤色部分
for j=1:b
if (p(i,j)<0.5)
p(i,j)=0;
ff(i,j,:)=[255 255 255];
else p(i,j)=1;
end
end
end
for i=1:a
for j=1:b
q(i,j)=double(255*p(i,j));
%灰度图
end
end
s1=medfilt2(q);
%中值滤波
se1=strel('square',10);
se2=strel('square',2);
基于 Caffe 的人脸识别系统 V1.0
2
s2=imopen(s1,se1);
s3=imclose(s2,se2);
%开闭运算
[a,b]=bwlabel(s3,8);
c=regionprops(a,'all');
%标记连通区域
model=imread('facemodel.bmp'); %读模板
for i=1:length(c)
d=c(i);
area(i)=d.Area;
end
area_mean=mean(area); %获取肤色区域面积平均值
figure,imshow(I);
hold on
facenum=0; %人脸数
for i=1:length(c)
reg=c(i);
boxcell=reg.BoundingBox;
%逐个分析肤色区域获取
%区域外接矩形
%根据区域的长宽比例以及面积比例大小等,初步过滤
if
boxcell(4)/boxcell(3)>0.8&&boxcell(4)/boxcell(3)<2.5&&boxcell(4)>30&&boxcell(3)>20&®.Area/(boxcell(4)*bo
xcell(3))>=0.5&®.Area>(area_mean*0.65)
facecell=imresize(reg.Image,[30 30]);
sp=corrcoef(double(model),double(facecell)); %和模板差别的相关系数
facestd(i)=sp(1,2);
if facestd(i)>0.5
rectangle('Position',reg.BoundingBox,'EdgeColor','r'); %标记
facenum=facenum+1;
%将获得区域大小调整到和模板一样
%由阈值判断
%计数
end
end
end
}
cvReleaseImage( &frame_copy );
cvReleaseCapture( &capture );
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help BP_Reconise
% Last Modified by GUIDE v2.5 21-May-2013 09:55:35
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton',
gui_Singleton, ...
基于 Caffe 的人脸识别系统 V1.0
3
'gui_OpeningFcn', @BP_Reconise_OpeningFcn, ...
'gui_OutputFcn', @BP_Reconise_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 BP_Reconise is made visible.
function BP_Reconise_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject
% eventdata
% handles
% varargin
handle to figure
reserved - to be defined in a future version of MATLAB
structure with handles and user data (see GUIDATA)
command line arguments to BP_Reconise (see VARARGIN)
% Choose default command line output for BP_Reconise
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes BP_Reconise wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = BP_Reconise_OutputFcn(hObject, eventdata, handles)
% varargout
% hObject
% eventdata
% handles
cell array for returning output args (see VARARGOUT);
handle to figure
reserved - to be defined in a future version of MATLAB
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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject
% eventdata
% handles
global net;
global base;
handle to pushbutton1 (see GCBO)
reserved - to be defined in a future version of MATLAB
structure with handles and user data (see GUIDATA)
基于 Caffe 的人脸识别系统 V1.0
4
[net,base]=BP_Train();
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject
% eventdata
% handles
global A
global TestDatabasePath
handle to pushbutton2 (see GCBO)
reserved - to be defined in a future version of MATLAB
structure with handles and user data (see GUIDATA)
[A,TestDatabasePath] = uigetfile('*pgm');
axes(handles.axes1);
imshow(strcat(TestDatabasePath,A));
%set(handles.text1,'string','ÈËÁ³Ê¶±ð')
handle to pushbutton3 (see GCBO)
reserved - to be defined in a future version of MATLAB
structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject
% eventdata
% handles
global TestDatabasePath
global A
global net;
tcoor= b * base;
test=mapminmax(tcoor);
% test=tcoor';
Y=sim(net,test');
[yi,index2]=sort(Y);
res=index2(40,1);
function [feature] = allFeature(num)
panDuan = 1;
while panDuan
if(featureNumber == 8||featureNumber == 16 || featureNumber == 24|| featureNumber == 32 ||
featureNumber == 48 || featureNumber == 64 || featureNumber == 80 )
panDuan = 0;
disp('¿ªÊ¼×°ÔØÍ¼ÏñÎļþ,ÇëµÈ´ý.......')
dir = ['\1.bmp '
'\2.bmp '
'\3.bmp '
'\4.bmp '
'\5.bmp '
'\6.bmp '
'\7.bmp '
'\8.bmp '
'\9.bmp '
'\10.bmp'];
for x=1:40,
基于 Caffe 的人脸识别系统 V1.0
5
dir = ['\1.bmp ';'\2.bmp ';'\3.bmp ';'\4.bmp '; '\5.bmp ';'\6.bmp '; '\7.bmp ';'\8.bmp ';'\9.bmp '; '\10.bmp'];
for x=1:40,
a = int2str(x);
b = ['s'];
d = [b a];
for i=1:10,
e = [d dir(i,1:7)];
M = double(imread(e));
for j=1:4,
for k=1:4,
`timescale 1 ns/ 1 ps
module warn_vlg_tst();
// constants
// general purpose registers
reg eachvec;
// test vector input registers
reg CLK;
reg RSTn;
reg [1:0] mode;
reg [10:0] yue;
// wires
wire cut;
wire warn0;
// assign statements (if any)
warn i1 (
// port map - connection between master ports and signals/registers
.CLK(CLK),
.RSTn(RSTn),
.cut(cut),
.mode(mode),
.warn0(warn0),
.yue(yue)
);
//---------------------------
initial
begin
RSTn=0;
#200;
RSTn=1;
$display("Running testbench");
end
//-----------------
initial
begin
mode=2'b01;
$display("Running testbench");
end
基于 Caffe 的人脸识别系统 V1.0
6
//----------
initial
begin
yue=11'd1;
$display("Running testbench");
end
//-----------
always
begin
CLK=1;
#10;
CLK=0;
#10;
end
//----------
endmodule
using namespace std;
// skin region location using rgb limitation
int main()
{
IplImage *srcImg = cvLoadImage("hand.jpg", 1);
IplImage *dstRGB = cvCreateImage(cvGetSize(srcImg), 8, 3);
IplImage *dstRG = cvCreateImage(cvGetSize(srcImg), 8, 1);
IplImage* dst_crotsu=cvCreateImage(cvGetSize(srcImg),8,1);
IplImage* dst_ycbcr=cvCreateImage(cvGetSize(srcImg),8,1);
IplImage* dst_yuv=cvCreateImage(cvGetSize(srcImg),8,3);
IplImage* dst_hsv=cvCreateImage(cvGetSize(srcImg),8,3);
cout<
nChannels<基于 Caffe 的人脸识别系统 V1.0
7
cvShowImage("dst_ycbcr", dst_ycbcr);
cvNamedWindow("dst_yuv", 1);
cvShowImage("dst_yuv", dst_yuv);
cvNamedWindow("dst_hsv", 1);
cvShowImage("dst_hsv", dst_hsv);
cvWaitKey(0);
cout << "Hello World!" << endl;
return 0;
}
void SkinRGB(IplImage* rgb,IplImage* _dst)
{
cout<<"111"<nChannels==3&& _dst->nChannels==3);
static const int R=2;
static const int G=1;
static const int B=0;
IplImage* dst=cvCreateImage(cvGetSize(_dst),8,3);
cvZero(dst);
for (int h=0;hheight;h++) {
unsigned char* prgb=(unsigned char*)rgb->imageData+h*rgb->widthStep;
unsigned char* pdst=(unsigned char*)dst->imageData+h*dst->widthStep;
for (int w=0;wwidth;w++) {
if ((prgb[R]>95 && prgb[G]>40 && prgb[B]>20 &&
prgb[R]-prgb[B]>15 && prgb[R]-prgb[G]>15/*&&
!(prgb[R]>170&&prgb[G]>170&&prgb[B]>170)*/)||//uniform illumination
(prgb[R]>200 && prgb[G]>210 && prgb[B]>170 &&
abs(prgb[R]-prgb[B])<=15 && prgb[R]>prgb[B]&& prgb[G]>prgb[B])//lateral illumination
) {
memcpy(pdst,prgb,3);
}
prgb+=3;
pdst+=3;
}
}
cvCopyImage(dst,_dst);
cvReleaseImage(&dst);
}
void cvSkinRG(IplImage* rgb,IplImage* gray)
{
assert(rgb->nChannels==3&&gray->nChannels==1);
const int R=2;
const int G=1;
const int B=0;
double Aup=-1.8423;
基于 Caffe 的人脸识别系统 V1.0
8
double Bup=1.5294;
double Cup=0.0422;
double Adown=-0.7279;
double Bdown=0.6066;
double Cdown=0.1766;
for (int h=0; hheight; h++)
{
unsigned char* pGray=(unsigned char*)gray->imageData+h*gray->widthStep;
unsigned char* pRGB=(unsigned char* )rgb->imageData+h*rgb->widthStep;
for (int w=0; wwidth; w++)
{
int s=pRGB[R]+pRGB[G]+pRGB[B];
double r=(double)pRGB[R]/s;
double g=(double)pRGB[G]/s;
double Gup=Aup*r*r+Bup*r+Cup;
double Gdown=Adown*r*r+Bdown*r+Cdown;
double Wr=(r-0.33)*(r-0.33)+(g-0.33)*(g-0.33);
if (gGdown && Wr>0.004)
{
*pGray=255;
}
else
{
*pGray=0;
}
pGray++;
pRGB+=3;
}
}
}
void cvThresholdOtsu(IplImage* src, IplImage* dst)
{
int height=src->height;
int width=src->width;
//histogram
float histogram[256]= {0};
for(int i=0; iimageData+src->widthStep*i;
for(int j=0; j