# 查看图像的通道数
# 必须看这个https://blog.csdn.net/juzicode00/article/details/117430122?utm_medium=
# distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-0.essearch_pc_relevant&spm=1001.2101.3001.4242
import sys
import cv2
import numpy as np
# 不能有中文
image_name = r"H:\01HTutorWork\3GF2\2DataAndLabel\2UAVdata\3FeiXiAndBaNanUAVdata\2UAVSegmentationDataset\target\singleChaPNG\bn017.png"
#image_name = r"H:\01HTutorWork\3GF2\2DataAndLabel\3BeiJing2_zhongxian\yuyifenge\SinglechalPng\z1.png"
# 3通道 H:\01HTutorWork\3GF2\2DataAndLabel\2UAVdata\1FeiXiUAVdata\3FeiXiSegmentationDataset\target\JPEGImages\fx001.jpg
# 3通道 H:\01HTutorWork\3GF2\2DataAndLabel\2UAVdata\1FeiXiUAVdata\3FeiXiSegmentationDataset\target\SegmentationClassPNG\fx001.png
# 单通道 H:\01HTutorWork\3GF2\2DataAndLabel\2UAVdata\1FeiXiUAVdata\3FeiXiSegmentationDataset\target\singleChaPNG\fx001.png
def lm_get_image_size(file_name):
print('load %s as ...' % file_name)
# 必须加cv2.IMREAD_UNCHANGED,意思是读取原图,否则自动转换为3通道
img = cv2.imread(file_name,cv2.IMREAD_UNCHANGED)
sp = img.shape
print(sp)
np_img = np.array(img)
print("最大值:", np.max(np_img))
print("最小值", np.min(np_img))
print("唯一值", np.unique(np_img))
sz1 = sp[0] # height(rows) of image
sz2 = sp[1] # width(colums) of image
sz3 = sp[2] # channels
print('height: %d \nwidth: %d \nchannels: %d' % (sz1, sz2, sz3))
return sp
def main():
lm_get_image_size(image_name)
if __name__ == '__main__':
sys.exit(main())
02-28
4477
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-31
8440
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-13
2333
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-11
1608
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-14
3612
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)