Получение ошибок при генерации связанных компонентов с opencv3 - PullRequest
0 голосов
/ 08 ноября 2019

Я хочу использовать функцию cv2.connectedComponentsWithStats для получения подключения

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)

Но я получаю сообщение об ошибке

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

Что я должен изменить, чтобы получить егоправо?

1 Ответ

0 голосов
/ 08 ноября 2019

Вам необходимо преобразовать тип данных изображения в uint8

Попробуйте это

bin_uint8 = (binarized * 255).astype(np.uint8)
output = cv2.connectedComponentsWithStats(bin_uint8, connectivity,cv2.CV_32S)
...