二维多边形的交集
我有两个 OpenCV 凸包 numpy 数组,我想检查交集,而不创建 for 循环或创建图像并对它们执行 numpy.bitwise_and ,这两个数组都是Python 中相当慢。数组看起来像这样:
[[[x1 y1]]
[[x2 y2]]
[[x3 y3]]
...
[[xn yn]]]
将 [[x1 y1]] 视为一个元素,我想在两个 numpy ndarray 之间执行交集。我怎样才能做到这一点?我发现了一些类似性质的问题,但我无法从那里找到解决方案。
I have two numpy arrays that are OpenCV convex hulls and I want to check for intersection without creating for loops or creating images and performing numpy.bitwise_and
on them, both of which are quite slow in Python. The arrays look like this:
[[[x1 y1]]
[[x2 y2]]
[[x3 y3]]
...
[[xn yn]]]
Considering [[x1 y1]] as one single element, I want to perform intersection between two numpy ndarrays. How can I do that? I have found a few questions of similar nature, but I could not figure out the solution to this from there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将数组的视图用作 intersect1d 函数的单个维度,如下所示:
这将创建每个数组的视图,将每一行更改为值的元组。然后执行交集,并将结果更改回原始格式。这是使用它的示例:
打印:
You can use a view of the array as a single dimension to the intersect1d function like this:
This creates a view of each array, changing each row to a tuple of values. It then performs the intersection, and changes the result back to the original format. Here's an example of using it:
This prints:
您可以使用 http://pypi.python.org/pypi/Polygon/2.0.4 ,这是一个示例:
要将 cv2.findContours 的结果转换为多边形点格式,您可以:
这会将形状从 (N, 1, 2) 转换为 (N, 2)
以下是一个完整的示例:
输出:
you can use http://pypi.python.org/pypi/Polygon/2.0.4, here is an example:
To convert the result of cv2.findContours to Polygon point format, you can:
This will convert the shape from (N, 1, 2) to (N, 2)
Following is a full example:
Output:
这就是我为完成工作所做的:
我必须使用 for 循环,这看起来有点乏味,尽管它给了我预期的结果。任何更好的方法将不胜感激!
So this is what I did to get the job done:
I had to use for loop, and this altogether looks a bit tedious, although it gives me expected results. Any better methods would be greatly appreciated!
受到 jiterrace 答案的启发
我在使用 Udacity 深度学习课程(试图找到训练数据和测试数据之间的重叠部分)。
我对“视图”不熟悉,发现语法有点难以理解,当我尝试与用“表”思考的朋友交流时可能也是如此。
我的方法基本上是将形状 (N, X, Y) 的 ndarray 展平/重塑为形状 (N, X*Y, 1)。
1)。 INNER JOIN(比较容易理解,速度慢)
2)。设置交点(快速)
inspired by jiterrace's answer
I came across this post while working with Udacity deep learning class(trying to find the overlap between training and test data).
I am not familiar with "view" and found the syntax a bit hard to understand, probably the same when I try to communicate to my friends who think in "table".
My approach is basically to flatten/reshape the ndarray of shape (N, X, Y) into shape (N, X*Y, 1).
1). INNER JOIN (easier to understand, slow)
2). SET INTERSECTION (fast)