【问题标题】:Unsupported combination of input and output array formats in function cv::reduce函数 cv::reduce 中不支持的输入和输出数组格式组合
【发布时间】:2019-06-10 10:23:00
【问题描述】:

我正在尝试运行我在 GitHub 上找到的行分段代码。当程序到达这行代码时:

def invert(im):
    im = abs(255 - im)
    im = im / 255

    return im


def enhance(im):
    kernel = np.ones((5, 5), np.uint8)
    im = cv2.erode(im, kernel, iterations=1)
    kernel = np.ones((15, 15), np.uint8)
    im = cv2.dilate(im, kernel, iterations=1)
    return im


im = cv2.imread(filename, 0)
imbw = sauvola.binarize(im, [20, 20], 128, 0.3)
im = invert(im)
im = enhance(im)
hist = cv2.reduce(im, 1, cv2.REDUCE_SUM, dtype=cv2.CV_32F)

我收到此错误:

错误:OpenCV(3.4.1) C:\projects\opencv-python\opencv\modules\core\src\matrix_operations.cpp:1111: 错误:(-210) 函数中不支持输入和输出数组格式的组合cv::reduce

我已经尝试了所有可用于 cv2.reduce() 函数的 dtype 值。我还尝试使用 numpy 更改图像的数据类型。但是,我仍然遇到同样的错误。

完整代码:https://github.com/smeucci/LineSegm/blob/master/python/linesegm/lib/linelocalization.py#L41

【问题讨论】:

  • 您的示例中真的需要 imbw = sauvola.binarize(im, [20, 20], 128, 0.3) 吗?我没有实现(很可能在你的仓库中),但我认为不需要,因为你不使用imbw。写了我的答案,因为没有那行代码。

标签: python opencv image-segmentation


【解决方案1】:

imread() 返回 uchar 数据,但您处理它时就像获得浮点数一样。

im = im / 255

例如,如果您将这行代码应用 uchar OpenCV 矩阵,这行代码将使所有像素都为零。

在读取图像后将您的im 像素格式转换为浮点数。这段代码对我有用:

im = cv2.imread("im.jpg", 0)
im = np.float32(im)
im = invert(im)
im = enhance(im)
hist = cv2.reduce(im, 1, cv2.REDUCE_SUM, dtype=cv2.CV_32F)

【讨论】:

    猜你喜欢
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 2018-07-25
    • 2018-11-03
    • 2013-05-19
    • 1970-01-01
    相关资源
    最近更新 更多