如何在2D numpy数组中找到行?
我有一个 2D Numpy阵列,我想找到水平和垂直线的边界点。
gray_img = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 255, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 255, 255, 255, 0],
[0, 255, 255, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 0, 0, 255, 255, 255, 255],
[0, 255, 255, 0, 0, 255, 255, 255, 255],
[0, 255, 255, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 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]])
desired_outcome = [ [[1,1],[10,1]],
[[1,2],[10,2]],
[[1,3],[2,3]], ...]
这是我想找到的线:
后来,我想删除较小的线,以保持超过2点距离的线条。
I have a 2D numpy array and I want to find boundary points of both horizontal and vertical lines.
gray_img = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 255, 255, 255, 255, 255, 0],
[0, 255, 255, 255, 255, 255, 255, 255, 0],
[0, 255, 255, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 0, 0, 255, 255, 255, 255],
[0, 255, 255, 0, 0, 255, 255, 255, 255],
[0, 255, 255, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 0, 0, 0, 0, 0, 0],
[0, 255, 255, 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]])
desired_outcome = [ [[1,1],[10,1]],
[[1,2],[10,2]],
[[1,3],[2,3]], ...]
Here are the lines I want to find:
Later, I want to remove the smaller lines to keep only those with more than 2 points distance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
垂直线:
输出:
水平线:
输出:
vertical lines:
output:
horizontal lines:
output:
这是基于前缀总和的水平线算法:
只需更改索引以获取垂直线即可。
您可以使用长度数组来过滤出所需的线。
Here's an algorithm for horizontal lines based on prefix sums:
Just change the indexing to get vertical lines.
You can use the lengths array to filter out which lines you want.
编辑:收集水平和垂直线,我还减少了我在第一稿中使用的一些多余的比较:当扫描一个方向时,只有右/底端坐标才能更新,而左/顶部则是恒定的,等于当前线段扫描的开始。仍然有一些可以压缩的多余代码。
EDIT2:添加了列表的最终格式,如问题所示。
它首先找到并列出水平线,如果垂直行业必须像示例输出一样先出现,则只需将第二个遍历放在顶部即可。
然后,您可以穿越结果列表并计算该距离(或者是指线路的Lenght),并仅将较长的行传输到另一个列表。
EDIT: Collecting both Horizontal and Vertical lines, also I reduced some superfluous comparisons that I used in the first draft: when scanning there is one direction, thus only the right/bottom end coordinates have to be updated, while the left/top are constant, equal to the beginning of the scanning of the current line segment. There is still some superfluous code that can be compressed.
EDIT2: Added the final formatting of the list as in the question.
It finds and lists first the horizontal lines, if the vertical have to come first as in your example output, then just put the second traversal on top.
Then you may traverse the result list and compute that distance (or you mean the lenght of the lines) and transfer only the longer lines to another list.
这是你想要的吗?
Is this what you want?