使内部矩形适合关节

时间:2018-09-14 16:02:05

标签: python opencv opencv3.0

使用关节数组,如下所示:

enter image description here

如何拟合内部矩形,以使没有矩形重叠并且使用所有点?基本上使表格单元格适合这些点。

我尝试抓住轮廓,效果很好:

enter image description here

找到22分。如何将这些点拟合到内部多边形?例如。在这张图片中找到21个矩形。

1 个答案:

答案 0 :(得分:1)

我发现了the joint array through this method,我想这是一个延续。

我想出了一个快速而肮脏的解决方案。这仅适用于完美的水平/垂直对齐,并且如果列中有间隙,则不会处理。

# First dilate the image
kernel = np.ones((5,5),np.uint8)
dilation = cv.dilate(img,kernel,iterations = 1)

# Find contours then points
(img, contours, hierarchy) = cv.findContours(dilation, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    points = []
    for con in contours:
        if (cv.contourArea(con)>0):
            M = cv.moments(con)
            cX = int(M["m10"] / M["m00"])
            cY = int(M["m01"] / M["m00"])
            points.append([cY, cX])

# attempt at finding rectangles
map = {}
for p in points:
    map[p[1]] = []

for p in points:
    map[p[1]].append(p[0])

# Check for rectangles
keys = sorted(map.keys(), key=int)
for i in range(len(keys)-1):
    one = np.array(map[keys[i]])
    two = np.array(map[keys[i+1]])
    intersect = np.in1d(one,two)
    intersect2 = np.in1d(two,one)

    # If two horizontal collections have an intersection it's likely a cell
    if (sum(intersect) >= 2):
        intersects = sorted(one[intersect], key=int)
        for x in range(len(intersects)-1):
            rect = [keys[i], intersects[x],keys[i+1], intersects[x+1]]
            showimg(rois[numimg][rect[1]:rect[3],rect[0]:rect[2]])