从候选轮廓列表中选择轮廓

时间:2014-07-23 14:01:17

标签: c++ opencv image-processing contour

在opencv轮廓检测中,我得到的是彼此接近的多个轮廓。在这个图像中,我必须从每组最接近的轮廓中选择一个轮廓。 如何为每组选择轮廓。  我知道扩张形象,已经完成,但对我来说效果不佳 看附图有四个轮廓。我需要从中选择两个,每组一个。 如何实现这一点。select a contours from group of possibles

1 个答案:

答案 0 :(得分:0)

如果您有源图像like this&您想要找到董事会from the Image。您可以遵循此伪代码。您可以轻松地在c ++中实现相同的功能。 请注意,我在这里使用CV_RETR_EXTERNAL参数来检索最外面的轮廓。

Image<Bgr, byte> Img_Result_Bgr = Img_Source_Bgr.Clone();
Image<Gray, byte> Img_Org_Gray = Img_Source_Bgr.Convert<Gray, byte>();
Img_Org_Gray = Img_Org_Gray.ThresholdBinary(new Gray(250), new Gray(255));

#region Finding Contours
using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation
    for (Contour<Point> contours = Img_Org_Gray.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_EXTERNAL, storage); contours != null; contours = contours.HNext)
    {
        Contour<Point> contours_Approximated = contours.ApproxPoly(contours.Perimeter * 0.005,storage);
        if (contours.Area > 10) //only consider contours with area greater than 100
        {
            Img_Result_Bgr.Draw(contours_Approximated, new Bgr(Color.Red), 2);
        }
    }
#endregion
imageBox1.Image = Img_Result_Bgr;