Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There are several problems with your approach.

Classically the grayscale images are called "black and white" (so the images that don't have color pixels), not the images that contain only black and white pixels.

Your algorithm tries check only for black and white colors, not for grayscale.

The second problem is that flatten will put all your values in a vector. So for the green color (0,255,0) you will get 3 values, and all of them are either 0 or 255. So a totally green image will be considered a black and white image.

The second problem is that Paint uses a technique named antialiasing, which means that the contour pixels are in fact gray pixels. This is used to eliminate the jagged, pixelated edges.

If you want to detect if an image is grayscale, convert it to HSV color space (using cv2.cvtColor), split it to the 3 channels (h,s,v = cv2.split(hsvimg)) and check the saturation channel for every pixel to be 0. If you want to see that it contains only black and white color, then after the pre vious step check the value channel for 0 and 255.

You can find below your image in true black and white (notice the pixelated edges).

black_and_white