在emgucv中查找轮廓点 [英] Finding contour points in emgucv

查看:605
本文介绍了在emgucv中查找轮廓点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与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. 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

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.

这篇关于在emgucv中查找轮廓点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆