从CV_32F到CV_8U的Mat对象的更改类型。

时间:2023-01-19 18:51:24

I tried to display an image of CV_32F type using imshow function but it showed a WHITE image. In the Documentation its given that floating point images will be mapped to 0-255 and displayed but it just showed a white image.I tried to convert it to CV_8U using

我尝试使用imshow函数来显示CV_32F类型的图像,但它显示了一个白色的图像。在文档中,它给出的浮点图像将被映射到0-255并显示,但它只是显示了一个白色的图像。我尝试将它转换为CV_8U。

Mat A=Mat::ones(300,300,CV_32FC1)*1000;

垫一个=垫::的(300300年,CV_32FC1)* 1000;

do some processing - assigning float values to pixels in A

一些处理——将浮点值分配给A中的像素?

......

……

Mat B;

垫B;

A.convertTo(B,CV_8U)

CV_8U A.convertTo(B)

When I imshow 'B' i get a black & white image, there are no shades of gray. Are the float valued pixels in A properly mapped to 0-255 ? Am I doing anything wrong?

当我给“B”添加一个黑白图像时,就没有灰色的阴影了。是否正确映射到0-255的浮动值像素?我做错什么了吗?

Few values in A are 1000 as initialized and rest are some floating point numbers which are assigned during processing.

在A中很少有值是初始化的,其余的值是在处理过程中分配的浮点数。

1 个解决方案

#1


43  

In OpenCV, if the image is of floating point type, then only those pixels can be visualized using imshow, which have value from 0.0 to 1.0, if the value is greater than 1.0, it will be shown as a white pixel and if less than 0.0, it will be shown as black pixel. To visualize a floating point image, scale its values to the range 0.0 - 1.0.

在OpenCV,如果图像是浮点类型,然后只有那些像素可以使用imshow可视化,值从0.0到1.0,如果该值大于1.0,它将显示为白色像素,如果小于0.0,它将显示为黑色像素。为了可视化一个浮点图像,将其值缩放到0.0 - 1.0。

As for the conversion part.... When used with default arguments, the cv::Mat::convertTo function just creates a matrix of the specified type, and then copies the values from the source matrix and then rounds them to the nearest possible value of the destination data type. If the value is out of range, it is clamped to the minimum or maximum values.

至于....转换部分当使用默认参数时,cv::Mat::convertTo函数只创建指定类型的矩阵,然后从源矩阵复制值,然后将其转到目标数据类型的最近可能值。如果值超出范围,则将其限制为最小值或最大值。

In the documentation of imshow, it is written that:

在imshow的文档中,它是这样写的:

If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].

如果图像是32位浮点数,则像素值乘以255。也就是说,取值范围[0,1]被映射到[0,255]。

It means that only the values in the range 0.0 to 1.0 will be mapped to 0 to 255. If a value is greater than 1.0, and multiplied by 255, it will become greater than 255. Than it will be clamped to the range of CV_8U and eventually it will also become 255.

这意味着只有0.0到1.0范围内的值将被映射为0到255。如果一个值大于1.0,并且乘以255,它就会大于255。它将被夹在CV_8U的范围内,最终也会变成255。

In your example, all the values which are 1000, will become 255 in the destination matrix as the destination type is CV_8U and the maximum possible value is 255. All the floating point values will be floored. No automatic mapping is done.

在您的示例中,所有1000的值都将在目标矩阵中成为255,因为目标类型是CV_8U,最大可能值是255。所有浮点值都将被使用。没有自动映射。

To appropriately map the values to the range of CV_8U use the 3rd and 4th parameters of the function cv::Mat::convertTo, so that the values are scaled before the conversion is done.

要适当地将值映射到CV_8U的范围,请使用函数cv的第3和第4个参数::convertTo,以便在转换完成之前将值缩放。

Suppose the matrix A has minimum and maximum values Min and Max, where Min!=Max.

假设矩阵A有最小值和最大值最小值和最大值,最小值=最大值。

To properly scale the values from 0 to 255, you can do the following:

要正确地将值从0扩展到255,可以执行以下操作:

if (Min!=Max){ 
    A -= Min;
    A.convertTo(B,CV_8U,255.0/(Max-Min));
}

You can also do this directly like this:

你也可以这样直接做:

if (Min!=Max)
    A.convertTo(B,CV_8U,255.0/(Max-Min),-255.0*Min/(Max-Min));

(edited taking into account zhangxaochen's comment)

(根据张韶晨的评论编辑)

#1


43  

In OpenCV, if the image is of floating point type, then only those pixels can be visualized using imshow, which have value from 0.0 to 1.0, if the value is greater than 1.0, it will be shown as a white pixel and if less than 0.0, it will be shown as black pixel. To visualize a floating point image, scale its values to the range 0.0 - 1.0.

在OpenCV,如果图像是浮点类型,然后只有那些像素可以使用imshow可视化,值从0.0到1.0,如果该值大于1.0,它将显示为白色像素,如果小于0.0,它将显示为黑色像素。为了可视化一个浮点图像,将其值缩放到0.0 - 1.0。

As for the conversion part.... When used with default arguments, the cv::Mat::convertTo function just creates a matrix of the specified type, and then copies the values from the source matrix and then rounds them to the nearest possible value of the destination data type. If the value is out of range, it is clamped to the minimum or maximum values.

至于....转换部分当使用默认参数时,cv::Mat::convertTo函数只创建指定类型的矩阵,然后从源矩阵复制值,然后将其转到目标数据类型的最近可能值。如果值超出范围,则将其限制为最小值或最大值。

In the documentation of imshow, it is written that:

在imshow的文档中,它是这样写的:

If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].

如果图像是32位浮点数,则像素值乘以255。也就是说,取值范围[0,1]被映射到[0,255]。

It means that only the values in the range 0.0 to 1.0 will be mapped to 0 to 255. If a value is greater than 1.0, and multiplied by 255, it will become greater than 255. Than it will be clamped to the range of CV_8U and eventually it will also become 255.

这意味着只有0.0到1.0范围内的值将被映射为0到255。如果一个值大于1.0,并且乘以255,它就会大于255。它将被夹在CV_8U的范围内,最终也会变成255。

In your example, all the values which are 1000, will become 255 in the destination matrix as the destination type is CV_8U and the maximum possible value is 255. All the floating point values will be floored. No automatic mapping is done.

在您的示例中,所有1000的值都将在目标矩阵中成为255,因为目标类型是CV_8U,最大可能值是255。所有浮点值都将被使用。没有自动映射。

To appropriately map the values to the range of CV_8U use the 3rd and 4th parameters of the function cv::Mat::convertTo, so that the values are scaled before the conversion is done.

要适当地将值映射到CV_8U的范围,请使用函数cv的第3和第4个参数::convertTo,以便在转换完成之前将值缩放。

Suppose the matrix A has minimum and maximum values Min and Max, where Min!=Max.

假设矩阵A有最小值和最大值最小值和最大值,最小值=最大值。

To properly scale the values from 0 to 255, you can do the following:

要正确地将值从0扩展到255,可以执行以下操作:

if (Min!=Max){ 
    A -= Min;
    A.convertTo(B,CV_8U,255.0/(Max-Min));
}

You can also do this directly like this:

你也可以这样直接做:

if (Min!=Max)
    A.convertTo(B,CV_8U,255.0/(Max-Min),-255.0*Min/(Max-Min));

(edited taking into account zhangxaochen's comment)

(根据张韶晨的评论编辑)