Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting errors during generating connectedcomponents wih opencv3

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?

like image 910
PRATHAMESH Avatar asked Aug 12 '26 10:08

PRATHAMESH


1 Answers

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)
like image 86
abhilb Avatar answered Aug 14 '26 08:08

abhilb