//开始匹配
private void btnStartMatch_Click(object sender, EventArgs e)
{
//准备轮廓(这里只比较最外围的轮廓)
Image<Bgr, Byte> image1 = new Image<Bgr, byte>((Bitmap)pbImage1.Image);
Image<Bgr, Byte> image2 = new Image<Bgr, byte>((Bitmap)pbImage2.Image);
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(CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, RETR_TYPE.CV_RETR_EXTERNAL);
Contour<Point> contour2 = imageThreshold2.FindContours(CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, RETR_TYPE.CV_RETR_EXTERNAL);
/*if (contour1.Perimeter / 50 > 2 && contour2.Perimeter / 50 > 2)
{
contour1 = contour1.ApproxPoly(contour1.Perimeter / 50, 2, new MemStorage()); //对轮廓进行多边形逼近(参数设为轮廓周长的1/50)
contour2 = contour2.ApproxPoly(contour2.Perimeter / 50, 2, new MemStorage());
}*/
//进行匹配
string result = "";
if (rbHuMoments.Checked)
result = MatchShapes(contour1, contour2); //Hu矩匹配
else if (rbContourTree.Checked)
result = MatchContourTrees(contour1, contour2); //轮廓树匹配
else if (rbPGH.Checked)
result = MatchPghHist(contour1, contour2); //成对几何直方图匹配
txtResult.Text += result;
}
//Hu矩匹配
private string MatchShapes(Contour<Point> contour1, Contour<Point> contour2)
{
//匹配方法
CONTOURS_MATCH_TYPE matchType = rbHuI1.Checked ? CONTOURS_MATCH_TYPE.CV_CONTOUR_MATCH_I1 : (rbHuI2.Checked ? CONTOURS_MATCH_TYPE.CV_CONTOURS_MATCH_I2 : CONTOURS_MATCH_TYPE.CV_CONTOURS_MATCH_I3);
Stopwatch sw = new Stopwatch();
sw.Start();
//匹配
double matchValue = contour1.MatchShapes(contour2, matchType);
sw.Stop();
double time = sw.Elapsed.TotalMilliseconds;
return string.Format("Hu矩匹配({0:G}),结果:{1:F05},用时:{2:F05}毫秒\r\n", matchType, matchValue, time);
}
//轮廓树匹配
private string MatchContourTrees(Contour<Point> contour1, Contour<Point> contour2)
{
//生成轮廓树
double thresholdOfCreate = double.Parse(txtThresholdOfCreateContourTrees.Text); //生成轮廓树的阀值
IntPtr ptrTree1 = CvInvoke.cvCreateContourTree(contour1.Ptr, new MemStorage().Ptr, thresholdOfCreate);
IntPtr ptrTree2 = CvInvoke.cvCreateContourTree(contour2.Ptr, new MemStorage().Ptr, thresholdOfCreate);
//匹配
double thresholdOfMatch = double.Parse(txtThresholdOfMatchContourTrees.Text); //比较轮廓树的阀值
Stopwatch sw = new Stopwatch();
sw.Start();
double matchValue = CvInvoke.cvMatchContourTrees(ptrTree1, ptrTree2, MATCH_CONTOUR_TREE_METHOD.CONTOUR_TREES_MATCH_I1, thresholdOfMatch);
sw.Stop();
double time = sw.Elapsed.TotalMilliseconds;
return string.Format("轮廓树匹配(生成轮廓树的阀值:{0},比较轮廓树的阀值:{1}),结果:{2:F05},用时:{3:F05}毫秒\r\n", thresholdOfCreate, thresholdOfMatch, matchValue, time);
}
//成对几何直方图匹配
private string MatchPghHist(Contour<Point> contour1, Contour<Point> contour2)
{
//生成成对几何直方图
Rectangle rect1 = contour1.BoundingRectangle;
float maxDist1 = (float)Math.Sqrt(rect1.Width * rect1.Width + rect1.Height * rect1.Height); //轮廓的最大距离:这里使用轮廓矩形边界框的对角线长度
int[] bins1 = new int[] { 60, 20 };
RangeF[] ranges1 = new RangeF[] { new RangeF(0f, 180f), new RangeF(0f, maxDist1) }; //直方图第0维为角度,范围在(0,180),第2维为轮廓两条边缘线段的距离
DenseHistogram hist1 = new DenseHistogram(bins1, ranges1);
CvInvoke.cvCalcPGH(contour1.Ptr, hist1.Ptr);
Rectangle rect2 = contour2.BoundingRectangle;
float maxDist2 = (float)Math.Sqrt(rect2.Width * rect2.Width + rect2.Height * rect2.Height);
int[] bins2 = new int[] { 60, 20 };
RangeF[] ranges2 = new RangeF[] { new RangeF(0f, 180f), new RangeF(0f, maxDist2) };
DenseHistogram hist2 = new DenseHistogram(bins2, ranges2);
CvInvoke.cvCalcPGH(contour2.Ptr, hist2.Ptr);
//匹配
Stopwatch sw = new Stopwatch();
sw.Start();
double compareResult;
HISTOGRAM_COMP_METHOD compareMethod = rbHistCorrel.Checked ? HISTOGRAM_COMP_METHOD.CV_COMP_CORREL : (rbHistChisqr.Checked ? HISTOGRAM_COMP_METHOD.CV_COMP_CHISQR : (rbHistIntersect.Checked ? HISTOGRAM_COMP_METHOD.CV_COMP_INTERSECT : HISTOGRAM_COMP_METHOD.CV_COMP_BHATTACHARYYA));
if (rbHistEmd.Checked)
{
//EMD
//将直方图转换成矩阵
Matrix<Single> matrix1 = FormProcessHist.ConvertDenseHistogramToMatrix(hist1);
Matrix<Single> matrix2 = FormProcessHist.ConvertDenseHistogramToMatrix(hist2);
compareResult = CvInvoke.cvCalcEMD2(matrix1.Ptr, matrix2.Ptr, DIST_TYPE.CV_DIST_L2, null, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
matrix1.Dispose();
matrix2.Dispose();
}
else
{
//直方图对比方式
hist1.Normalize(1d);
hist2.Normalize(1d);
compareResult = CvInvoke.cvCompareHist(hist1.Ptr, hist2.Ptr, compareMethod);
}
sw.Stop();
double time = sw.Elapsed.TotalMilliseconds;
return string.Format("成对几何直方图匹配(匹配方式:{0}),结果:{1:F05},用时:{2:F05}毫秒\r\n", rbHistEmd.Checked ? "EMD" : compareMethod.ToString("G"), compareResult, time);
}