Home>

I'm trying to calculate the number of non-zero brightness pixels in a gray image.
When I used cv2.countNonZero, TypeError: Expected Ptrfor argument'% s'
I got this error and want to resolve this error.

---->8 cnt = cv2.countNonZero (thresh)
TypeError: Expected Ptr<cv :: UMat>for argument'% s'
Corresponding source code
import cv2
import numpy as np
img = cv2.imread ('/content/Blue.jpg')
# Count the number of pixels with 1 pixel.
thresh = cv2.threshold (img, 1,255, cv2.THRESH_BINARY)
cnt = cv2.countNonZero (thresh)
print (cnt)
What I tried

I performed mask processing and used the image (/content/Blue.jpg) divided into R, G, B, so I checked if I used a library such as pil in the previous processing. ..

Supplementary information (FW/tool version, etc.)

It may be a naive question, but thank you ...
Blue.jpg is the following image.

  • Answer # 1

    By not using cv2.countNonZero, I was able to calculate the number of pixels with non-zero brightness.
    It's not the ultimate solution to my question, but I was able to achieve my goal.

    import cv2
    import numpy as np
    img = cv2.imread ('/content/Blue.jpg')
    # Count the number of pixels whose brightness is 1 or more.
    n_pixels = (0