Get a quanity of pixels with specific color at image. Python, opencv
Get a quanity of pixels with specific color at image. Python, opencv
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.
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)
?– Mark Setchell
Aug 29 at 13:29
[13,29]
(254,254,254)
[13,29]
(8,7,6)
I want to find all pixels that are (254,254,254). Not coordinates, only quanity, number of such pixels.
– Kirill
Aug 29 at 13:32
2 Answers
2
I would do that with numpy
which is vectorised and much faster than using for
loops:
numpy
for
#!/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()
:
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 RGB values match "sought", and count
result = np.count_nonzero(np.all(im==sought,axis=2))
print(result)
Yep, was about to suggest something along those lines :)
– Dan Mašek
Aug 29 at 13:51
Thanks! The way with np.count_nonzero is faster that with a cycle. One question? What is axis=2?
– Kirill
Aug 29 at 13:56
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
.– Mark Setchell
Aug 29 at 14:02
numpy
for
axis=1
axis=2
axis=3
np.all(im==sought,axis=2)
sought
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)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Welcome to Stack Overflow! What have you tried? Can you show us some code?
– Hein Wessels
Aug 29 at 13:10