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:
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();
}
}
}