简体   繁体   English

python 黑白图像检测

[英]python black and white image detection

I am trying to identify if an image is black and white or a color image using Open CV in python language.我正在尝试使用 python 语言中的 Open CV 来识别图像是黑白图像还是彩色图像。 i have created a black and white image using MS paint to check the same.我已经使用 MS Paint 创建了一个黑白图像来检查相同的内容。 even though the image is black white it still has RGB values other than 0 and 255. below is the code i have used and images i have used.即使图像是黑白的,它仍然具有 0 和 255 以外的 RGB 值。下面是我使用的代码和我使用的图像。 the output i am getting is color image.我得到的 output 是彩色图像。 I checked the RGB values they have values other than 0 and 255, i am not able to debug why, can some one help me with this?我检查了 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")

我使用的图像

Image will have black-white colors if and only if for given pixel (x,y) values on each channel will be equal.当且仅当每个通道上的给定像素 (x,y) 值相等时,图像将具有黑白 colors。

For example:例如:

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          

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM