C# OpenCV学习笔记七之图片相似比较

7 篇文章 1 订阅
7 篇文章 1 订阅
        private void Form1_Load(object sender, EventArgs e)
        {
            string result = MatchHist();
            MessageBox.Show(result);
        }

        //成对几何直方图匹配 
        private static string MatchHist()
        {
            string haarXmlPath = "C:\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt_tree.xml";
            HaarCascade haar1 = new HaarCascade(haarXmlPath);
            int[] hist_size = new int[1] { 256 };//建一个数组来存放直方图数据
            //准备轮廓 
            Image<Bgr, Byte> image1 = new Image<Bgr, byte>("D:\\Marvin1.jpg");
            Image<Bgr, Byte> image2 = new Image<Bgr, byte>("D:\\Marvin3.jpg");
            MCvAvgComp[] faces = haar1.Detect(image1.Convert<Gray, byte>(), 1.4, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
            MCvAvgComp[] faces2 = haar1.Detect(image2.Convert<Gray, byte>(), 1.4, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
            int l1 = faces.Length;
            int l2 = faces2.Length;
            image1 = image1.Copy(faces[0].rect);
            image2 = image2.Copy(faces2[0].rect);
            Image<Gray, Byte> imageGray1 = image1.Convert<Gray, Byte>();
            Image<Gray, Byte> imageGray2 = image2.Convert<Gray, Byte>();
            Image<Gray, Byte> imageThreshold1 = imageGray1.ThresholdBinaryInv(new Gray(128d), new Gray(255d));
            Image<Gray, Byte> imageThreshold2 = imageGray2.ThresholdBinaryInv(new Gray(128d), new Gray(255d));
            
            Contour<Point> contour1 = imageThreshold1.FindContours();
            Contour<Point> contour2 = imageThreshold2.FindContours();
            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);
            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;
            //Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD compareMethod = Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD.CV_COMP_CHISQR;
            //Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD compareMethod = Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD.CV_COMP_CORREL;
            Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD compareMethod = Emgu.CV.CvEnum.HISTOGRAM_COMP_METHOD.CV_COMP_INTERSECT;
            CvInvoke.cvNormalizeHist(HistImg1, 1d); //直方图对比方式 
            CvInvoke.cvNormalizeHist(HistImg2, 1d);

            CvInvoke.cvNamedWindow("img1");
            CvInvoke.cvShowImage("img1", image1);

            CvInvoke.cvNamedWindow("img2");
            CvInvoke.cvShowImage("img2", image2);

            compareResult = CvInvoke.cvCompareHist(HistImg1, HistImg2, compareMethod);
            sw.Stop();
            double time = sw.Elapsed.TotalMilliseconds;
            return string.Format("成对几何直方图匹配(匹配方式:{0}),结果:{1:F05},用时:{2:F05}毫秒\r\n", compareMethod.ToString("G"), compareResult, time);

        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值