本文主要是介绍【Emgu CV教程】10.13、利用形状场景算法比较轮廓,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 一、演示
- 1.原始素材
- 2.代码
- 3.运行结果
形状场景算法比较轮廓,要引用Emgu.CV.Shape,定义检测器ShapeContextDistanceExtractor,然后对两个轮廓进行比较,得出结果。
一、演示
1.原始素材
原始图像srcMat1如下图:
待比较的图像srcMat2如下图:
2.代码
轮廓检索模式要选择RetrType.External,代码如下:
Mat tempMat1 = srcMat1.Clone();
Mat tempMat2 = srcMat2.Clone();
Mat gray1 = new Mat();
Mat gray2 = new Mat();// 二值化图片1,确保黑里面找白
CvInvoke.CvtColor(tempMat1, gray1, ColorConversion.Bgr2Gray);
CvInvoke.Threshold(gray1, gray1, 50, 255, ThresholdType.Binary);
VectorOfVectorOfPoint contours1 = new VectorOfVectorOfPoint();
VectorOfRect hierarchy1 = new VectorOfRect();
CvInvoke.FindContours(gray1, contours1, hierarchy1, RetrType.External, ChainApproxMethod.ChainApproxNone);
Mat contourMat1 = new Mat(new System.Drawing.Size(gray1.Cols, gray1.Rows), DepthType.Cv8U, 1);
contourMat1.SetTo(new MCvScalar(0, 0, 0));// 按照面积筛选,太小的轮廓不计算
Dictionary<int, double> dict1 = new Dictionary<int, double>();
if (contours1.Size > 0)
{for (int i = 0; i < contours1.Size; i++){double area = CvInvoke.ContourArea(contours1[i]);Rectangle rect = CvInvoke.BoundingRectangle(contours1[i]);if (rect.Width > 2 && rect.Height > 2 && area < 3000000){dict1.Add(i, area);}}
}// 二值化图片2,确保黑里面找白
CvInvoke.CvtColor(tempMat2, gray2, ColorConversion.Bgr2Gray);
CvInvoke.Threshold(gray2, gray2, 50, 255, ThresholdType.Binary);
VectorOfVectorOfPoint contours2 = new VectorOfVectorOfPoint();
VectorOfRect hierarchy2 = new VectorOfRect();
CvInvoke.FindContours(gray2, contours2, hierarchy2, RetrType.External, ChainApproxMethod.ChainApproxNone);
Mat contourMat2 = new Mat(new System.Drawing.Size(gray1.Cols, gray1.Rows), DepthType.Cv8U, 1);
contourMat2.SetTo(new MCvScalar(0, 0, 0));// 按照面积筛选,太小的轮廓不计算
Dictionary<int, double> dict2 = new Dictionary<int, double>();
if (contours2.Size > 0)
{for (int i = 0; i < contours2.Size; i++){double area = CvInvoke.ContourArea(contours2[i]);Rectangle rect = CvInvoke.BoundingRectangle(contours2[i]);if (rect.Width > 2 && rect.Height > 2 && area < 3000000){dict2.Add(i, area);}}
}// 排序,取最大的轮廓再判断
IOrderedEnumerable<KeyValuePair<int, double>> descendingDict1 = dict1.OrderByDescending(x => x.Value);
IOrderedEnumerable<KeyValuePair<int, double>> descendingDict2 = dict2.OrderByDescending(x => x.Value);
using (HistogramCostExtractor comparer = new ChiHistogramCostExtractor())
using (ThinPlateSplineShapeTransformer transformer = new ThinPlateSplineShapeTransformer())
using (ShapeContextDistanceExtractor extractor = new ShapeContextDistanceExtractor(comparer, transformer))
{CvInvoke.DrawContours(contourMat1, contours1, descendingDict1.First().Key, new MCvScalar(255, 255, 255), 1);CvInvoke.DrawContours(contourMat2, contours2, descendingDict2.First().Key, new MCvScalar(255, 255, 255), 1);float distance = extractor.ComputeDistance(contours1[descendingDict1.First().Key], contours2[descendingDict2.First().Key]);MessageBox.Show("形状场景距离是:" + distance);
}
3.运行结果
利用extractor.ComputeDistance()函数计算出的形状场景距离是:0.1476557
如果srcMat2采用和srcMat1相同的图形,计算出的形状场景距离是:0.000375
因此记住它的特点:使用“距离”作为形状比较的度量标准,距离越小相似度越高。
原创不易,请勿抄袭。共同进步,相互学习。
这篇关于【Emgu CV教程】10.13、利用形状场景算法比较轮廓的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!