打开CV Hough圆检测计数缺少一些对象

时间:2016-07-09 14:43:53

标签: c# winforms opencv

我在我的(C#Winforms)项目中使用OpenCV来检测Circles。

我的代码未检测到图像中的所有圆圈。这是我的代码:

using OpenCV;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace DemoOpenCV
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(@"./IMG/1.jpg");
            pictureBox2.Image = Image.FromFile(@"./IMG/3.jpg");
        }
        private void startCircleDetectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.startDetection(pictureBox1.Image, CamEnum.Cam1);
            this.startDetection(pictureBox2.Image, CamEnum.Cam2);
        }

        private void startDetection(Image inputImage, CamEnum _camName)
        {
            try
            {
                if (inputImage == null)
                {
                    return;
                }

                Image image = (Image)inputImage.Clone();

                CvSize size = new CvSize(image.Width, image.Height);
                IplImage dst = cvlib.CvCreateImage(size, 8, 3);

                if (image.PixelFormat == PixelFormat.Format24bppRgb)
                {
                    dst = cvlib.ToIplImage((Bitmap)image, false);
                }
                else if (image.PixelFormat == PixelFormat.Format8bppIndexed)
                {
                    cvlib.CvCreateImage(size, 8, 1);
                    IplImage iplImage = cvlib.ToIplImage((Bitmap)image, false);
                    cvlib.CvCvtColor(ref iplImage, ref dst, 8);
                }
                else
                {
                    // throw not support format ex;
                }
                houghCircleDetection((object)dst, _camName);

            }
            catch (Exception ex)
            {

            }
        }

        private void houghCircleDetection(object img, CamEnum camName)
        {
            try
            {
                IplImage image1 = (IplImage)img;
                CvSize size = cvlib.CvGetSize(ref image1);
                IplImage image2 = cvlib.CvCreateImage(size, 8, 3);
                IplImage image3 = cvlib.CvCreateImage(size, 8, 1);
                IplImage pImage = this.preprocessImage(image1, camName);
                cvlib.CvCanny(ref pImage, ref image3, Math.Max(42 / 2.0, 1.0), 42, 3);
                CvSeq pCircles = HoughAlgorithm.DetectCircles(pImage, camName);
                cvlib.CvCopy(ref image1, ref image2);
                HoughAlgorithm.DrawCircles(image2, pCircles);
                if (camName == CamEnum.Cam1)
                {
                    pictureBox_Result1.Image = (Image)cvlib.ToBitmap(image2, false);
                    toolStripMenuItem_Result1.Text = pCircles.total.ToString();
                }
                else
                {
                    pictureBox_Result2.Image = (Image)cvlib.ToBitmap(image2, false);
                    toolStripMenuItem_Result2.Text = pCircles.total.ToString();
                }
            }
            catch (Exception ex)
            {

            }
        }

        private IplImage preprocessImage(IplImage image, CamEnum _camName)
        {
            IplImage iplImage = new IplImage();
            try
            {
                iplImage = cvlib.CvCreateImage(cvlib.CvGetSize(ref image), 8, 1);
                cvlib.CvCvtColor(ref image, ref iplImage, 6);
            }
            catch (Exception ex)
            {

            }
            return iplImage;
        }


    }

    public enum CamEnum
    {
        Cam1,
        Cam2
    }
}

其他课程:

using OpenCV;
using System;
using System.Runtime.InteropServices;

namespace DemoOpenCV
{
    public sealed class HoughAlgorithm
    {

        private HoughAlgorithm()
        {
        }

        public static void DrawCircles(IplImage pImage, CvSeq pCircles)
        {
            try
            {
                int num = pCircles.total;
                for (int index = 0; index < num; ++index)
                {
                    float[] destination = new float[3];
                    Marshal.Copy(cvlib.CvGetSeqElem(ref pCircles, index), destination, 0, 3);
                    cvlib.CvCircle(ref pImage, cvlib.CvPoint((int)Math.Round((double)destination[0]), (int)Math.Round((double)destination[1])), 3, cvlib.CV_RGB(0, (int)byte.MaxValue, 0), -1, 8, 0);
                    cvlib.CvCircle(ref pImage, cvlib.CvPoint((int)Math.Round((double)destination[0]), (int)Math.Round((double)destination[1])), (int)Math.Round((double)destination[2]), cvlib.CV_RGB((int)byte.MaxValue, 0, 0), 3, 8, 0);
                }
            }
            catch
            {
                throw;
            }
        }


        public static CvSeq DetectCircles(IplImage pImage, CamEnum _camName)
        {
            try
            {
                CvMemStorage memStorage = cvlib.CvCreateMemStorage(0);
                if (_camName == CamEnum.Cam1)
                {
                    return cvlib.CvHoughCircles(ref pImage, memStorage.ptr, 3, 1, 150, 42, 36, 45, 65); // this parameter for best result
                }
                else
                {
                    return cvlib.CvHoughCircles(ref pImage, memStorage.ptr, 3, 1, 150, 42, 36, 45, 65);
                }
            }
            catch
            {
                throw;
            }
        }
    }

    public struct HoughDetectionResult
    {
        public int count;
        public int[] xCoordinates;
        public int[] yCoordinates;
        public int[] radii;
    }

}

完整的演示项目:here

这是我的结果:

My result

0 个答案:

没有答案