【问题标题】:Finding corresponding rectangle coordinates given grid points给定网格点查找对应的矩形坐标
【发布时间】:2019-06-23 13:13:17
【问题描述】:

我已经使用 openCV 处理了图像以获得图像模式。图像模式分别由 2 个水平和垂直线的 Python 列表表示。线条代表图案的边界。

fx = horizontal lines fy = vertical lines

每个列表都根据距图像左上角的距离顺序排列。接下来,我使用以下方法计算发现的线的交点:

def get_corners(fx,fy):
    corners = []
    for x_line in fx:
        for y_line in fy:
            corner = get_intersection(x_line,y_line)
            if corner is not None:
                corners.append(corner)

这应该给我corners(格式:(x,y)),从左到右,从上到下。现在我想使用这些坐标从图像中裁剪出矩形。

corners 列表的大小各不相同,模式堆叠,这意味着它们有共同点。给定点列表,fxfy 的行列表的大小和大小:

如何使用点来裁剪矩形?

如果需要,请随时更改get_corners()

这是一个示例:模式检测会在 2x2 中生成 4 个可能的矩形。这意味着列表points 共有9 个值。

Points: [[],[],[],
         [],[],[],
         [],[],[]]

我可以使用以下方法裁剪第一个矩形:

x1,y1 = points[0] #top left corner of the first pattern
x2,y2 = points[5] #bottom right corner of the first pattern
#rectangle
rectange = img[y1:y2,x1:x2]

【问题讨论】:

  • 你有一个真实的输出例子和预期的例子吗?为了看看它有什么问题,因为我不完全确定已经理解你的问题。谢谢!
  • 我没有例子,因为我自己无法完成算法。我更新了我的问题以显示我到目前为止所拥有的内容,但我对分数使用了硬值。我需要能够根据我获得的分数来选择这些值,这取决于我获得的行数。
  • 你为什么还要关心角点?线条更容易处理(只需以与提取角相同的方式提取矩形)。两条正交线怎么可能不相交?它们是线吗?
  • 线条一直贯穿图像。它们表示为(rho, theta),这意味着一条水平线可以代表多个矩形。我知道它们从哪里开始或结束的唯一方法是使用交叉点。你能解释一下你将如何使用这些线条吗?
  • 至少添加一个输入图像样本,突出显示您需要的矩形

标签: python-3.x algorithm opencv


【解决方案1】:

在这里暗中刺伤,但这是你的中间形象吗?我还假设您要区分正方形和矩形,也就是说,您不需要正方形,只需要矩形。

如果是这种情况,我会使用以下步骤:

cnt_rectangles = 0
rectangle_list = []
for index in np.arange(len(points)-1):
   p = points[index]
   q = points[index+1]
   if (p[0] == q[0]) || p[1] == q[1]
       #the rectangle vertices must not have the same column or row. reject.
       continue
   else if abs(p[0] - q[0]) == abs(p[1] - q[1]):
       #this is a square. reject
       continue
   else:
       #this is a rectangle
       cnt_rectangels+=1
       rectangle_list.append((p, q))

【讨论】:

  • 是的,这就是想法。但这不会只比较相邻点吗?此外,如果矩阵是 3x3,那么您将拥有重叠的矩形。
  • @JoshSharkey 这会将每个点与其他点进行比较,以便您拥有每个组合。你不应该得到重叠的矩形。您是否尝试过解决方案?
【解决方案2】:

您的问题有点不清楚,但这里有一个解决方案,假设您的 img 变量是一个 numpy 数组(因为问题用 opencv 标记),交点索引在 corners 中。请注意,我修改了 get_corners() 函数以将角构建成行而不是单个平面数组,以便于处理,正如您所说的那样。

import numpy as np

def get_corners(fx, fy):
    corners = []
    for x_line in fx:
        row = [] # NOTE: we're building rows here!
        for y_line in fy:
            corner = get_intersection(x_line, y_line)
            if corner is not None:
                row.append(corner)
        corners.append(row)

def get_crops(img, corners):
    crops = []
    for i, row in enumerate(corners[0:-1]):
        for j in range(len(row) - 1):
            x1, y1 = row[j]
            next_row = corners[i+1]
            x2, y2 = next_row[j+1]
            # this slicing works with my test_img,
            # but you may need to adjust it for yours
            crops.append(img[x1:x2+1, y1:y2+1])
    return crops

test_corners = [
    [ [0, 0], [0, 1], [0, 2] ],
    [ [1, 0], [1, 1], [1, 2] ],
    [ [2, 0], [2, 1], [2, 2] ],
]

test_img = np.array(corners) # test img to easily see indices

crops = get_crops(test_img, test_corners)

for i, crop in enumerate(crops):
    print("crop [{}]: {}\n".format(i, crop))

这是测试运行的输出。当然,真实的图像会有其他数据,但这显示了如何对我的test_img numpy 数组进行切片。

crop [0]: [[[0 0] [0 1]]
           [[1 0] [1 1]]]

crop [1]: [[[0 1] [0 2]]
           [[1 1] [1 2]]]

crop [2]: [[[1 0] [1 1]]
           [[2 0] [2 1]]]

crop [3]: [[[1 1] [1 2]]
           [[2 1] [2 2]]]

【讨论】:

    猜你喜欢
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    相关资源
    最近更新 更多