shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.22k stars 1.13k forks source link

Cv2.CvtColor has very low chance CRASH #1608

Open WangXiaoMing419 opened 9 months ago

WangXiaoMing419 commented 9 months ago

Summary of your issue

While I use OpenCV convert YUVI420 video frame to a bitmap. I found at 30fps, the OpenCV crash around 1-2hours. For a single frame, the crash chance is pertty low, but if I run the video enougth long time, it will crash for sure. And this exception can't try...catch in C# code, it will cause the application crash immediately.

Environment

.Net framework 4.6.1 Windows10/Windows11 both tested,behave the same

Example code:

public static OpenCvSharp.Mat DecodeI420Frame(VideoFrame frame)
        {
            OpenCvSharp.Mat yuvImg = new OpenCvSharp.Mat();
            yuvImg.Create(frame.Height * 3 / 2, frame.Width, OpenCvSharp.MatType.CV_8UC1);

            unsafe
            {
                frame.CopyTo(yuvImg.Data.ToPointer());
            }
            var target = new OpenCvSharp.Mat();
            OpenCvSharp.Cv2.CvtColor(yuvImg, target, OpenCvSharp.ColorConversionCodes.YUV2BGRA_I420);
            return target;
        }

Output:

It always crash this line:
OpenCvSharp.Cv2.CvtColor(yuvImg, target, OpenCvSharp.ColorConversionCodes.YUV2BGRA_I420);