python - 使内部矩形适合关节

标签 python opencv opencv3.0

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

enter image description here

如何调整内部矩形,使矩形不重叠并使用所有点?基本上使表格单元格适合点。

我试过捕获轮廓,效果很好:

enter image description here

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

最佳答案

我找到了 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]])

关于python - 使内部矩形适合关节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52335553/

相关文章:

opencv - 即使出现#include 错误,Visual Studio 也能编译和运行

python - 如何在python中为linux设置复杂的环境变量?

opencv - 使用 GrabCut 去除琐碎的背景

Python OpenCV 视频格式浏览器播放

python - 当 .csv 文件保存在 Python 中的 .zip 文件中时,如何从 url 中抓取 .csv 文件?

python - Django Admin 应用程序还是我自己的?

android - 无法在 Qt 中链接 OpenCV Android

python - 如何在 SST Python 中设置 results_directory

python - 使用子进程同时执行两个进程的问题

c++ - 如何将 CV_64F 类型的 Matrix 转换为具有 Double 元素的一维数组