简体   繁体   English

OpenCV:使用imwrite保存一个双矩阵

[英]Opencv: save a double matrix with imwrite

I have a matrix (Mat) constituted by double, in the range [0,1]. 我有一个由[0,1]范围内的double组成的矩阵(Mat)。 When I save it by means of command imwrite, the resulting image is totally black. 当我通过命令写入保存它时,生成的图像是完全黑色的。 I suppose the problem is a casting problem, but I don't know how to solve it. 我认为问题是强制性问题,但我不知道如何解决。 Thanks 谢谢

The only way for OpenCV to store array of doubles without converting them to other formats (and losing information) is by using FileStorage . OpenCV存储双精度数组而不将其转换为其他格式(并丢失信息)的唯一方法是使用FileStorage imwrite is restricted to arrays of 'char' or 'short'. imwrite仅限于'char'或'short'数组。

You get the totally black image is because all images pixels are within range [0,1] (actually either 0 or 1 when saving to image), which is approaching total black (either for gray-scale image or color image). 您得到全黑图像是因为所有图像像素都在[0,1]范围内(保存到图像时实际上为0或1),接近全黑(对于灰度图像或彩色图像)。

To save the matrix to the image with normal color, you need first to transform the double matrix to range [0, 255] by multiplying each value by 255 . 要保存矩阵与正常颜色的图像,必须先变换双矩阵到范围[0, 255]通过由每个值乘以255 Remember to transform back if you later load the matrix from this image by dividing each value by 255 . 请记住,如果以后再通过将每个值除以255从此图像加载矩阵,请进行转换。

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

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