3

I have small image. enter image description here b g r, not gray.

original = cv2.imread('im/auto5.png')
print(original.shape)  # 27,30,3 
print(original[13,29]) # [254 254 254]

As you can see, there is white pic (digit 14) in my image, mostly black. On the right corner (coordinates [13,29]) I get [254 254 254] - white color.

I want to calculate number of pixels with that specific color. I need it to further comparing such images with different numbers inside. There are different backgrounds on these squares, and I consider exactly white color.

3
  • 1
    Welcome to Stack Overflow! What have you tried? Can you show us some code? Commented Aug 29, 2018 at 13:10
  • Do you mean you want to find all pixels matching the one at coordinates [13,29] or you want to find all pixels that are (254,254,254). What I mean is, what do you want to look for if the pixel at [13,29] is (8,7,6)? Commented Aug 29, 2018 at 13:29
  • I want to find all pixels that are (254,254,254). Not coordinates, only quanity, number of such pixels. Commented Aug 29, 2018 at 13:32

3 Answers 3

11

I would do that with numpy which is vectorised and much faster than using for loops:

#!/usr/local/bin/python3
import numpy as np
from PIL import Image

# Open image and make into numpy array
im=np.array(Image.open("p.png").convert('RGB'))

# Work out what we are looking for
sought = [254,254,254]

# Find all pixels where the 3 RGB values match "sought", and count them
result = np.count_nonzero(np.all(im==sought,axis=2))
print(result)

Sample Output

35

It will work just the same with OpenCV's imread():

#!/usr/local/bin/python3
import numpy as np
import cv2

# Open image and make into numpy array
im=cv2.imread('p.png')

# Work out what we are looking for
sought = [254,254,254]

# Find all pixels where the 3 NoTE ** BGR not RGB  values match "sought", and count
result = np.count_nonzero(np.all(im==sought,axis=2))
print(result)
Sign up to request clarification or add additional context in comments.

4 Comments

Yep, was about to suggest something along those lines :)
Thanks! The way with np.count_nonzero is faster that with a cycle. One question? What is axis=2?
I timed the numpy way at 22 microseconds and the equivalent for loop at 1.5 milliseconds, so 68x faster. axis=1 runs up and down the height of the image, axis=2 runs across the image left-to-right, and axis=3 runs into the image along the R, G and B. so, by saying np.all(im==sought,axis=2) I mean that R, G and B are all equal to sought.
I've made an edit but not sure where its gone. I thought this was a great answer but wanted to point out that the OpenCV code uses BGR not RGB. Thanks
1

Image in cv2 is an iterable object. So you can just iterate through all pixels to count the pixels you are looking for.

import os
import cv2
main_dir = os.path.split(os.path.abspath(__file__))[0]
file_name = 'im/auto5.png'
color_to_seek = (254, 254, 254)

original = cv2.imread(os.path.join(main_dir, file_name))

amount = 0
for x in range(original.shape[0]):
    for y in range(original.shape[1]):
        b, g, r = original[x, y]
        if (b, g, r) == color_to_seek:
            amount += 1

print(amount)

Comments

0

Try this :

import cv2
import numpy as np
img = cv2.imread("im/auto5.png")
color = [48,75,105] # set color to looking for
im = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = np.count_nonzero(np.all(im==color,axis=2))
print("Total pixels " + str(result))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.