0
votes

I am trying to identify if an image is black and white or a color image using Open CV in python language. i have created a black and white image using MS paint to check the same. 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. the output i am getting is color image. 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?

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

1
disable antialiasing when you drawMiki
I have used this particular image to test, there are other images downloaded too.bindu madhavi
these images are not binary (only 2 colors black and white), but grayscale. You can compute the histogram for an image. If say 99% of the colors are near black (say <10) or near white ( say >245) than you can say that for your purposes the image is b/wMiki
i checked the same if they fall in a particular range or so, they are very deversified.bindu madhavi
so please post some image so we don't have to guess...Miki

1 Answers

2
votes

Image will have black-white colors if and only if for given pixel (x,y) values on each channel will be equal.

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