#include "opencv/cv.hpp" #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include " Opencv2/imgproc/imgproc.hpp "#include <iostream> #include <stdio.h>using namespace std;using namespace CV; String cascadename = "D:\\opencv-2.4.2\\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 (src,dst,0); Cvresetimageroi (SRC); return DST;} iplimage* Detect (mat& img, cascadeclassifier& cascade, double scale) {int i = 0; Double T = 0; Vector<rect> 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<rect>::const_iterator r = Faces.begin (); R! = Faces.end (); r++, i++) {iplimage* temp = Cutim Age (& (Iplimage (IMG)), Cvrect (R->x, R->y, R->width, r->height)); return temp; } return NULL; Draw histogram with int histogrambins = 256;float histogramrange1[2]={0,255};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->d Epth, 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_array,histogramrange); Cvhistogram *histogram2 = cvcreatehist (1, &histogrambins, cv_hist_array,histogramrange); Cvcalchist (&srcimage, Histogram1); Cvcalchist (&targetimage, Histogram2); Cvnormalizehist (Histogram1, 1); Cvnormalizehist (HISTOGRAM2, 1); Cv_comp_chisqr,cv_comp_bhattacharyya These two can be used to do a histogram comparison, the smaller the value, the more similar to the graph of printf ("CV_COMP_CHISQR:%.4f\n", Cvcomparehist ( HISTOGRAM1, Histogram2, CV_COMP_CHISQR)); printf ("Cv_comp_bhattacharyya:%.4f\n", Cvcomparehist (Histogram1, Histogram2, Cv_comp_bhattacharyya)); Cv_comp_correl, Cv_comp_intersect The comparison of the two histograms, the larger the value, the more similar the graph to printf ("Cv_comp_correl:%.4f\n", Cvcomparehist (Histogram1 , Histogram2, Cv_comp_correl)); printf ("Cv_comp_intersect:%.4f\n", Cvcomparehist (Histogram1, HISTOGRAM2, cv_comp_iNtersect)); Cvreleasehist (&HISTOGRAM1); Cvreleasehist (&HISTOGRAM2); if (image1->nchannels! = 1) {cvreleaseimage (&srcimage); } if (Image2->nchannels! = 1) {cvreleaseimage (&targetimage); } return 0;} String srcimage = "D:\\ldh1.jpg"; String targetimage = "d:\\ldh5.jpg"; int main (int argc, char* argv[]) {cascadeclassifier cascade; Namedwindow ("Image1"); Namedwindow ("Image2"); if (!cascade.load (Cascadename)) {return-1; } Mat srcimg, targetimg; Iplimage* FaceImage1; Iplimage* FaceImage2; srcimg = Imread (srcimage); targetimg = Imread (targetimage); FaceImage1 = Detect (SRCIMG, cascade, 1); if (FaceImage1 = = NULL) {return-1; }//cvsaveimage ("D:\\face.jpg", FaceImage1, 0); FaceImage2 = Detect (TARGETIMG, cascade, 1); if (FaceImage2 = = NULL) {return-1; }//cvsaveimage ("D:\\face1.jpg", FaceImage2, 0); Imshow ("Image1", Mat (FaceImage1)); Imshow ("Image2", Mat (FaceImage2)); Comparehist (FaceImage1, faceImage2); Cvwaitkey (0); Cvreleaseimage (&FACEIMAGE1); Cvreleaseimage (&faceimage2); return 0;}
The comparison of the CV histogram shows that the graph is more similar