AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

Blob detection ( c# newb ^^)

The forum is to discuss topics related to different areas of image processing and computer vision.

Blob detection ( c# newb ^^)

Postby Combatboy6 » Thu Apr 07, 2016 4:54 pm

Hi so in the course of my Internship i am programming a Golf Putting detection software which has to detect simple shapes such as a circle and and a ball. I have very little experience with c# so please dont be to harsh on me hehe. I have gotten the camera to connect and apply filtering to the image, as well as the blob detection for the circle. I have the used the rectangles that have been found by the blobcounter to mark the blob in the picturebox. The blobcounter does store data in the x,y,height and width though when i draw the rectangle that is supposed to surround the circle it is not surrounding the blob is offset to it.here is the code ^^ id be very greatful if someone has an idee to why the rectangles are offset to the blob?[img]F:\Putter%20Video%20Analysis\1.jpg[/img]
Code: Select all
namespace PutterBallRollDetection
{
    public partial class Form1 : Form
    {
        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoSource;
        private Rectangle[] blobRect;
        private Bitmap imageHoleCalibration;
        private int[] Hole;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            //video devices available
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            // create selection for each device
            foreach (FilterInfo device in videoDevices)
            {
                comboBox1.Items.Add(device.Name);
            }
            videoSource = new VideoCaptureDevice();

            int[] Hole = new int[4];

        }

        private void button1_Click(object sender, EventArgs e)
        {

            // stops the stream and shuts off the video source
            if (videoSource.IsRunning)
            {
                videoSource.Stop();
                pictureBox1.Image = null;
                pictureBox1.Invalidate();
                button1.Text = "Start Stream";
                comboBox1.SelectedIndex = -1;

                button2.Visible = false;
                button2.Enabled = false;
                button3.Visible = false;
                button3.Enabled = false;
                button4.Visible = false;
                button4.Enabled = false;

                videoSource = new VideoCaptureDevice();


            }

            // starts stream and uses Framehandler
            if (comboBox1.SelectedIndex != -1)
            {


                videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
                // set new frame handler
                videoSource.NewFrame += new NewFrameEventHandler(videoSource_Newframe);

                videoSource.Start();


                button1.Text = "Stop Stream";

                button2.Visible = true;
                button2.Enabled = true;
                button3.Visible = true;
                button3.Enabled = true;
                button4.Visible = true;
                button4.Enabled = true;

            }
            // Messages user if no device is selected
            if (comboBox1.SelectedIndex == -1)
            {
                videoSource.Stop();

                pictureBox1.Image = null;
                pictureBox1.Invalidate();


                button1.Text = "Start Stream";
                comboBox1.SelectedIndex = -1;


                Form2 frm = new Form2();
                frm.Show();

                videoSource = new VideoCaptureDevice();
            }

        }
        void videoSource_Newframe(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap input = new Bitmap(1,1);


            if (button4.Text == "Enable Filters")
            {
                System.Drawing.Bitmap image = new Bitmap((Bitmap)eventArgs.Frame.Clone());
                input = image;   //  Picturebox
            }


            if (button4.Text == "Disable Filters")
            {

                System.Drawing.Bitmap imageMod = new Bitmap((Bitmap)eventArgs.Frame.Clone());
                System.Drawing.Bitmap imageFinal;

                // create grayscale filter (BT709)
                Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);

                // apply the filter
                imageFinal = filter.Apply(imageMod);


                // create Binary filter
                Threshold filterGray = new Threshold(50);

                // apply the filter
                filterGray.ApplyInPlace(imageFinal);

                input = imageFinal ;
                imageHoleCalibration = imageFinal;

                if (Hole != null)                   // adding Hole calibration
                {
                    Graphics g = pictureBox1.CreateGraphics();
                    Pen pen = new Pen(Color.Red);
                    g.DrawRectangle(pen, Hole[0], Hole[1], Hole[2], Hole[3]);
                    input = imageHoleCalibration;
                   
                }

            }
           


            pictureBox1.Image = input;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (videoSource.IsRunning)
                videoSource.Stop();         
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (button2.Text == "Enable Tracking")
            {
                button2.Text = "Disable Tracking";
            }
            else
            {
                button2.Text = "Enable Tracking";
            }

        }


        private void button3_Click(object sender, EventArgs e)
        {

            if (button3.Text == "Start Calibration")
                {
                button3.Text = "Stop Calibration";
                BlobCounter blobCounter = new BlobCounter();
                blobCounter.FilterBlobs = true;
                blobCounter.MinHeight = 20;
                blobCounter.MinWidth = 20;

                // process input image
                blobCounter.ObjectsOrder = ObjectsOrder.Size;
                blobCounter.ProcessImage(imageHoleCalibration);


                Blob[] blobs = blobCounter.GetObjectsInformation();
                blobRect = blobCounter.GetObjectsRectangles(); // gets the rectangles for the blobs
                //AForge.Point edges = blobCounter.GetBlobsEdgePoints(blobs[0]);


                // collects data coordinates of the hole

                Hole = new int[4];

                Hole[3] = blobRect[0].Height;
                Hole[2] = blobRect[0].Width;
                Hole[0] = blobRect[0].X;
                Hole[1] = blobRect[0].Y;




            }
            else
            {
                button3.Text = "Start Calibration";
                Hole = null;


            }

             

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (button4.Text == "Enable Filters")
            {
                button4.Text = "Disable Filters";
            }
            else
            {
                button4.Text = "Enable Filters";
            }
        }

    }
        }
   
Combatboy6
 
Posts: 1
Joined: Thu Apr 07, 2016 4:39 pm



Return to Image Processing and Computer Vision