public static Mat WriteableBitmapToMat(WriteableBitmap writeableBitmap)
{
// 获取WriteableBitmap的像素数据
writeableBitmap.Lock();
IntPtr ptr = writeableBitmap.BackBuffer;
int width = writeableBitmap.PixelWidth;
int height = writeableBitmap.PixelHeight;
int stride = writeableBitmap.BackBufferStride;
writeableBitmap.Unlock();
// 创建Mat对象
Mat mat = new Mat(height, width, MatType.CV_8UC4, ptr, stride);
// 转换颜色空间(如果需要)
Cv2.CvtColor(mat, mat, ColorConversionCodes.BGRA2BGR);
return mat;
}
WPF WriteableBitmap转OpenCVSharp Mat
最新推荐文章于 2024-04-10 16:16:11 发布
这段代码将WindowsPhone或UWP中的WriteableBitmap对象转换为OpenCV的Mat类型,以便进行图像处理。它首先锁定WriteableBitmap的像素缓冲区,然后创建一个Mat对象,最后可能进行颜色空间转换(从BGRA到BGR)。
摘要由CSDN通过智能技术生成