How to detect 7 segments from frame image from a video file using opencv4 and c#?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật