OpenCV 将位图转换为 Mat

Mik*_*eyB 5 c# opencv bitmap mat

我正在使用 c# 和 OpenCV。我有一个位图,我想使用 OpenCV 提供的 VideoWriter 将其写入视频帧。我已经用 Python 完成了这个,所以知道它会起作用。我只需要从 Bitmap 到 Mat 的转换步骤。

我的(部分)代码看起来大致是这样的......

VideoWriter video = new VideoWriter(filename, fps, frameSize, false);
Bitmap image = SomethingReturningABitmap();
// NEED CONVERT FROM Bitmap to Mat
Mat frame = new Mat();
video.Write(frame);
Run Code Online (Sandbox Code Playgroud)

Tab*_*ock 5

我正在使用位图转换器 OpenCV 扩展将我的位图转换为内存中的 Mat 对象。

PSTK ps = new PSTK();
Image img = ps.CaptureScreen();    
Bitmap bmpScreenshot = new Bitmap(img);
image = BitmapConverter.ToMat(bmpScreenshot);
Run Code Online (Sandbox Code Playgroud)

人们也可以执行以下操作,但是这样您将在从磁盘写入和读取数据时产生大量开销。

myBitmap.Save("TempFile.PNG");
Mat myMatImage = CvInvoke.Imread("TempFile.PNG");
Run Code Online (Sandbox Code Playgroud)

我想大致看看有什么区别。我正在使用每种方法处理 500 帧的 1920x1080 桌面屏幕投射,以下是我的发现。

平均而言,该Bitmap.Save/Cv2.ImRead方法需要花费0.1403 seconds per frame. 平均而言,该Bitmap Converter方法需要花费0.09604 seconds per frame. 使用我的家用台式机保存和重新读取文件大约需要 50% 的时间。第二代酷睿 I7,16GB 内存。