shimat / opencvsharp

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

.NET Framework 4.8 can't open video, the same code .NET6 is ok #1559

Open WolfCanglong opened 1 year ago

WolfCanglong commented 1 year ago

Summary of your issue

I downloaded this example https://github.com/shimat/opencvsharp_samples/ BgSubtractorMOG contains code examples for opening videos. SamplesCore can open video normally using .NET6, but SamplesLegacy using .NET Framework 4.8 cannot open video. I tested other similar codes and it works fine in .NET6, but it doesn't work in .NET Framework 4.8 no matter it's local file or RTMP stream.

Environment

win11 vs2022

What did you do when you faced the problem?

https://github.com/shimat/opencvsharp_samples/ run SamplesCore, 2 BgSubtractorMOG, OK run SamplesLegacy, 2 BgSubtractorMOG, can't work

Example code:

            using var capture = new VideoCapture(MoviePath.Bach);
            using var mog = BackgroundSubtractorMOG.Create();
            using var windowSrc = new Window("src");
            using var windowDst = new Window("dst");

            using var frame = new Mat();
            using var fg = new Mat();
            while (true)
            {
                capture.Read(frame);
                if (frame.Empty())
                    break;
                mog.Apply(frame, fg, 0.01);

                windowSrc.Image = frame;
                windowDst.Image = fg;
                Cv2.WaitKey(50);
            }

Output:

.NET Framework 4.8

----------------------------------------
2
BgSubtractorMOG start executing...
BgSubtractorMOG completed, time cost:186ms

The window flashes by, and the command line returns to the selection menu

var capture = new VideoCapture(MoviePath.Bach);
capture.IsOpened alway return false

capture.Open(MoviePath.Bach)  alway return false

.NET 6

----------------------------------------
2
BgSubtractorMOG start executing...

work well

What did you intend to be?

.NET Framework 4.8 project can open video

WolfCanglong commented 1 year ago

On 4.5.3.20211228 .NET Framework 4.8 also works. I copied this version's DLL to the new version it can work. I feel that there is a problem with the new version of the DLL.

wndfly commented 1 year ago

I have the same problem, can't open video files on .NET Framework 4.7.2, 4.8.0, 4.8.1. I tried OpenCvSharp4 versions from 4.5.1 to 4.7.0.

           var _cap = new VideoCapture("D:\\test.mp4");

            Console.WriteLine(_cap.IsOpened());
            while (true)
            {
                var frame = new Mat();
                _cap.Read(frame);

                if (frame.Empty())
                {
                    Console.WriteLine("ERROR! blank frame grabbed");
                    break;
                }

                Cv2.ImShow("test", frame);
                Cv2.WaitKey(1);
            }
WolfCanglong commented 1 year ago

I have the same problem, can't open video files on .NET Framework 4.7.2, 4.8.0, 4.8.1. I tried OpenCvSharp4 versions from 4.5.1 to 4.7.0.

           var _cap = new VideoCapture("D:\\test.mp4");

            Console.WriteLine(_cap.IsOpened());
            while (true)
            {
                var frame = new Mat();
                _cap.Read(frame);

                if (frame.Empty())
                {
                    Console.WriteLine("ERROR! blank frame grabbed");
                    break;
                }

                Cv2.ImShow("test", frame);
                Cv2.WaitKey(1);
            }

新版DLL有问题,用我说的那个版本(nuget里面相关的全都要改)重新生成就行,或者建个新工程把生成的DLL拷到程序目录

wndfly commented 1 year ago

I have the same problem, can't open video files on .NET Framework 4.7.2, 4.8.0, 4.8.1. I tried OpenCvSharp4 versions from 4.5.1 to 4.7.0.

           var _cap = new VideoCapture("D:\\test.mp4");

            Console.WriteLine(_cap.IsOpened());
            while (true)
            {
                var frame = new Mat();
                _cap.Read(frame);

                if (frame.Empty())
                {
                    Console.WriteLine("ERROR! blank frame grabbed");
                    break;
                }

                Cv2.ImShow("test", frame);
                Cv2.WaitKey(1);
            }

新版DLL有问题,用我说的那个版本(nuget里面相关的全都要改)重新生成就行,或者建个新工程把生成的DLL拷到程序目录

多谢,我找到问题了。 32位的dll有问题64位的正常,构建选择里把any cpu换成x64,32位优先去掉,这样最新版的库就可以在.net framework 4.8.1下正常使用了。

dabenpb commented 11 months ago

Is there any plan to fix this issue? It is still a problem with 4.8.0.20230708