【问题标题】:Getting errors during generating connectedcomponents wih opencv3使用 opencv3 生成连接组件时出错
【发布时间】:2020-03-05 00:28:11
【问题描述】:

我想使用函数 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'

我应该改变什么才能让它正确?

【问题讨论】:

    标签: python-3.x image-processing opencv3.0


    【解决方案1】:

    需要将图片数据类型转换为uint8

    试试这个

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

    【讨论】:

    • 这对我有用,如果我是 OP 我会接受这个答案
    猜你喜欢
    • 2011-05-29
    • 1970-01-01
    • 2018-09-22
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多