[英]python opencv detect the image is completely white
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
You can perform a bitwise_not operation on the input image and apply the same logic(this is just a hack):您可以对输入图像执行 bitwise_not 操作并应用相同的逻辑(这只是一个 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
You could just usenumpy.all :你可以只使用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
If you read the image with OpenCV, then it comes as a numpy array.如果您使用 OpenCV 读取图像,那么它会以 numpy 数组的形式出现。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.