【发布时间】:2017-12-26 12:09:57
【问题描述】:
我需要从输入图像中计算 Hu 矩。输入图像input 由多个对象组成,因此我需要使用连接组件标记函数对其进行预处理:
# input image is thresholded
(T, thresh) = cv2.threshold(input, 90, 255, cv2.THRESH_BINARY)
# getting the labels of the connected components
output = cv2.connectedComponentsWithStats(thresh, 4, cv2.CV_32S)
num_labels = output[0]
labels = output[1]
stats = output[2]
centroids = output[3]
# for every component in the output image
for c in centroids[1:num_labels]:
img_moments = cv2.moments(c)
hu = cv2.HuMoments(img_moments)
但是,这并没有给我正确的组件 Hu 矩值。最初我使用阈值图像来获取时刻cv2.moments(thresh),但是当它们是图像中的多个组件时,这没有用。我正在使用 Python 2 和 OpenCV 3。
更新:为了记录,我已经获得了正确数量的图像标签,在这种情况下input 图像有 10 个组件 + 1 个背景标签,即 11 个标签,我知道第一个标签是背景,因此数组值全为零。我想要获取其余标签的值(从 1 到 n 标签)并将这些值解析为 Numpy 数组以单独计算时刻。
更新 2:添加了阈值源图像。
【问题讨论】:
-
“没有给我正确的 Hu 时刻”是什么意思?提供图片也无妨...
-
@Piglet 我已经用图像中显示的 4 种对象训练了一个距离分类器,因此我知道每种对象的 Hu 矩的近似值。
img_moments = cv2.moments(c)的输出值不是所需的值,因为返回的矩值都与用于训练的矩值不相似。
标签: python opencv computer-vision connected-components