I'm trying to iterate over an image with only black and white pixels. For every black pixel I want to decrease a score, while for each white pixel I would like to increase a score. However upon testing the following code I get this error:
ValueError: The truth value of an array with more than one element is ambiguous.
It has something to do with the img[i, j]
statement. How can there be multiple pixels in that aray? Am I not specifically calling one pixel by using img[i,j]
? Does someone know how I could fix this, or if there is another working method for accessing 1 specific pixel?
def score(img):
score = 0
height, width, _ = img.shape
for i in range(height):
for j in range(width):
if img[i, j] == [255,255,255]:
score = score + 1
else:
score = score - 1
print(score)
The image was read using the openCV library. The original image is then filtered for a specific color, with which a mask is created. This mask only has black and white pixels, as mentioned before.
img = cv2.imread("images/test.jpg")
mask = cv2.inRange(img, lower_bound, upper_bound)
img
has three dimensions, soimg[i, j]
is not a single value, but a vector. If the third dimension is1
, you can just doimg[i, j, 0]
instead. However, in that case, you can just doscore = 2 * np.count_nonzero(img) - img.size
. – jdehesaprint(score)
won't work, also can you please show which library (at least, best will be how you define) you use to create the image – U12-Forward