gpt4 book ai didi

python - 获取numpy矩阵中正方形的坐标

转载 作者:行者123 更新时间:2023-11-28 17:11:35 24 4
gpt4 key购买 nike

给定以下 numpy 矩阵

import numpy as np
np_matrix = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,2,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0]
,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,2,0,0,1,0,0,0]
,[0,0,0,0,0,0,0,0,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,1,2,0]
,[1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0]
,[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0]
,[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0]
,[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0]
,[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0]
,[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0]
,[2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0]])

print(np_matrix)
print(np_matrix.shape)

可以表示为图像。

enter image description here

这里有一些关于输入矩阵的假设:

  • 矩阵形状可以变化。它不是固定尺寸。
  • 方 block 的数量因矩阵而异。但是矩阵中总是至少有一个正方形。
  • 每个绿点周围都有一个正方形,正方形内部有一个点。正方形内的点用数字 2 标记
  • 正方形在 X 轴上是连续的,正方形的边界永远不会与其他正方形的边界重叠。正方形边框用数字 1 标记
  • 空白用数字0标记

我有两个问题。以最有效的方式如何获得包含所有绿点坐标的数组?我怎样才能得到一个包含绿点周围正方形所有角的数组?

enter image description here

这是我为这个例子寻找的结果:

green_dots_coordinates = [
[0,0], # dot 1 with coordinates x1, y1 inside square 1
[7,12], # dot 2 with coordinates x2, y2 inside square 2
[16,27],# dot 3 with coordinates x3, y3 inside square 3
[29,21],# dot 4 with coordinates x4, y4 inside square 4
[34,7], # dot 5 with coordinates x5, y5 inside square 5
]



sqaures_corners_coordinates = [
#square nr 1
[
[0,6], # x1, y1
[2,6], # x2, y2
[0,0], # x3, y3
[2,0], # x4, y4
],

#square nr 2
[
[3,14], # x1, y1
[9,14], # x2, y2
[3,7], # x3, y3
[9,7], # x4, y4
],

#square nr 3
[
[12,31], # x1, y1
[23,31], # x2, y2
[12,24], # x3, y3
[23,24], # x4, y4
],

#square nr 4
[
[25,22], # x1, y1
[32,22], # x2, y2
[25,15], # x3, y3
[32,15], # x4, y4
]

,#square nr 5
[
[33,13], # x1, y1
[35,13], # x2, y2
[33,0], # x3, y3
[35,0], # x4, y4
],

]

最佳答案

坦率地说,您的输出格式非常奇怪,需要大量颠倒索引的顺序,(并且使输出对于索引原始数组毫无用处)但这有效:

def find_boxes(np_matrix):
np_mat = np_matrix[::-1, :] # reversed in expected output
def find_extent(arr, val):
xn = arr.size
x0 = np.flatnonzero(arr == 1)
xi = np.searchsorted(x0, val, side = 'right')
if xi == x0.size:
x1 = x0[xi-1]
x2 = xn - 1
elif xi == 0:
x1 = 0
x2 = x0[xi]
else:
x1 = x0[xi-1]
x2 = x0[xi]
return np.array([x1, x2])

green = np.where(np_mat == 2)
green = tuple(g[np.argsort(green[-1])] for g in green)
coords = np.empty((green[0].size, 2, 4))

for i, (x, y) in enumerate(zip(*green)):
coords[i, 0] = np.tile(find_extent(np_mat[x, :], y), 2)
coords[i, 1] = np.repeat(find_extent(np_mat[:, y], x)[::-1], 2) # reversed again
return np.stack(green)[::-1].T, coords.swapaxes(1,2).astype(int)
# reversed again and transposed

测试:

find_boxes(np_matrix)
Out:
(array([[ 0, 0],
[ 7, 12],
[16, 27],
[29, 21],
[34, 7]], dtype=int32),
array([[[ 0, 6],
[ 2, 6],
[ 0, 0],
[ 2, 0]],

[[ 3, 14],
[ 9, 14],
[ 3, 7],
[ 9, 7]],

[[12, 31],
[23, 31],
[12, 24],
[23, 24]],

[[25, 22],
[32, 22],
[25, 15],
[32, 15]],

[[33, 13],
[35, 13],
[33, 0],
[35, 0]]]))

关于python - 获取numpy矩阵中正方形的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47253243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com