The code snippet is a bit long but it’s all connected.
I also added image frame to show what i want to detect as segment.
in this case the -4b in the glass window.
in my code i use the mouse to draw a rectangle red rectangle and it should detect the segment inside the rectangle. not sure if to add the rectangle code because the overall code is about 300 lines.
private void InitializeVideoCapture()
{
_videoCapture = new VideoCapture(@"D:Csharp ProjectsVideoSegmentAnalyzerVideo Files to AnalyzeVideo001.mp4");
_totalFrames = _videoCapture.FrameCount;
// Display the first frame for drawing the ROI
_firstFrame = new Mat();
if (_videoCapture.Read(_firstFrame))
{
pictureBoxFrame.Image = BitmapConverter.ToBitmap(_firstFrame);
}
_timer = new Timer();
_timer.Interval = 30; // Adjust as needed
_timer.Tick += ProcessFrame;
}
private void ButtonStart_Click(object sender, EventArgs e)
{
if (_roi.Width == 0 || _roi.Height == 0)
{
MessageBox.Show("Please draw a rectangle to define the ROI.");
return;
}
_currentFrame = 0;
progressBarAnalysis.Value = 0;
labelStatus.Text = "Starting analysis...";
_timer.Start();
}
private void ButtonTest_Click(object sender, EventArgs e)
{
if (_firstFrame != null)
{
ProcessSingleFrame(_firstFrame, 0);
}
}
private async void ProcessSingleFrame(Mat frame, int frameNumber)
{
// Process and analyze the frame
bool blinkDetected = false;
string lastSegment = string.Empty;
var (analyzedFrame, segmentDetected) = await Task.Run(() => AnalyzeFrame(frame, frameNumber, ref blinkDetected, ref lastSegment));
if (analyzedFrame != null)
{
pictureBoxAnalyzed.Image = BitmapConverter.ToBitmap(analyzedFrame);
labelStatus.Text = $"Segment detected in frame {frameNumber}.";
}
else
{
pictureBoxAnalyzed.Image = null; // Clear the PictureBox if no segments are detected
labelStatus.Text = $"No segment detected in frame {frameNumber}.";
}
}
and the AnalayzeFrame method:
private (Mat, bool) AnalyzeFrame(Mat frame, int frameNumber, ref bool blinkDetected, ref string lastSegment)
{
// Ensure we have a valid ROI
if (_roi.Width == 0 || _roi.Height == 0)
return (null, false);
// Convert ROI to OpenCV Rect and crop the frame
Rect rect = new Rect(_roi.X, _roi.Y, _roi.Width, _roi.Height);
Mat roiFrame = new Mat(frame, rect);
// Convert ROI frame to grayscale
Mat grayFrame = new Mat();
Cv2.CvtColor(roiFrame, grayFrame, ColorConversionCodes.BGR2GRAY);
// Apply GaussianBlur
Mat blurredFrame = new Mat();
Cv2.GaussianBlur(grayFrame, blurredFrame, new Size(5, 5), 0);
// Apply Canny edge detection
Mat canny = new Mat();
Cv2.Canny(blurredFrame, canny, 50, 150);
// Apply morphological transformation
Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(5, 5));
Mat closed = new Mat();
Cv2.MorphologyEx(canny, closed, MorphTypes.Close, kernel);
// Find contours
Point[][] contours;
HierarchyIndex[] hierarchy;
Cv2.FindContours(closed, out contours, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple);
bool segmentDetected = false;
foreach (var contour in contours)
{
double peri = Cv2.ArcLength(contour, true);
var approx = Cv2.ApproxPolyDP(contour, 0.02 * peri, true);
double area = Cv2.ContourArea(approx);
if (approx.Length == 4 && area > 1000)
{
Rect boundingRect = Cv2.BoundingRect(approx);
Cv2.Rectangle(frame, boundingRect, new Scalar(36, 255, 12), 3);
segmentDetected = true;
// Save the frame
string filePath = $@"D:DetectedFramesframe_{frameNumber}.png";
frame.SaveImage(filePath);
}
}
return segmentDetected ? (frame, true) : (null, false);
}