C# 将OpenCV Mat 32FC1数据复制到可写位图

C# 将OpenCV Mat 32FC1数据复制到可写位图,c#,wpf,opencv,image-processing,bitmap,C#,Wpf,Opencv,Image Processing,Bitmap,很短的一个故事是,我想将CV_32FC1 OpenCV图像复制到一个Windows[WriteableBitmap][1],其格式为[PixelFormats.Gray32Float][2] 以下代码适用于所有8位图像格式CV_8UF1、CV_8UF3等 writeableBitmap = new WriteableBitmap(imgWrapper.Width, imgWrapper.Height, 96, 96,

很短的一个故事是,我想将CV_32FC1 OpenCV图像复制到一个Windows[WriteableBitmap][1],其格式为[PixelFormats.Gray32Float][2]

以下代码适用于所有8位图像格式CV_8UF1、CV_8UF3等

writeableBitmap = new WriteableBitmap(imgWrapper.Width, 
                                      imgWrapper.Height, 96, 96,
                                      imgWrapper.PixelFormat, null);

var frameSize = new Int32Rect(0, 0, imgWrapper.Width, imgWrapper.Height);
writeableBitmap.WritePixels(frameSize, imgWrapper.Data, imgWrapper.Stride, 0);
Data是一个数组字节[],所以我盲目地将数据复制到WriteableBitmap。显然,WriteableBitmap和OpenCV图像对于8位图像具有相同的布局,但对于32位灰度图像可能没有

1 OpenCV如何格式化32FC1 Mat数据? 2在Visual Studio中,是否有方法将Mat.data视为32位浮点。在调试器中,它始终显示为字节。 3我读过关于将32F图像缩放为0-1的内容。这是普遍的吗?Windows WriteableBitmap对数据的解释是否相同


感谢您提供的帮助或提示

查看CV_32FC1垫子中每个像素的值,我最终将其转储到一个xml文件中

string filename = "averageImage.xml";
FileStorage fs(filename, FileStorage::WRITE);
fs << "R" << averageImage32FNormalized; 
然后您可以使用writeableBitmap.WritePixels,如我的原始帖子所示

请注意,如上所示,cv::normalize不会将最暗的像素规格化为0,将最亮的像素规格化为1。它似乎假设浮点值的刻度为0-255。换句话说,150.3的浮点值被转换为0.59 150.3/255

Mat averageImage32FNormalized;
cv::normalize(averageImage32F, averageImage32FNormalized, 0, 1, NORM_MINMAX);