简体   繁体   English

将OpenCV Mat 32FC1数据复制到WriteableBitmap

[英]Copy OpenCV Mat 32FC1 data to a WriteableBitmap

The very short story is that I want to copy a CV_32FC1 OpenCV image to a Windows [WriteableBitmap][1] which is formatted as [PixelFormats.Gray32Float][2] 简短的故事是我要将CV_32FC1 OpenCV映像复制到Windows [WriteableBitmap] [1],其格式为[PixelFormats.Gray32Float] [2]

The following code works for all the 8 bit image formats (CV_8UF1, CV_8UF3 etc) 以下代码适用于所有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);

imgWrapper.Data is an array byte[], so I am "blindly" copying the data to the WriteableBitmap. imgWrapper.Data是一个数组byte [],所以我“盲目”将数据复制到WriteableBitmap。 Apparently the WriteableBitmap and the OpenCV images have the same layout for 8 bit images, but maybe not for gray 32 bit images. 显然,WriteableBitmap和OpenCV图像对于8位图像具有相同的布局,但对于32位灰度图像则可能不具有相同的布局。

1) How does OpenCV format 32FC1 Mat data? 1)OpenCV如何格式化32FC1 Mat数据? 2) Is there a way in Visual Studio to see the Mat.data as 32 bit floats. 2)在Visual Studio中有没有一种方法可以将Mat.data视为32位浮点数。 In the debugger it's always shown as bytes. 在调试器中,它始终显示为字节。 3) I've read about scaling 32F images to 0-1. 3)我读过关于将32F图像缩放到0-1的信息。 Is this universal? 这是普遍的吗? Does the Windows WriteableBitmap have the same interpretation of the data? Windows WriteableBitmap是否具有对数据的相同解释?

Thanks for any help or tips 感谢您的帮助或提示

To see the value for each pixel in an CV_32FC1 Mat, I ended up dumping it to an xml file. 为了查看CV_32FC1 Mat中每个像素的值,我最终将其转储到xml文件中。

string filename = "averageImage.xml";
FileStorage fs(filename, FileStorage::WRITE);
fs << "R" << averageImage32FNormalized; 

To see the data within VS, you can point the Mat data to a float array float* data = (float*)(averageImage32F.data); 要查看VS中的数据,可以将Mat数据指向一个浮点数组float * data =(float *)(averageImage32F.data); Then type "data,100" in the watch window (for example to see the first 100 values) 然后在监视窗口中键入“ data,100”(例如,查看前100个值)

For display in WPF, the floating point values need to be scaled to 0-1 为了在WPF中显示,浮点值需要缩放为0-1

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

Then you can use writeableBitmap.WritePixels as shown in my original post. 然后,您可以使用如我的原始文章中所示的writeableBitmap.WritePixels。

Note that cv::normalize as I've shown above doesn't normalize the darkest pixel to 0 and the lightest pixel to 1. It seems to assume that the floating point values are on 0-255 scale. 请注意,如上所述,cv :: normalize并未将最暗的像素标准化为0,将最亮的像素标准化为1。似乎假设浮点值在0-255范围内。 In other words a floating point value of 150.3 was converted to 0.59 (150.3/255). 换句话说,浮点值150.3转换为0.59(150.3 / 255)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM