简体   繁体   English

在emgucv中查找轮廓点

[英]Finding contour points in emgucv

I am working with emguCV for finding contours essential points then saving this point in a file and user redraw this shape in future.我正在使用 emguCV 查找轮廓要点,然后将此点保存在文件中,并且用户将来重绘此形状。 so, my goal is this image:所以,我的目标是这张图片:

example例子
例子

my solution is this: 1. import image to picturebox 2. edge detection with canny algorithm 3. finding contours and save points我的解决方案是这样的: 1. 将图像导入到图片框 2. 使用精明算法进行边缘检测 3. 查找轮廓并保存点

I found a lot of points with below codes but i can't drawing first shape with this point!我在下面的代码中找到了很多点,但我无法用这一点绘制第一个形状!

using Emgu.CV;
using Emgu.Util;

private void button1_Click(object sender, EventArgs e)          
{
    Bitmap bmp = new Bitmap(pictureBox1.Image);
    Image<Bgr, Byte> img = new Image<Bgr, byte>(bmp);

    Image<Gray, Byte> gray = img.Convert<Gray, Byte>().PyrDown().PyrUp();

    Gray cannyThreshold = new Gray(80);
    Gray cannyThresholdLinking = new Gray(120);
    Gray circleAccumulatorThreshold = new Gray(120);

    Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold, cannyThresholdLinking).Not();

    Bitmap color;
    Bitmap bgray;
    IdentifyContours(cannyEdges.Bitmap, 50, true, out bgray, out color);

    pictureBox1.Image = color;
}

public void IdentifyContours(Bitmap colorImage, int thresholdValue, bool invert, out Bitmap processedGray, out Bitmap processedColor)
{
    Image<Gray, byte> grayImage = new Image<Gray, byte>(colorImage);
    Image<Bgr, byte> color = new Image<Bgr, byte>(colorImage);

    grayImage = grayImage.ThresholdBinary(new Gray(thresholdValue), new Gray(255));

    if (invert)
    {
        grayImage._Not();
    }

    using (MemStorage storage = new MemStorage())
    {
        for (Contour<Point> contours = grayImage.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage); contours != null; contours = contours.HNext)
        {
            Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.015, storage);
            if (currentContour.BoundingRectangle.Width > 20)
            {
                CvInvoke.cvDrawContours(color, contours, new MCvScalar(255), new MCvScalar(255), -1, 1, Emgu.CV.CvEnum.LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));
                color.Draw(currentContour.BoundingRectangle, new Bgr(0, 255, 0), 1);
            }

            Point[] pts = currentContour.ToArray();
            foreach (Point p in pts)
            {
                //add points to listbox
                listBox1.Items.Add(p);
            }
       }
  }

   processedColor = color.ToBitmap();
   processedGray = grayImage.ToBitmap();
}

In your code you have added contour approximation operation在您的代码中,您添加了轮廓近似操作

Contour<Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.015, storage);

This contour approximation will approximate your Contour to a nearest polygon & so your actual points got shifted.这种轮廓近似会将您的轮廓近似最近的多边形,因此您的实际点会发生偏移。 If you want to reproduce the same image you need not to do any approximation.如果要重现相同的图像,则无需进行任何近似处理。

Refer this thread. 参考这个线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM