Python版本是Python3.7.3,OpenCV版本OpenCV.3.4.1,开发环境为PyCharm
在图像处理过程中,经常需要获取图像的属性,例如图像的大小、类型等。这里介绍几个常用的属性。
● shape:如果是彩色图像,则返回包含行数、列数、通道数的数组;如果是二值图像或者灰度图像,则仅返回行数和列数。通过该属性的返回值是否包含通道数,可以判断一幅图像是灰度图像(或二值图像)还是彩色图像。
● size:返回图像的像素数目。其值为“行×列×通道数”,灰度图像或者二值图像的通道数为1。
● dtype:返回图像的数据类型。
eg:编写程序,观察图像的常用属性值。
代码如下:
import cv2
gray=cv2.imread("lena.bmp",0)
color=cv2.imread("lenacolor.png")
print("图像gray属性:")
print("gray.shape=",gray.shape)
print("gray.size=",gray.size)
print("gray.dtype=",gray.dtype)
print("图像color属性:")
print("color.shape=",color.shape)
print("color.size=",color.size)
print("color.dtype=",color.dtype)
在本例中,分别读取了灰度图像gray和彩色图像color,并分别观察了它们的shape、size、dtype属性。
运行程序,控制台会输出如下结果:
图像gray属性:
gray.shape= (256, 256)
gray.size= 65536
gray.dtype= uint8
图像color属性:
color.shape= (512, 512, 3)
color.size= 786432
color.dtype= uint8