图像属性shape,size,dtype

import cv2
if __name__=='__main__':
    src = "./samples/cat_dog.png"
    image = cv2.imread(src)
    GrayImage = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

    #图像形状-shape返回行数,列数,通道数(如果是灰度图像,最后的通道数为一且不显示)
    print(image.shape)
    print(GrayImage.shape)


    #图像大小-size返回图像的像素数目行数*列数*通道数
    print(image.size)
    print(GrayImage.size)

    #图像类型-dtype返回图像的的,通常返回uint8
    print(image.dtype)
    print(GrayImage.dtype)

(224, 224, 3)   长 宽 通道数
(224, 224) 
150528    长*宽*通道数
50176
uint8
uint8

import numpy as np
a = np.array([1,2,3,4,5,6,7,8])  #一维数组
print(a.shape[0])  #值为8,因为有8个数据
print(a.shape[1])  #IndexError: tuple index out of range

a = np.array([[1,2,3,4],[5,6,7,8]])  #二维数组
print(a.shape[0])  #值为2,最外层矩阵有2个元素,2个元素还是矩阵。
print(a.shape[1])  #值为4,内层矩阵有4个元素。
print(a.shape[2])  #IndexError: tuple index out of range


版权声明:本文为weixin_45436729原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。