Reputation: 2912
I wrote a script to detect if an image has a white background, by totalling all the pixels that are white and if it exceeds a threshold percentage of the total pixels.
This process takes time and especially long if I have a lot of images. Is there a more efficient way in numpy or opencv that can do it, rather than just using parallel processing?
def find_white_background(imgpath, threshold="0.3"):
"""remove images with transparent or white background"""
imgArr = cv2.imread(imgpath)
w, h, alpha = imgArr.shape
total = w * h
background = np.array([255, 255, 255])
cnt = 0
for row in imgArr:
for pixel in row:
if np.array_equal(pixel, background):
cnt += 1
percent = cnt / total
if percent >= threshold:
return True
else:
return False
Upvotes: 3
Views: 3401
Reputation: 16147
This should provide greater efficiency by comparing the entire array to your background color array at once instead of looping.
def find_white_background(imgpath, threshold=0.3):
"""remove images with transparent or white background"""
imgArr = cv2.imread(imgpath)
background = np.array([255, 255, 255])
percent = (imgArr == background).sum() / imgArr.size
if percent >= threshold:
print(percent)
return True
else:
return False
Upvotes: 3