【问题标题】:python find most upper left coordinate and corners in opencv numpypython在opencv numpy中找到最左上角的坐标和角
【发布时间】:2021-06-29 03:41:34
【问题描述】:

使用 python,给定一个点顶点和质心的 numpy 数组,我如何找到最左上角的顶点(沿 45 度轴)、右上角、左下角和右下角顶点?

很容易找到最右或最高的,(它只是 Max X 坐标,或 Max Y)

在这个例子中,(6,7) 不是最左边的 (4,10),也不是最高的 (10,5);但是它是最左上角的。

我希望能够扩展并快速排序最靠近左上角的第二个或第三个。

注意:有时坐标可能不直接位于 45 度轴上。

目前使用 Python,带有 numpy 和 opencv。

注意:“它很容易找到最右或最高的,(它只是 Max X 坐标,或 Max Y)” 如果有办法将参考系更改为 45 度轴角,他们可以相应地找到最左上角或右上角。试图找到执行此操作的方法,

更新:查看此示例,希望将其扩展到左下角和右下角

https://stackoverflow.com/a/64659569/15435022

是左下角吗?

bottom_left = sorted(keypoints_to_search, key=lambda p: (-p.pt[0]) - (p.pt[1]))[0] 

【问题讨论】:

  • "(3,8) 不是最左边的 (5)" -- 3 小于 5,因此它比 (5,10) 更靠左。你的插图是不正确的,要么那个点的位置错误,要么它的坐标是错误的。
  • 嗨 @DanMašek 感谢刚刚修复
  • 好的。那么现在,你对左上最点的定义是什么?这个点有什么数学性质?在这种情况下,绝对左上角是什么(即我们从哪里测量)?右上角呢? |为方便起见,我们将原点设置为 (0,0)。假设我们有 3 个点,都与原点的距离相同:(0,1)、(1,0)、(sqrt(0.5), sqrt(0.5))。其中最右上角的是哪一个?让我们将第三个点更改为(1-sqrt(0.5), 1-sqrt(0.5)),这样所有东西都与(1,1) 的距离相同。现在最右上的是哪个?为什么?
  • hi @DanMašek 有问题,注意:“它很容易找到最右或最高的,(它只是 Max X 坐标,或 Max Y)”如果有办法改变框架参考(相对论点),以45度的轴角,他们可以找到最左上角或右上角。试图找到执行此操作的方法,
  • 哦,只是坐标的简单变换。 getRotationMatrix2D 然后将其用于transform。 | pastebin.com/d6g5TXem | i.imgur.com/pnZrKYJ.png |像那样? (制作可视化需要更长的时间:D)

标签: python python-3.x numpy opencv


【解决方案1】:

您在问题中更新的另一个stackoverflow answer 很好地回答了这个问题。

基本上你要找的东西可以放在下面的python函数中:-

def order_points(pts):
    # initialzie a list of coordinates that will be ordered
    # such that the first entry in the list is the top-left,
    # the second entry is the top-right, the third is the
    # bottom-right, and the fourth is the bottom-left
    rect = np.zeros((4, 2), dtype = "float32")

    # the top-left point will have the smallest sum, whereas
    # the bottom-right point will have the largest sum
    s = pts.sum(axis = 1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]

    # now, compute the difference between the points, the
    # top-right point will have the smallest difference,
    # whereas the bottom-left will have the largest difference
    diff = np.diff(pts, axis = 1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]

    # return the ordered coordinates
    return rect

参考:https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-29
    • 2016-01-28
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 2018-08-25
    • 2018-12-29
    • 2013-12-18
    相关资源
    最近更新 更多