static double Compare(string str1, string str2, HistCompMethods method) { Mat mat1 = new Mat(str1, ImreadModes.AnyColor); Mat mat2 = new Mat(@str1, ImreadModes.AnyColor); Mat mat3 = new Mat(); var value = Cv2.CompareHist(mat1, mat2, method); Console.WriteLine(new FileInfo(str1).Name + "-" + new FileInfo(str2).Name + ":" + value); return(value); }
/// <summary> /// compares two histograms stored in dense arrays /// </summary> /// <param name="h1">The first compared histogram</param> /// <param name="h2">The second compared histogram of the same size as h1</param> /// <param name="method">The comparison method</param> /// <returns></returns> public static double CompareHist(InputArray h1, InputArray h2, HistCompMethods method) { if (h1 == null) throw new ArgumentNullException(nameof(h1)); if (h2 == null) throw new ArgumentNullException(nameof(h2)); h1.ThrowIfDisposed(); h2.ThrowIfDisposed(); var ret = NativeMethods.imgproc_compareHist1(h1.CvPtr, h2.CvPtr, (int)method); GC.KeepAlive(h1); GC.KeepAlive(h2); return ret; }
private static void HistogarmToComparison(string path1, string path2, string path3) { using (Mat baseMat = new Mat(path1, ImreadModes.AnyColor | ImreadModes.AnyDepth)) using (Mat src1 = new Mat(path2, ImreadModes.AnyColor | ImreadModes.AnyDepth)) using (Mat src2 = new Mat(path3, ImreadModes.AnyColor | ImreadModes.AnyDepth)) { //1:从BGR空间转换到HSV色彩空间 Mat baseHSV = new Mat(); Mat src1HSV = new Mat(); Mat src2HSV = new Mat(); Cv2.CvtColor(baseMat, baseHSV, ColorConversionCodes.BGR2HSV); Cv2.CvtColor(src1, src1HSV, ColorConversionCodes.BGR2HSV); Cv2.CvtColor(src2, src2HSV, ColorConversionCodes.BGR2HSV); //Mat[] mats = new Mat[] { baseHSV, src1HSV, src2HSV }; Mat[] mats1 = Cv2.Split(baseHSV); Mat[] mats2 = Cv2.Split(src1HSV); Mat[] mats3 = Cv2.Split(src2HSV); //计算直方图并归一化数据 int bin1 = 50; //灰度等级 int bin2 = 60; //灰度等级 int[] histSiz = { bin1, bin2 }; int[] channels = { 0, 1 }; //图像通道数 Rangef[] rangefs = new Rangef[] //梯度值范围 { new Rangef(0, 180), new Rangef(0, 256) }; Mat Img1 = new Mat(baseMat.Size(), MatType.CV_32FC2); Mat Img2 = new Mat(src1.Size(), MatType.CV_32FC2); Mat Img3 = new Mat(src2.Size(), MatType.CV_32FC2); HistCompMethods method = HistCompMethods.KLDiv; Cv2.CalcHist(mats1, channels, new Mat(), Img1, 2, histSiz, rangefs, true, false); Cv2.Normalize(Img1, Img1, 0, 1, NormTypes.MinMax, -1, null); Cv2.CalcHist(mats2, channels, new Mat(), Img2, 2, histSiz, rangefs, true, false); Cv2.Normalize(Img2, Img2, 0, 1, NormTypes.MinMax, -1, null); Cv2.CalcHist(mats3, channels, new Mat(), Img3, 2, histSiz, rangefs, true, false); Cv2.Normalize(Img3, Img3, 0, 1, NormTypes.MinMax, -1, null); //比较直方图 double bcomb = Cv2.CompareHist(Img1, Img1, method); double s1coms2 = Cv2.CompareHist(Img2, Img3, method); double bcoms1 = Cv2.CompareHist(Img1, Img2, method); double bcoms2 = Cv2.CompareHist(Img1, Img3, method); Mat mats1Ands2 = new Mat(); src2.CopyTo(mats1Ands2); Cv2.PutText(baseMat, bcomb.ToString(), new Point(30, 30), HersheyFonts.HersheyComplex, 1, new Scalar(0, 255, 0), 2, LineTypes.AntiAlias); Cv2.PutText(src1, bcoms1.ToString(), new Point(30, 30), HersheyFonts.HersheyComplex, 1, new Scalar(0, 255, 0), 2, LineTypes.AntiAlias); Cv2.PutText(src2, bcoms2.ToString(), new Point(30, 30), HersheyFonts.HersheyComplex, 1, new Scalar(0, 255, 0), 2, LineTypes.AntiAlias); Cv2.PutText(mats1Ands2, s1coms2.ToString(), new Point(30, 30), HersheyFonts.HersheyComplex, 1, new Scalar(0, 255, 0), 2, LineTypes.AntiAlias); using (new Window("Img1 :Img1", WindowMode.Normal, baseMat)) using (new Window("Img1 :Img2", WindowMode.Normal, src1)) using (new Window("Img1 :Img3", WindowMode.Normal, src2)) { Cv2.WaitKey(0); } } }
/// <summary> /// 通过直方图比较 /// </summary> /// <param name="sourceImage"></param> /// <param name="targetImage"></param> /// <param name="method"></param> /// <returns></returns> public static double CompareImageByHistogram(string sourceImage, string targetImage, HistCompMethods method = HistCompMethods.KLDiv) { using (Mat source = new Mat(sourceImage, ImreadModes.AnyColor | ImreadModes.AnyDepth)) using (Mat target = new Mat(targetImage, ImreadModes.AnyColor | ImreadModes.AnyDepth)) { //1:从BGR空间转换到HSV色彩空间 Mat src1HSV = new Mat(); Mat src2HSV = new Mat(); Cv2.CvtColor(source, src1HSV, ColorConversionCodes.BGR2HSV); Cv2.CvtColor(target, src2HSV, ColorConversionCodes.BGR2HSV); //Mat[] mats = new Mat[] { baseHSV, src1HSV, src2HSV }; Mat[] mats2 = Cv2.Split(src1HSV); Mat[] mats3 = Cv2.Split(src2HSV); //计算直方图并归一化数据 int bin1 = 50; //灰度等级 int bin2 = 60; //灰度等级 int[] histSiz = { bin1, bin2 }; int[] channels = { 0, 1 }; //图像通道数 Rangef[] rangefs = new Rangef[] //梯度值范围 { new Rangef(0, 180), new Rangef(0, 256) }; Mat Img2 = new Mat(source.Size(), MatType.CV_32FC2); Mat Img3 = new Mat(target.Size(), MatType.CV_32FC2); Cv2.CalcHist(mats2, channels, new Mat(), Img2, 2, histSiz, rangefs, true, false); Cv2.Normalize(Img2, Img2, 0, 1, NormTypes.MinMax, -1, null); Cv2.CalcHist(mats3, channels, new Mat(), Img3, 2, histSiz, rangefs, true, false); Cv2.Normalize(Img3, Img3, 0, 1, NormTypes.MinMax, -1, null); //比较直方图 double s1coms2 = Cv2.CompareHist(Img2, Img3, method); return(s1coms2); } }