Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Python count pixels

Tags:

python

opencv

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.

like image 738
RyeRai Avatar asked Aug 23 '17 09:08

RyeRai


People also ask

How do you count pixels in OpenCV?

Counting Pixels This sum() function can be used to count the number of pixels on the basis of the required criteria.

How do I know how many pixels a picture is?

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.

How do I Count the number of pixels in OpenCV?

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.

How do I Count the number of pixels in a image?

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.

How to extract only white pixels from a CV2 image using NumPy?

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.

What is OpenCV and how to use it?

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:


1 Answers

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()
like image 154
I.Newton Avatar answered Nov 01 '22 09:11

I.Newton