获取图像属性
图像的属性包括:行,列,通道,图像数据类型,像素数目等
img.shape 可以获取图像的形状。他的返回值是一个包含行数,列数,
通道数的元组。
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Sun May 5 15:51:34 2019 4 @author: nwpujun 5 #获取图像属性 6 """ 7 import cv2 8 import numpy as np 9 img=cv2.imread('2018.png') 10 11 print img.shape
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
注意:如果图像是灰度图,返回值仅有行数和列数。所以通过检查这个返回值
就可以知道加载的是灰度图还是彩色图。
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Sun May 5 15:51:34 2019 4 @author: nwpujun 5 #获取图像属性 6 """ 7 import cv2 8 import numpy as np 9 img=cv2.imread('2018.png') 10 11 print img.shape 12 print img.size 13 print img.dtype
img.size 可以返回图像的像素数目
img.dtype 返回的是图像的数据类型.
注意:在除虫(debug)时 img.dtype 非常重要。因为在 OpenCV-
Python 代码中经常出现数据类型的不一致。

更多精彩