Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
i wrote this code , there a few seconds video (ball and court of volleyball) ,
i using Emgu CV (image processing) , i could find ball by (HoughCircles) and i drew a cirle around the ball , then i used emgu for different parts of court of volleyball (inside and outside of court and white line of court) ,
now all thing are working properly
here is my question:
What code should I write so that the program can detect which part of the court the circle I drew around the ball hit (if it hits the inside of the court and on the white line, it will display the message "IN", and if it hits the outside of the field, it will display the message "OUT")
thanks for your helps in advance
video for test (only 200 Kb) : https://filebin.net/s5nxw45g61z8wkz1/1.mp4
https://i.stack.imgur.com/a6pQ9.jpg[^]

What I have tried:

C#
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System;
using System.Drawing;
using System.Windows.Forms;


namespace MainForm
{
    public partial class Form1 : Form
    {
        private VideoCapture videoCapture;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Video Files|*.avi;*.mp4;*.wmv|All Files|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string videoPath = openFileDialog.FileName;
                videoCapture = new VideoCapture(videoPath);
                ProcessVideo();
            }
        }
        private void ProcessVideo()
        {
             Mat frame = new Mat();
            Mat blurred = new Mat();
            Mat hsvFrame = new Mat();
            Mat fieldMask2 = new Mat();
            Mat fieldMask = new Mat();
            Mat lineMask = new Mat();

            int frameRate = (int)videoCapture.Get(CapProp.Fps);
            int frameCount = frameRate * 50;
             for (int i = 0; i < frameCount; i++)
            {
                 bool success = videoCapture.Read(frame);

                 if (!success)
                {
                    MessageBox.Show("Error in reading frame");
                    break;
                }

                CvInvoke.CvtColor(frame, hsvFrame, ColorConversion.Bgr2Hsv);
                Mat grayFrame = new Mat();
                CvInvoke.CvtColor(frame, grayFrame, ColorConversion.Bgr2Gray);
            
                ScalarArray lowerWhite = new ScalarArray(new MCvScalar(15, 25, 100));
                ScalarArray upperWhite = new ScalarArray(new MCvScalar(100, 100, 255));
                ScalarArray lowerGreen = new ScalarArray(new MCvScalar(1, 160, 100));
                ScalarArray upperGreen = new ScalarArray(new MCvScalar(100, 255, 255));
                ScalarArray lowerBlue = new ScalarArray(new MCvScalar(90, 50, 50));
                ScalarArray upperBlue = new ScalarArray(new MCvScalar(120, 255, 255));

                CvInvoke.GaussianBlur(hsvFrame, blurred, new Size(5, 5), 1.5);
                CvInvoke.InRange(blurred, lowerGreen, upperGreen, fieldMask);
                CvInvoke.InRange(blurred, lowerBlue, upperBlue, fieldMask2);
                CvInvoke.InRange(blurred, lowerWhite, upperWhite, lineMask);

                 CircleF[] circles = CvInvoke.HoughCircles(grayFrame, HoughModes.Gradient, 2.0, 20.0, 100.0, 45.0, 34, 39);
                foreach (CircleF circle in circles)
                {
                    CvInvoke.Circle(frame, Point.Round(circle.Center), (int)circle.Radius, new Bgr(Color.White).MCvScalar, 3);
                    CvInvoke.Circle(fieldMask, Point.Round(circle.Center), (int)circle.Radius, new Bgr(Color.White).MCvScalar, 3);
                    CvInvoke.Circle(fieldMask2, Point.Round(circle.Center), (int)circle.Radius, new Bgr(Color.White).MCvScalar, 3);
                    CvInvoke.Circle(lineMask, Point.Round(circle.Center), (int)circle.Radius, new Bgr(Color.White).MCvScalar, 3);
                }

                Mat ballResult2 = new Mat();
                Mat ballResult3 = new Mat(); 
                Mat ballResult4 = new Mat();

                CvInvoke.BitwiseAnd(frame, frame, ballResult2, fieldMask);
                CvInvoke.BitwiseAnd(frame, frame, ballResult3, fieldMask2);
                CvInvoke.BitwiseAnd(frame, frame, ballResult4, lineMask);

                CvInvoke.Imshow("Main", frame);
                CvInvoke.Imshow("outside of court", ballResult3);
                CvInvoke.Imshow("inside of court", ballResult2);
                CvInvoke.Imshow("Line", ballResult4);

                CvInvoke.WaitKey(30);  
            }

             videoCapture.Dispose();
            CvInvoke.DestroyAllWindows();
        }
    }
}
Posted
Updated 16-Jun-23 1:07am

1 solution

You can use "IntersectsWith" like it was done in this article
Small WinForm Pong Game - C#[^]

or use a method like shown in this tutorial:
Collision Detection - Happy Coding[^]
 
Share this answer
 
Comments
payam mohammadi 17-Jun-23 2:25am    
thanks , but it is not possible to find intersection between a shape (circle) and a color mask

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900