I'm working with a little project with application of OpenCV
and I'm stuck with something that I don't know how to implement. Suppose I have an image (1024x768
). In this image there is a red bounding box at the center.
Is it possible to count the pixels inside the red box using OpenCS
? given that the image is 1024x768
in dimension.
I tried to use bounding rectangle by thresholding the red color and tried using convexhull
but then I can't extract how many pixels are inside the red marker.
Counting Pixels This sum() function can be used to count the number of pixels on the basis of the required criteria.
The width and height of the image in pixels. To find the total number of pixels, multiply the width and height together. In this case, 4700 pixels x 3133 pixels = 14,725,100 pixels. That's a lot of pixels.
To display OpenCV provides the imshow () method for displaying the image we have recently read. NumPy provides a function sum () that returns the sum of all array elements in the NumPy array. This sum () function can be used to count the number of pixels on the basis of the required criteria.
For black images you get the total number of pixels (rows*cols) and then subtract it from the result you get from cv2.countNonZero (mat). For other values, you can create a mask using cv2.inRange () to return a binary mask showing all the locations of the color/label/value you want and then use cv2.countNonZero to count how many of them there are.
This condition can be written in the NumPy as: number_of_white_pix = np.sum (img == 255) # extracting only white pixels number_of_black_pix = np.sum (img == 0) # extracting only black pixels The first line says to extract and count all pixels from cv2 image object “img” whose pixel value is 255 i.e. white pixels.
It is an open-source library to process images and videos for various applications in real life, like segmentation, object detection, and many more. The main benefit of the OpenCV library is working on NumPy arrays that can be work with a different library. The process to count the objects in an image is done with the below process:
That's simple. Clearly the inner gray and outer colour are different. just threshold the image.
ret,thresh=cv2.threshold(img,133,255,cv2.THRESH_BINARY_INV)
Then use:
cv2.countNonZero(your_thresh_img)
That gives you the number of white pixels, which is the count you need. In your Image, it was 183920 pixels.
Edit
import numpy as np
import cv2
img=cv2.imread("your_image.png",0)
def nothing(x):
pass
cv2.namedWindow('image')
cv2.createTrackbar('min','image',0,255,nothing)
cv2.createTrackbar('max','image',0,255,nothing)
while(1):
a = cv2.getTrackbarPos('min','image')
b = cv2.getTrackbarPos('max','image')
ret,thresh=cv2.threshold(img,a,b,cv2.THRESH_BINARY_INV)
cv2.imshow("output",thresh)
k = cv2.waitKey(10) & 0xFF
if k == 27:
break
print cv2.countNonZero(thresh)
cv2.destroyAllWindows()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With