如何使用open cv

时间:2018-02-18 16:36:08

标签: python opencv

好的,既然没有人回答让我试着改变我的问题, 怎么可能试图找出这个盒子?

想要检测的图片

得到这个:

想要的图片

由于盒子里面有白色字母,因此我遇到了麻烦。(我认为) 任何人都可以提供解决方案吗?

我开始使用简历,做一个简单的项目来学习。 我试图在盒子里面装盒子并计算它们。 我正在检测边缘并执行Thresh二进制inv。 我得到了这个,我觉得这很好,我应该可以使用,

处理后的图片

在我继续进行形状检测后,我只找到一个盒子,一个大盒子。

我想到的是: A)问题是里面的盒子没有完成" B)线不是段

但是对于我读到的内容,如果它对边缘进行处理,它应该看到它有4个,并被检测为矩形。

这是一个代码

thresh = cv2.threshold(edges, 177, 255, cv2.THRESH_BINARY_INV)[1]

cv2.imshow("Image", thresh)
cv2.waitKey(0)

cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
sd = ShapeDetector()

在我迭代了边缘并尝试以绿色绘制它们之后

我的形状检测器

class ShapeDetector:
def __init__(self):
    pass

def detect(self, c):
    print("ola")
    # initialize the shape name and approximate the contour
    shape = "unidentified"
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.04 * peri, True)

    if len(approx) == 4:
        # compute the bounding box of the contour and use the
        # bounding box to compute the aspect ratio
        (x, y, w, h) = cv2.boundingRect(approx)
        ar = w / float(h)
        print( "Found one!")
        shape = "rectangulo"

    return shape

提前致谢

1 个答案:

答案 0 :(得分:1)

而不是cv2.RETR_EXTERNAL(仅检索外部轮廓),您应该使用其他内容,例如cv2.RETR_LIST(包含所有轮廓的简单列表)或cv2.RETR_TREE(层次结构中的所有轮廓)。

请参阅https://docs.opencv.org/3.1.0/d9/d8b/tutorial_py_contours_hierarchy.html

编辑: RETR_TREE更复杂,但为您提供其他轮廓内部轮廓的信息,因此您可以忽略其中一些轮廓。

如果想要完美的矩形,则应绘制boundingRect()的结果而不是原始轮廓。

如果您发现重叠的矩形,您还可以添加更多条件来消除它们(例如,w和h应介于最小值和最大值之间)。

相关问题