python 使用Python和OpenCV顺时针排序坐标

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用Python和OpenCV顺时针排序坐标相关的知识,希望对你有一定的参考价值。

from imutils import perspective
import numpy as np

def order_points_new(pts):
    # sort the points based on their x-coordinates
    xSorted = pts[np.argsort(pts[:, 0]), :]

    # grab the left-most and right-most points from the sorted
    # x-roodinate points
    leftMost = xSorted[:2, :]
    rightMost = xSorted[2:, :]

    # now, sort the left-most coordinates according to their
    # y-coordinates so we can grab the top-left and bottom-left
    # points, respectively
    leftMost = leftMost[np.argsort(leftMost[:, 1]), :]
    (tl, bl) = leftMost

    # if use Euclidean distance, it will run in error when the object
    # is trapezoid. So we should use the same simple y-coordinates order method.

    # now, sort the right-most coordinates according to their
    # y-coordinates so we can grab the top-right and bottom-right
    # points, respectively
    rightMost = rightMost[np.argsort(rightMost[:, 1]), :]
    (tr, br) = rightMost

    # return the coordinates in top-left, top-right,
    # bottom-right, and bottom-left order
    return np.array([tl, tr, br, bl], dtype="float32")

pts = np.array([[10,10],
               [10,20],
               [20,20],
               [30,10]])
ordered_old = perspective.order_points(pts)
ordered_new = order_points_new(pts)

print("raw points")
print(pts.astype("int"))
print("ordered by Euclidean distance")
print(ordered_old.astype("int"))
print("orderd by y-coordinates")
print(ordered_new.astype("int"))
print("")

以上是关于python 使用Python和OpenCV顺时针排序坐标的主要内容,如果未能解决你的问题,请参考以下文章

[python] 工作记录一利用opencv,numpy旋转图片无黑边(轮子必须由我造!!!)

在Python中理解顺时针螺旋的打印矩阵

python——n*n矩阵顺时针旋转90度

剑指offer顺时针打印矩阵python

python之n*n矩阵顺时针旋转90度

顺时针打印矩阵元素(python实现)