cv2.imread reads a little detail of grayscale image

We all know that grayscale images or infrared images are single-channel images, while color images are three-channel images. But when we use img.shape to read grayscale/infrared images, the three-channel result is returned:

img_path = "/home/zhaotongdong/data/kaggle_humap/256/archive/masks/1e2425f28_18.png"#这里是我的单通道灰度图
img = cv2.imread(img_path)
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow('1', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

But when print does show a three-channel
Insert picture description here
main reasons: opencv reads the image with three channels of default, 如果是灰度图/红外图片则会which is the layer 复制三次(RGB缺省),因此读出来的图片是三通道。
if we want to start reading in accordance with single-channel grayscale images / infrared picture, can在imread()函数中加入相关参数(cv2.IMREAD_GRAYSCALE):

img_path = "/home/zhaotongdong/data/kaggle_humap/256/archive/masks/1e2425f28_18.png"#这里是我的单通道灰度图

img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
cv2.imshow('1', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Insert picture description here

Guess you like

Origin blog.csdn.net/AWhiteDongDong/article/details/111185930