简体   繁体   English

如何用开放的简历填充盒子内的盒子

[英]how to fill boxes inside boxes with open cv

Ok, since no one is answering let me try and change my question, how could one go about trying to identify this boxes? 好吧,既然没有人回答让我尝试改变我的问题,怎么可能试图找出这个盒子呢?

Picture where i want to detect 想要检测的图片

to get this: 得到这个:

Picture i want to get 我想要的图片

im having trouble since there are white letters inside the box.(i think) Can anyone provide a soluction? 我有麻烦,因为盒子里面有白色字母。(我认为)任何人都可以提供解决方案吗?

I'm starting out with CV, doing a simple project to learn. 我开始使用简历,做一个简单的项目来学习。 I'm trying to get boxes inside boxes and count them. 我正试图在盒子里面装盒子并计算它们。 I'm detecting the edges and doing a Thresh binary inv. 我正在检测边缘并执行Thresh二进制inv。 I'm getting this, which i think is pretty good and i should be able to work with, 我得到了这个,我觉得这很好,我应该可以使用,

picture after processing 处理后的图片

after i go on doing a detection of shapes, but i only find one box, te big one. 在我继续检测形状后,我只找到一个盒子,一个大盒子。

I'm to the point of thinking: A) the problem is that the boxes inside are not "completed" B) the lines are not a segment 我的想法是:A)问题是里面的盒子没有“完成”B)线条不是段

But for what i read, if it dectets the edges it should see it has 4, and detected as a rectangle. 但是对于我所读到的内容,如果它对边缘进行迭代,它应该看到它有4个,并被检测为矩形。

Here is a code 这是一个代码

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()

after i iterate thourg the edges and try to draw over them in green 在我迭代了边缘并尝试用绿色画出它们之后

And my shape dectector 而我的形状检测器

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

Thanks in advance 提前致谢

Instead of cv2.RETR_EXTERNAL (which retrieves only external contours) you should use something else such as cv2.RETR_LIST (simple list with all contours) or cv2.RETR_TREE (all contours in a hierarchy). 您应该使用其他内容,例如cv2.RETR_LIST(包含所有轮廓的简单列表)或cv2.RETR_TREE(层次结构中的所有轮廓),而不是cv2.RETR_EXTERNAL(仅检索外部轮廓)。

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

EDIT: RETR_TREE is more complex but gives you the information of what contours are inside other contours, so you can ignore some of them. 编辑:RETR_TREE更复杂,但为您提供其他轮廓内部轮廓的信息,因此您可以忽略其中一些轮廓。

If you want perfect rectangles, you should draw the result of the boundingRect() instead of the original contour. 如果想要完美的矩形,则应绘制boundingRect()的结果而不是原始轮廓。

If you find overlapping rectangles, you can also add more conditions to eliminate them (w and h should be between a minimum and a maximum for example). 如果您发现重叠的矩形,您还可以添加更多条件来消除它们(例如,w和h应介于最小值和最大值之间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM