3

I've want to use the function cv2.connectedComponentsWithStats to get the connectivity

from skimage import io
from skimage.color import rgb2gray
img1 = io.imread('Bingo/25.jpg', as_gray=True)

from scipy import ndimage

def sobel_filters(img):
    Kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], np.float32)
    Ky = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], np.float32)

    Ix = ndimage.filters.convolve(img, Kx)
    Iy = ndimage.filters.convolve(img, Ky)

    G = np.hypot(Ix, Iy)
    G = G / G.max() * 255
    theta = np.arctan2(Iy, Ix)
    return G


e=sobel_filters(img1)
threshold = 70

# make all pixels < threshold black
binarized = 1.0 * (e > threshold)
connectivity = 4 # or whatever you prefer

output = cv2.connectedComponentsWithStats(binarized, connectivity,cv2.CV_32S)

But I'm getting an error

error: (-215:Assertion failed) iDepth == CV_8U || iDepth == CV_8S in function 'cv::connectedComponents_sub1'

What should I change to get it right?

1 Answer 1

4

You need to convert the image data type to uint8

Try this

bin_uint8 = (binarized * 255).astype(np.uint8)
output = cv2.connectedComponentsWithStats(bin_uint8, connectivity,cv2.CV_32S)
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me, if I were OP I would accept this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.