c#emgu显示最大的轮廓

时间:2018-03-20 09:58:27

标签: c# visual-studio opencv emgucv

我正在尝试获取精确图像的最大边界矩形。我认为它有效,但我不知道如何可视化矩形。最后我想得到矩形的右边和右下坐标,但我认为当我能够将图像导入我的虚空时,我也可以从图像中获取这些值。我是新手,所以我正在学习,阅读和询问。如果我只是需要研究一些理论,请赐教!愿意学习。

当我运行此代码时,我的控制台输出是:

Emgu.CV.Util.VectorOfPoint

代码是这样的:

    private void CannyFrame(object sender, EventArgs e)
    {
        if (_capture != null && _capture.Ptr != IntPtr.Zero)
        {
            _capture.Retrieve(imgOriginal, 0);
            CvInvoke.CvtColor(imgOriginal, imgHSV, ColorConversion.Bgr2Hsv); //Convert the captured frame from BGR to HSV
            CvInvoke.InRange(imgHSV, new ScalarArray(new MCvScalar(iLowH, iLowS, iLowV)), new ScalarArray(new MCvScalar(iHighH, iHighS, iHighV)), imgThres);
            CvInvoke.Canny(imgThres, imgCanny, 100, 200, 3);
            Form1.FindLargestContour(imgCanny, imgContour);
            pictureBox3.Image = imgContour.Bitmap;
        }
    }

    public static VectorOfPoint FindLargestContour(IInputOutputArray imgCanny, IInputOutputArray imgContour)
    {
        int largest_contour_index = 0;
        double largest_area = 0;
        VectorOfPoint largestContour;
        using (Mat hierachy = new Mat())
        using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
        {
            IOutputArray hierarchy;
            CvInvoke.FindContours(imgCanny, contours, hierachy, RetrType.Tree, ChainApproxMethod.ChainApproxSimple);
            for (int i = 0; i < contours.Size; i++)
            {
                MCvScalar color = new MCvScalar(0, 0, 255);
                double a = CvInvoke.ContourArea(contours[i], false);  //  Find the area of contour
                if (a > largest_area)
                {
                    largest_area = a;
                    largest_contour_index = i;                //Store the index of largest contour
                }
                CvInvoke.DrawContours(imgContour, contours, largest_contour_index, new MCvScalar(255, 0, 0));
            }
            CvInvoke.DrawContours(imgContour, contours, largest_contour_index, new MCvScalar(0, 0, 255), 3, LineType.EightConnected, hierachy);
            largestContour = new VectorOfPoint(contours[largest_contour_index].ToArray());
        }
        Console.WriteLine(largestContour);
        return largestContour;
    }

1 个答案:

答案 0 :(得分:0)

我通过制作

解决了我的问题
public static VectorOfPoint FindLargestContour(IInputOutputArray imgCanny, IInputOutputArray imgContour)

进入

private static void FindLargestContour(IInputOutputArray imgCanny, IInputOutputArray imgContour)

在我删除的空白中

return largestContour;

不确定是否可行,但我的代码还是这样。

相关问题