DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

How to develop circle detection in C#

Leon Dunn user avatar by
Leon Dunn
·
Feb. 06, 15 · · Code Snippet
Like (0)
Save
Tweet
4.27K Views

Join the DZone community and get the full member experience.

Join For Free

After searching a lot in this topic, I finally found the perfect solution for circle detection. I used a prewritten C# code that I found on www.camera-sdk.com . To use this code here, you’ll need to have a Visual C# WPF Application that can be created in Visual Studio and you should add VOIPSDK.dll and NVA.dll (both can be found on the webpage) to the references.

Now, the first snippet under Form1.Designer.cs is the code for the GUI of the program. The GUI you can implement with this code will help you to be able to manage your program without any troubles.

The second snippet is the code for developing the circle detection function itself. The code contains all the details you need for your program.

I hope these codes will be useful for you all! Have fun! :)

// Form1.Designer.cs

namespace CircleDetection
{
    partial class Form1
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.btn_Set = new System.Windows.Forms.Button();
            this.tb_AccumulatorResolution = new System.Windows.Forms.TextBox();
            this.tb_CannyThreshold = new System.Windows.Forms.TextBox();
            this.tb_CircleAccumulatorThreshold = new System.Windows.Forms.TextBox();
            this.tb_MaximumRadius = new System.Windows.Forms.TextBox();
            this.tb_MinimumDistance = new System.Windows.Forms.TextBox();
            this.tb_MinimumRadius = new System.Windows.Forms.TextBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.label6 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.btn_HighlightSet = new System.Windows.Forms.Button();
            this.label11 = new System.Windows.Forms.Label();
            this.label10 = new System.Windows.Forms.Label();
            this.tb_DrawThickness = new System.Windows.Forms.TextBox();
            this.tb_Blue = new System.Windows.Forms.TextBox();
            this.chk_ShowImage = new System.Windows.Forms.CheckBox();
            this.tb_Green = new System.Windows.Forms.TextBox();
            this.tb_Red = new System.Windows.Forms.TextBox();
            this.lb_Detection = new System.Windows.Forms.ListBox();
            this.label12 = new System.Windows.Forms.Label();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.label1.Location = new System.Drawing.Point(30, 265);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(87, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Original image";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.label2.Location = new System.Drawing.Point(370, 265);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(103, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "Processed image";
            // 
            // btn_Set
            // 
            this.btn_Set.Location = new System.Drawing.Point(189, 368);
            this.btn_Set.Name = "btn_Set";
            this.btn_Set.Size = new System.Drawing.Size(58, 23);
            this.btn_Set.TabIndex = 2;
            this.btn_Set.Text = "Set";
            this.btn_Set.UseVisualStyleBackColor = true;
            this.btn_Set.Click += new System.EventHandler(this.btn_Set_Click);
            // 
            // tb_AccumulatorResolution
            // 
            this.tb_AccumulatorResolution.Location = new System.Drawing.Point(153, 26);
            this.tb_AccumulatorResolution.Name = "tb_AccumulatorResolution";
            this.tb_AccumulatorResolution.Size = new System.Drawing.Size(87, 20);
            this.tb_AccumulatorResolution.TabIndex = 3;
            // 
            // tb_CannyThreshold
            // 
            this.tb_CannyThreshold.Location = new System.Drawing.Point(153, 58);
            this.tb_CannyThreshold.Name = "tb_CannyThreshold";
            this.tb_CannyThreshold.Size = new System.Drawing.Size(87, 20);
            this.tb_CannyThreshold.TabIndex = 4;
            // 
            // tb_CircleAccumulatorThreshold
            // 
            this.tb_CircleAccumulatorThreshold.Location = new System.Drawing.Point(153, 90);
            this.tb_CircleAccumulatorThreshold.Name = "tb_CircleAccumulatorThreshold";
            this.tb_CircleAccumulatorThreshold.Size = new System.Drawing.Size(87, 20);
            this.tb_CircleAccumulatorThreshold.TabIndex = 5;
            // 
            // tb_MaximumRadius
            // 
            this.tb_MaximumRadius.Location = new System.Drawing.Point(153, 122);
            this.tb_MaximumRadius.Name = "tb_MaximumRadius";
            this.tb_MaximumRadius.Size = new System.Drawing.Size(87, 20);
            this.tb_MaximumRadius.TabIndex = 6;
            // 
            // tb_MinimumDistance
            // 
            this.tb_MinimumDistance.Location = new System.Drawing.Point(153, 154);
            this.tb_MinimumDistance.Name = "tb_MinimumDistance";
            this.tb_MinimumDistance.Size = new System.Drawing.Size(87, 20);
            this.tb_MinimumDistance.TabIndex = 7;
            // 
            // tb_MinimumRadius
            // 
            this.tb_MinimumRadius.Location = new System.Drawing.Point(153, 186);
            this.tb_MinimumRadius.Name = "tb_MinimumRadius";
            this.tb_MinimumRadius.Size = new System.Drawing.Size(87, 20);
            this.tb_MinimumRadius.TabIndex = 8;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.label6);
            this.groupBox1.Controls.Add(this.label5);
            this.groupBox1.Controls.Add(this.label8);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.label7);
            this.groupBox1.Controls.Add(this.tb_MaximumRadius);
            this.groupBox1.Controls.Add(this.btn_Set);
            this.groupBox1.Controls.Add(this.tb_AccumulatorResolution);
            this.groupBox1.Controls.Add(this.tb_MinimumDistance);
            this.groupBox1.Controls.Add(this.tb_MinimumRadius);
            this.groupBox1.Controls.Add(this.tb_CannyThreshold);
            this.groupBox1.Controls.Add(this.tb_CircleAccumulatorThreshold);
            this.groupBox1.Location = new System.Drawing.Point(679, 176);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(258, 397);
            this.groupBox1.TabIndex = 12;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Settings";
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(61, 125);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(87, 13);
            this.label6.TabIndex = 13;
            this.label6.Text = "MaximumRadius:";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(6, 93);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(142, 13);
            this.label5.TabIndex = 12;
            this.label5.Text = "CircleAccumulatorThreshold:";
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(64, 189);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(84, 13);
            this.label8.TabIndex = 15;
            this.label8.Text = "MinimumRadius:";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(61, 61);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(87, 13);
            this.label4.TabIndex = 11;
            this.label4.Text = "CannyThreshold:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(29, 29);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(119, 13);
            this.label3.TabIndex = 10;
            this.label3.Text = "AccumulatorResolution:";
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(55, 157);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(93, 13);
            this.label7.TabIndex = 14;
            this.label7.Text = "MinimumDistance:";
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.btn_HighlightSet);
            this.groupBox2.Controls.Add(this.label11);
            this.groupBox2.Controls.Add(this.label10);
            this.groupBox2.Controls.Add(this.tb_DrawThickness);
            this.groupBox2.Controls.Add(this.tb_Blue);
            this.groupBox2.Controls.Add(this.chk_ShowImage);
            this.groupBox2.Controls.Add(this.tb_Green);
            this.groupBox2.Controls.Add(this.tb_Red);
            this.groupBox2.Location = new System.Drawing.Point(679, 12);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(258, 158);
            this.groupBox2.TabIndex = 13;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Highlight";
            // 
            // btn_HighlightSet
            // 
            this.btn_HighlightSet.Location = new System.Drawing.Point(189, 129);
            this.btn_HighlightSet.Name = "btn_HighlightSet";
            this.btn_HighlightSet.Size = new System.Drawing.Size(58, 23);
            this.btn_HighlightSet.TabIndex = 19;
            this.btn_HighlightSet.Text = "Set";
            this.btn_HighlightSet.UseVisualStyleBackColor = true;
            this.btn_HighlightSet.Click += new System.EventHandler(this.btn_HighlightSet_Click);
            // 
            // label11
            // 
            this.label11.AutoSize = true;
            this.label11.Location = new System.Drawing.Point(64, 97);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(84, 13);
            this.label11.TabIndex = 18;
            this.label11.Text = "DrawThickness:";
            // 
            // label10
            // 
            this.label10.AutoSize = true;
            this.label10.Location = new System.Drawing.Point(74, 63);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(33, 13);
            this.label10.TabIndex = 17;
            this.label10.Text = "RGB:";
            // 
            // tb_DrawThickness
            // 
            this.tb_DrawThickness.Location = new System.Drawing.Point(153, 95);
            this.tb_DrawThickness.Name = "tb_DrawThickness";
            this.tb_DrawThickness.Size = new System.Drawing.Size(87, 20);
            this.tb_DrawThickness.TabIndex = 17;
            // 
            // tb_Blue
            // 
            this.tb_Blue.Location = new System.Drawing.Point(198, 59);
            this.tb_Blue.Name = "tb_Blue";
            this.tb_Blue.Size = new System.Drawing.Size(42, 20);
            this.tb_Blue.TabIndex = 16;
            // 
            // chk_ShowImage
            // 
            this.chk_ShowImage.AutoSize = true;
            this.chk_ShowImage.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.chk_ShowImage.Location = new System.Drawing.Point(37, 25);
            this.chk_ShowImage.Name = "chk_ShowImage";
            this.chk_ShowImage.Size = new System.Drawing.Size(85, 17);
            this.chk_ShowImage.TabIndex = 14;
            this.chk_ShowImage.Text = "ShowImage:";
            this.chk_ShowImage.UseVisualStyleBackColor = true;
            // 
            // tb_Green
            // 
            this.tb_Green.Location = new System.Drawing.Point(153, 59);
            this.tb_Green.Name = "tb_Green";
            this.tb_Green.Size = new System.Drawing.Size(42, 20);
            this.tb_Green.TabIndex = 15;
            // 
            // tb_Red
            // 
            this.tb_Red.Location = new System.Drawing.Point(108, 59);
            this.tb_Red.Name = "tb_Red";
            this.tb_Red.Size = new System.Drawing.Size(42, 20);
            this.tb_Red.TabIndex = 14;
            // 
            // lb_Detection
            // 
            this.lb_Detection.FormattingEnabled = true;
            this.lb_Detection.Location = new System.Drawing.Point(10, 322);
            this.lb_Detection.Name = "lb_Detection";
            this.lb_Detection.Size = new System.Drawing.Size(660, 251);
            this.lb_Detection.TabIndex = 14;
            // 
            // label12
            // 
            this.label12.AutoSize = true;
            this.label12.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.label12.Location = new System.Drawing.Point(12, 305);
            this.label12.Name = "label12";
            this.label12.Size = new System.Drawing.Size(91, 13);
            this.label12.TabIndex = 15;
            this.label12.Text = "Detection info:";
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(947, 581);
            this.Controls.Add(this.label12);
            this.Controls.Add(this.lb_Detection);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "MainForm";
            this.Text = "Circle Detection";
            this.Load += new System.EventHandler(this.MainForm_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button btn_Set;
        private System.Windows.Forms.TextBox tb_AccumulatorResolution;
        private System.Windows.Forms.TextBox tb_CannyThreshold;
        private System.Windows.Forms.TextBox tb_CircleAccumulatorThreshold;
        private System.Windows.Forms.TextBox tb_MaximumRadius;
        private System.Windows.Forms.TextBox tb_MinimumDistance;
        private System.Windows.Forms.TextBox tb_MinimumRadius;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.CheckBox chk_ShowImage;
        private System.Windows.Forms.TextBox tb_Blue;
        private System.Windows.Forms.TextBox tb_Green;
        private System.Windows.Forms.TextBox tb_Red;
        private System.Windows.Forms.Label label11;
        private System.Windows.Forms.Label label10;
        private System.Windows.Forms.TextBox tb_DrawThickness;
        private System.Windows.Forms.ListBox lb_Detection;
        private System.Windows.Forms.Button btn_HighlightSet;
        private System.Windows.Forms.Label label12;
    }
}


// Form1.cs

using System;
using System.Drawing;
using System.Windows.Forms;
using Ozeki.Media.MediaHandlers;
using Ozeki.Media.MediaHandlers.Video;
using Ozeki.Media.MediaHandlers.Video.CV;
using Ozeki.Media.MediaHandlers.Video.CV.Processer;
using Ozeki.Media.Video.Controls;
using Ozeki.Media.MediaHandlers.Video.CV.Data;

namespace CircleDetection
{
    public partial class Form1 : Form
    {
        WebCamera _webCamera;
        MediaConnector _connector;

        ImageProcesserHandler _imageProcesserHandler;
        ICircleDetector _circleDetector;
        FrameCapture _frameCapture;

        VideoViewerWF _originalView;
        VideoViewerWF _processedView;
        DrawingImageProvider _originalImageProvider;
        DrawingImageProvider _processedImageProvider;

        public Form1()
        {
            InitializeComponent();
        }

        void MainForm_Load(object sender, EventArgs e)
        {
            Init();

            SetVideoViewers();

            InitDetectorFields();

            ConnectWebcam();

            Start();
        }

        void Init()
        {
            _frameCapture = new FrameCapture();
            _frameCapture.SetInterval(5);

            _webCamera = WebCamera.GetDefaultDevice();
            _connector = new MediaConnector();
            _originalImageProvider = new DrawingImageProvider();
            _processedImageProvider = new DrawingImageProvider();

            _circleDetector = ImageProcesserFactory.CreateCircleDetector();
            _circleDetector.DetectionOccurred += _circleDetector_DetectionOccurred;

            _imageProcesserHandler = new ImageProcesserHandler();
            _imageProcesserHandler.AddProcesser(_circleDetector);
        }

        void SetVideoViewers()
        {
            _originalView = new VideoViewerWF
            {
                BackColor = Color.Black,
                Location = new Point(10, 20),
                Size = new Size(320, 240)
            };

            _originalView.SetImageProvider(_originalImageProvider);
            Controls.Add(_originalView);

            _processedView = new VideoViewerWF
            {
                BackColor = Color.Black,
                Location = new Point(350, 20),
                Size = new Size(320, 240)
            };

            _processedView.SetImageProvider(_processedImageProvider);
            Controls.Add(_processedView);
        }

        void InitDetectorFields()
        {
            InvokeGUIThread(() =>
            {
                chk_ShowImage.Checked = _circleDetector.ShowImage;
                tb_Red.Text = _circleDetector.DrawColor.R.ToString();
                tb_Green.Text = _circleDetector.DrawColor.G.ToString();
                tb_Blue.Text = _circleDetector.DrawColor.B.ToString();
                tb_DrawThickness.Text = _circleDetector.DrawThickness.ToString();

                tb_AccumulatorResolution.Text = _circleDetector.AccumulatorResolution.ToString();
                tb_CannyThreshold.Text = _circleDetector.CannyThreshold.ToString();
                tb_CircleAccumulatorThreshold.Text = _circleDetector.CircleAccumulatorThreshold.ToString();
                tb_MaximumRadius.Text = _circleDetector.MaximumRadius.ToString();
                tb_MinimumDistance.Text = _circleDetector.MinimumDistance.ToString();
                tb_MinimumRadius.Text = _circleDetector.MinimumRadius.ToString();
            });
        }

        void ConnectWebcam()
        {
            _connector.Connect(_webCamera, _originalImageProvider);

            _connector.Connect(_webCamera, _frameCapture);
            _connector.Connect(_frameCapture, _imageProcesserHandler);
            _connector.Connect(_imageProcesserHandler, _processedImageProvider);
        }

        void Start()
        {
            _originalView.Start();
            _processedView.Start();

            _frameCapture.Start();
            _webCamera.Start();
        }

        void btn_HighlightSet_Click(object sender, EventArgs e)
        {
            InvokeGUIThread(() =>
            {
                _circleDetector.ShowImage = chk_ShowImage.Checked;
                _circleDetector.DrawColor = Color.FromArgb(Int32.Parse(tb_Red.Text), Int32.Parse(tb_Green.Text), Int32.Parse(tb_Blue.Text));
                _circleDetector.DrawThickness = Int32.Parse(tb_DrawThickness.Text);
            });
        }

        void btn_Set_Click(object sender, EventArgs e)
        {
            InvokeGUIThread(() =>
            {
                _circleDetector.AccumulatorResolution = Double.Parse(tb_AccumulatorResolution.Text);
                _circleDetector.CannyThreshold = Double.Parse(tb_CannyThreshold.Text);
                _circleDetector.CircleAccumulatorThreshold = Double.Parse(tb_CircleAccumulatorThreshold.Text);
                _circleDetector.MaximumRadius = Int32.Parse(tb_MaximumRadius.Text);
                _circleDetector.MinimumDistance = Double.Parse(tb_MinimumDistance.Text);
                _circleDetector.MinimumRadius = Int32.Parse(tb_MinimumRadius.Text);
            });
        }

        void _circleDetector_DetectionOccurred(object sender, CircleDetectedEventArgs e)
        {
            InvokeGUIThread(() =>
            {
                lb_Detection.Items.Clear();

                foreach (var info in e.Info)
                {
                    lb_Detection.Items.Add(info);
                }
            });
        }

        void InvokeGUIThread(Action action)
        {
            BeginInvoke(action);
        }

    }
}
Snippet (programming) application Windows Presentation Foundation

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • An Overview of 3 Java Embedded Databases
  • Unit Vs Integration Testing: What's the Difference?
  • The Right Way to Hybridize Your Product Development Technique
  • How To Integrate Third-Party Login Systems in Your Web App Using OAuth 2.0

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo