Count Non Zero error!
Here's a part of code
# If foreground construct label mask and count the number of pixels
labelMask = np.zeros(thresh.shape, dtype="uint8")
labelMask[labels == label] = 255
numPixels = cv2.countNonZero(labelMask)
I'm getting this error which I'm not able to understand, even after trying to google for the solution
OpenCV Error: Assertion failed (cn == 1) in countNonZero, file /tmp/opencv3-20170817-33981-15wdezm/opencv-3.3.0/modules/core/src/stat.cpp, line 1352
Traceback (most recent call last):
File "/Users/santhoshdc/23_08_2017/connected_component_Analysis.py", line 28, in <module>
numPixels = cv2.countNonZero(labelMask)
cv2.error: /tmp/opencv3-20170817-33981-15wdezm/opencv-3.3.0/modules/core/src/stat.cpp:1352: error: (-215) cn == 1 in function countNonZero
How do I correct this?
countNonZero() can handle only single channel images.
what is
thresh.shape
? (i bet, your labelsMask has more than one channel.)labelMask
is a matrix that is of size defined by thethresh.shape
yea, exactly,that's why i'm asking you about the shape of your
thresh
array ;)Here's the code
and the shape is ? (if your
img
was not grayscale, it will have 3 or 4 channels !)@berak I already have a processed image in black and white pixels only after applying combination of bilateral filter and canny filter. I get the same error when I pass that image through directly here
labels = measure.label(thresh,neighbors=8,background=0)
I did change the img to gray for a color image. The code works perfectly for a coloured image.
^^ sorry, but i have no idea, what that line does, and the context you're using it in.
Detecting multiple bright spots in an image with Python and OpenCV
I am trying to implement connected-component analysis, with the help of the link on top.
I applied Bilateral Filter and Canny Filter to extract the edges.
I want to remove unwanted small length edges in the image.
do you realize, that all of that should have gone into your original question ?