Reputation: 473
How to detect if an image is white. It means that the image has only a white background. This is for black background. Example code:
image = cv2.imread("image.jpg", 0)
if cv2.countNonZero(image) == 0:
print "Image is black"
return True
else:
print "Colored image"
return False
Upvotes: 1
Views: 1825
Reputation: 11193
You could just use numpy.all:
Full white image:
img_white = np.ones([10, 10, 3], np.uint8) * 255
res1 = np.all([img_white == 255])
print(res1) #=> True
Non full white image:
img_non_white = img_white.copy()
img_non_white[1, 1] = (100, 255, 255)
res2 = np.all([img_non_white == 255])
print(res2) #=> False
Upvotes: 1
Reputation: 28
If you read the image with OpenCV, then it comes as a numpy array. So what you need to do is to check how many white points are in there.
import cv2
import numpy as np
img = cv2.imread('img.png', cv2.IMREAD_GRAYSCALE)
n._white_pix = np.sum(img == 255)
Upvotes: 0
Reputation: 1420
You can perform a bitwise_not operation on the input image and apply the same logic(this is just a hack):
image = cv2.imread("image.jpg", 0)
image = cv2.bitwise_not(image)
if cv2.countNonZero(image) == 0:
print "Image is white"
return True
else:
print "Black region is there"
return False
Upvotes: 2