opencv获取图像大小

图像矩阵的shape属性表示图像的大小,shape会返回tuple元组,第一个元素表示矩阵行数,第二个元组表示矩阵列数,第三个元素是通道数

1
2
3
4
5
6
7
8
9
10
11
12
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