資源簡介
OpenCV識別出兩張圖片的人臉,并比較出兩張人臉的相似度,并給出相似值。
包含完整的工程文件,可立即運行!??!
用facedetect功能將兩張圖片中的人臉檢測出來
將人臉部分的圖片剪切出來,存到兩張只有人臉的圖片里。
將這兩張人臉圖片轉換成單通道的圖像
使用直方圖比較這兩張單通道的人臉圖像,得出相似度。

代碼片段和文件信息
//?opencvtest.cpp?:?定義主控臺應用程式的進入點。
#include?“stdafx.h“
#include?“opencv\cv.hpp“??
#include?“opencv2\objdetect\objdetect.hpp“??
#include?“opencv2\highgui\highgui.hpp“??
#include?“opencv2\imgproc\imgproc.hpp“??
??
#include???
#include???
??
using?namespace?std;??
using?namespace?cv;??
?
String?cascadeName?=?“D:\\OpenCV\\data\\haarcascades\\haarcascade_frontalface_alt.xml“;??
?
IplImage*?cutImage(IplImage*?src?CvRect?rect)?{??
????cvSetImageROI(src?rect);??
????IplImage*?dst?=?cvCreateImage(cvSize(rect.width?rect.height)??
????????????src->depth??
????????????src->nChannels);??
??
????cvCopy(srcdst0);??
????cvResetImageROI(src);??
????return?dst;??
}??
??
IplImage*?detect(?Mat&?img?CascadeClassifier&?cascade?double?scale)??
{??
????int?i?=?0;??
????double?t?=?0;??
????vector?faces;??
????Mat?gray?smallImg(?cvRound?(img.rows/scale)?cvRound(img.cols/scale)?CV_8UC1?);??
??
????cvtColor(?img?gray?CV_BGR2GRAY?);??
????resize(?gray?smallImg?smallImg.size()?0?0?INTER_LINEAR?);??
????equalizeHist(?smallImg?smallImg?);??
??
????t?=?(double)cvGetTickCount();??
????cascade.detectMultiScale(?smallImg?faces??
????????1.3?2?CV_HAAR_SCALE_IMAGE??
????????Size(30?30)?);??
????t?=?(double)cvGetTickCount()?-?t;??
????printf(?“detection?time?=?%g?ms\n“?t/((double)cvGetTickFrequency()*1000.)?);??
????for(?vector::const_iterator?r?=?faces.begin();?r?!=?faces.end();?r++?i++?)??
????{??
????????IplImage*?temp?=?cutImage(&(IplImage(img))?cvRect(r->x?r->y?r->width?r->height));??
????????return?temp;??
????}??
??
????return?NULL;??
}??
//畫直方圖用??
int?HistogramBins?=?256;??
float?HistogramRange1[2]={0255};??
float?*HistogramRange[1]={&HistogramRange1[0]};??
int?CompareHist(IplImage*?image1?IplImage*?image2)??
{??
????IplImage*?srcImage;??
????IplImage*?targetImage;??
????if?(image1->nChannels?!=?1)?{??
????????srcImage?=?cvCreateImage(cvSize(image1->width?image1->height)?image1->depth?1);??
???????cvCvtColor(image1?srcImage?CV_BGR2GRAY);??
????}?else?{??
????????srcImage?=?image1;??
????}??
??
????if?(image2->nChannels?!=?1)?{??
????????targetImage?=?cvCreateImage(cvSize(image2->width?image2->height)?srcImage->depth?1);??
????????cvCvtColor(image2?targetImage?CV_BGR2GRAY);??
????}?else?{??
????????targetImage?=?image2;??
????}??
??
????CvHistogram?*Histogram1?=?cvCreateHist(1?&HistogramBins?CV_HIST_ARRAYHistogramRange);??
????CvHistogram?*Histogram2?=?cvCreateHist(1?&HistogramBins?CV_HIST_ARRAYHistogramRange);??
??
????cvCalcHist(&srcImage?Histogram1);??
????cvCalcHist(&targetImage?Histogram2);??
??
????cvNormalizeHist(Histogram1?1);??
????cvNormalizeHist(Histogram2?1);??
??
????//?CV_COMP_CHISQRCV_COMP_BHATTACHARYYA這兩種都可以用來做直方圖的比較,值越小,說明圖形越相似??
????//printf(“CV_COMP_CHISQR?:?%.4f\n“?cvCompareHist(Histogram1?Histogram2?CV_COMP_CHISQR));??
????//printf(“CV_COMP_BHATTACHARYYA?:?%.4f\n“?cvCompareHist(Histogra
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????74240??2013-03-28?14:20??opencvtest\Debug\opencvtest.exe
?????文件?????795740??2013-03-28?14:20??opencvtest\Debug\opencvtest.ilk
?????文件????1633280??2013-03-28?14:20??opencvtest\Debug\opencvtest.pdb
?????文件????2359296??2013-03-28?14:20??opencvtest\ipch\opencvtest-28dddb42\opencvtest-296a8017.ipch
?????文件???????1578??2013-03-28?14:20??opencvtest\opencvtest\Debug\cl.command.1.tlog
?????文件??????20286??2013-03-28?14:20??opencvtest\opencvtest\Debug\CL.read.1.tlog
?????文件????????982??2013-03-28?14:20??opencvtest\opencvtest\Debug\CL.write.1.tlog
?????文件??????????2??2013-03-28?14:20??opencvtest\opencvtest\Debug\li
?????文件??????????2??2013-03-28?14:20??opencvtest\opencvtest\Debug\li
?????文件??????????2??2013-03-28?14:20??opencvtest\opencvtest\Debug\li
?????文件??????????2??2013-03-28?14:20??opencvtest\opencvtest\Debug\li
?????文件??????????2??2013-03-28?14:20??opencvtest\opencvtest\Debug\li
?????文件??????????2??2013-03-28?14:20??opencvtest\opencvtest\Debug\li
?????文件???????2364??2013-03-28?14:20??opencvtest\opencvtest\Debug\li
?????文件???????4612??2013-03-28?14:20??opencvtest\opencvtest\Debug\li
?????文件???????1044??2013-03-28?14:20??opencvtest\opencvtest\Debug\li
?????文件????????424??2013-03-28?14:20??opencvtest\opencvtest\Debug\mt.command.1.tlog
?????文件????????374??2013-03-28?14:20??opencvtest\opencvtest\Debug\mt.read.1.tlog
?????文件????????374??2013-03-28?14:20??opencvtest\opencvtest\Debug\mt.write.1.tlog
?????文件????????406??2013-03-28?09:45??opencvtest\opencvtest\Debug\opencvtest.exe.em
?????文件????????472??2013-03-28?09:45??opencvtest\opencvtest\Debug\opencvtest.exe.em
?????文件????????381??2013-03-28?14:20??opencvtest\opencvtest\Debug\opencvtest.exe.intermediate.manifest
?????文件?????????78??2013-03-28?14:20??opencvtest\opencvtest\Debug\opencvtest.lastbuildstate
?????文件???????3009??2013-03-28?14:20??opencvtest\opencvtest\Debug\opencvtest.log
?????文件?????247772??2013-03-28?14:20??opencvtest\opencvtest\Debug\opencvtest.obj
?????文件????1179648??2013-03-28?08:45??opencvtest\opencvtest\Debug\opencvtest.pch
?????文件????????210??2013-03-28?09:45??opencvtest\opencvtest\Debug\opencvtest_manifest.rc
?????文件????????602??2013-03-28?09:45??opencvtest\opencvtest\Debug\rc.command.1.tlog
?????文件????????346??2013-03-28?09:45??opencvtest\opencvtest\Debug\rc.read.1.tlog
?????文件????????354??2013-03-28?09:45??opencvtest\opencvtest\Debug\rc.write.1.tlog
............此處省略23個文件信息
- 上一篇:離散數學第二版課后習題答案
- 下一篇:EDAverilog課程設計報告
評論
共有 條評論