函数原型:
void imshow(const string& winname, InputArray mat)
参数:
winname – Name of the window.
image – Image to be shown.
功能:
Displays an image in the specified window.
接下来主要说明一下当显示非uchar即非CV_8U类型的图像时,imshow是怎么操作的。
opencv中图像的基本数据类型有:
#define CV_8U 0
#define CV_8S 1
#define CV_16U 2
#define CV_16S 3
#define CV_32S 4
#define CV_32F 5
#define CV_64F 6
imshow在显示图像时,会将上面各种类型的数据都映射到[0, 255]。
在显示图像之前,imshow会判断输入图像的数据类型是否为CV_8U,如果不是,则会对图像数据进行转换,具体的转换方式如下:
int src_depth = CV_MAT_DEPTH(src->type); // 获取输入图像的数据类型
double scale = src_depth <= CV_8S ? 1 : src_depth <= CV_32S ? 1./256 : 255; // 获取scale
double shift = src_depth == CV_8S || src_depth == CV_16S ? 128 : 0; // 获取shift
在通过上面的方式确定scale和shift之后,接下来便是对数据进行转换:
cvConvertScale( src, dst, scale, shift );
通过转换得到的dst便是用来显示的图像了。
文档中的说明:
The function may scale the image, depending on its depth:
If the image is 8-bit unsigned, it is displayed as is.
If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].
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].