通过在此图像中应用 find_contour(opencv 模块)可能只获得 5 个对象(每个符号一个):https://docs.google.com/file/d/0ByS6Z5WRz-h2WHEzNnJucDlRR2s/edit ?
现在我获得了 64 个对象
之后,我想检索 Humoments 并与其他图像进行比较。
现在我只尝试对相同的图像进行一点翻译,为了测试它返回它们是相同的。
我的问题是我如何才能只获得 5 个对象来应用体量,或者是否有其他解决方案来计算图像的体量?
import cv2
im = cv2.imread('Sassatelli 1984 n. 165 mod1.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(imgray, (0,0), 5)
cv2.imshow('Blur', blur)
cv2.waitKey()
th = 20
edges = cv2.Canny(blur, th, th*3)
cv2.imshow('canny',edges)
cv2.waitKey()
contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print('objects found')
print(len(contours))
cnt = contours[0]
cv2.drawContours(blur,contours,-1,(0,255,0),3)
cv2.imshow('draw contours',blur)
cv2.waitKey()
moments = cv2.moments(cnt)
最佳答案
案例一:图片保存为jpg格式的问题
当您以 jpg 格式保存仅黑白(即仅像素值 0 和 255)图像时,存在有损压缩,这会改变像素值。如果你想看,创建这样的图像,将其保存为 jpg,打开保存的图像并缩放到黑白边缘。您可以看到像素值的变化。
所以当你找到轮廓时,你期望只有白色的物体,但实际上,也有一些中间值,这也被认为是轮廓。它增加了轮廓的数量。
所以为了避免这个问题,
cv2.RETR_EXTERNAL
findcontours() 函数的标志。import cv2
import numpy as np
img = cv2.imread('sof.jpg')
gray = cv2.imread('sof.jpg',0)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
cv2.drawContours(img,contours,-1,(0,255,0),2)
cv2.imshow('img',img),cv2.waitKey(0),cv2.destroyAllWindows()
关于image-processing - 通过 find_contour 获取每个标志的 1 个轮廓并在 cv2 中检索其 Humoments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14430932/