python黑白图像检测

问题描述 投票:0回答:1

我正在尝试使用python语言中的Open CV识别图像是黑白图像还是彩色图像。我已经使用MS Paint创建了黑白图像以进行检查。即使图像是黑白图像,它仍然具有0和255以外的RGB值。以下是我使用的代码和我使用的图像。我得到的输出是彩色图像。我检查了RGB值,它们的值不是0和255,我无法调试为什么,有人可以帮助我吗?

img = cv2.imread('C:/Comp_vision/image_data/black_and_white.jpg')

image_pixel =img.flatten()

bnw_cnt = sum(np.where((image_pixel == 0) | (image_pixel == 255), 1, 0))

if np.vectorize(bnw_cnt) == np.vectorize(image_pixel):
    print("Black and white image")
else:
    print ("Color image")

the image i used

python opencv image-processing computer-vision analytics
1个回答
1
投票

且仅当对于每个通道的给定像素(x,y)值相等时,图像才会具有黑白颜色。

例如:

def check_gray(img):
    for x in range(img.shape[0])
        for y in range(img.shape[1])
            b, g, r == img[x,y]
            if not(b == g == r):
                return False

    return True          
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.