Ask Your Question
0

Count Non Zero error!

asked 2017-08-23 06:49:24 -0600

Santhosh1 gravatar image

updated 2017-08-23 06:52:44 -0600

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?

edit retag flag offensive close merge delete

Comments

1

countNonZero() can handle only single channel images.

what is thresh.shape ? (i bet, your labelsMask has more than one channel.)

berak gravatar imageberak ( 2017-08-23 07:03:26 -0600 )edit

labelMask is a matrix that is of size defined by the thresh.shape

Santhosh1 gravatar imageSanthosh1 ( 2017-08-23 07:14:32 -0600 )edit

yea, exactly,that's why i'm asking you about the shape of your thresh array ;)

berak gravatar imageberak ( 2017-08-23 07:26:26 -0600 )edit

Here's the code

thresh = cv2.threshold(img,200,255, cv2.THRESH_BINARY)[1]
Santhosh1 gravatar imageSanthosh1 ( 2017-08-23 07:36:16 -0600 )edit
1

and the shape is ? (if your img was not grayscale, it will have 3 or 4 channels !)

berak gravatar imageberak ( 2017-08-23 07:44:11 -0600 )edit

@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 herelabels = measure.label(thresh,neighbors=8,background=0)

Santhosh1 gravatar imageSanthosh1 ( 2017-08-23 08:10:11 -0600 )edit

I did change the img to gray for a color image. The code works perfectly for a coloured image.

Santhosh1 gravatar imageSanthosh1 ( 2017-08-23 08:11:30 -0600 )edit

^^ sorry, but i have no idea, what that line does, and the context you're using it in.

berak gravatar imageberak ( 2017-08-23 08:12:30 -0600 )edit

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.

Santhosh1 gravatar imageSanthosh1 ( 2017-08-23 08:18:56 -0600 )edit
1

do you realize, that all of that should have gone into your original question ?

berak gravatar imageberak ( 2017-08-23 08:30:11 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-08-23 07:55:25 -0600

Santhosh1 gravatar image

I did convert it

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edit flag offensive delete link more

Comments

@berak Thank You ;) Fixed the problem

Santhosh1 gravatar imageSanthosh1 ( 2017-08-23 07:56:38 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-08-23 06:49:24 -0600

Seen: 15,437 times

Last updated: Aug 23 '17