//成对几何直方图匹配
public static string MatchHist()
{
string haarXmlPath = @"haarcascade_frontalface_alt_tree.xml";
HaarCascade haar = new HaarCascade(haarXmlPath);
int[] hist_size = new int[1] { 256 };//建一个数组来存放直方图数据
//IntPtr img1 = CvInvoke.cvLoadImage("", Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR); //根据路径导入图像
//准备轮廓
Image
Image
MCvAvgComp[] faces = haar.Detect(image1.Convert
MCvAvgComp[] faces2 = haar.Detect(image2.Convert
int l1 = faces.Length;
int l2 = faces2.Length;
image1 = image1.Copy(faces[0].rect);
image2 = image2.Copy(faces2[0].rect);
Image
Image
Image
Image
//Contour
Contour
Contour
IntPtr HistImg1 = CvInvoke.cvCreateHist(1, hist_size, Emgu.CV.CvEnum.HIST_TYPE.CV_HIST_ARRAY, null, 1); //创建一个空的直方图
IntPtr HistImg2 = CvInvoke.cvCreateHist(1, hist_size, Emgu.CV.CvEnum.HIST_TYPE.CV_HIST_ARRAY, null, 1);
//CvInvoke.cvHaarDetectObjects();
IntPtr[] inPtr1 = new IntPtr[1] { imageThreshold1 };
IntPtr[] inPtr2 = new IntPtr[1] { imageThreshold2 };
CvInvoke.cvCalcHist(inPtr1, HistImg1, false, IntPtr.Zero); //计算inPtr1指向图像的数据,并传入HistImg1中
CvInvoke.cvCalcHist(inPtr2, HistImg2, false, IntPtr.Zero);
Stopwatch sw = new Stopwatch();
sw.Start();
double compareResult;
Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD compareMethod = Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD.CV_COMP_BHATTACHARYYA;
CvInvoke.cvNormalizeHist(HistImg1, 1d); //直方图对比方式
CvInvoke.cvNormalizeHist(HistImg2, 1d);
compareResult = CvInvoke.cvCompareHist(HistImg1, HistImg2, compareMethod);
//compareResult = CvInvoke.cvMatchShapes(HistImg1, HistImg2, Emgu.CV.CvEnum.CONTOURS_MATCH_TYPE.CV_CONTOURS_MATCH_I3, 1d);
sw.Stop();
double time = sw.Elapsed.TotalMilliseconds;
return string.Format("成对几何直方图匹配(匹配方式:{0}),结果:{1:F05},用时:{2:F05}毫秒\r\n", compareMethod.ToString("G"), compareResult, time);
}