图像矩阵的shape属性表示图像的大小,shape会返回tuple元组,第一个元素表示矩阵行数,第二个元组表示矩阵列数,第三个元素是3,表示像素值由光的三原色组成。
import cv2
import numpy as np
fn="baboon.jpg"
if __name__ == '__main__':
print 'load %s as ...' % fn
img = cv2.imread(fn)
sp = img.shape
print sp
sz1 = sp[0]#height(rows) of image
sz2 = sp[1]#width(colums) of image
sz3 = sp[2]#the pixels value is made up of three primary colors
print 'width: %d \nheight: %d \nnumber: %d' %(sz1,sz2,sz3)
运行结果:
load baboon.jpg as …
(512, 512, 3)
width: 512
height: 512
number: 3
版权声明:本文为qq_23851075原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。