四边形坐标顺时针排序
来源:互联网 发布:angularjs2 seo问题 编辑:程序博客网 时间:2024/06/04 15:59
1. 做图像检测的时候处理数据经常遇到给出矩形的四个坐标点,要求找出左上角坐标并对乱序的坐标按顺时针或者逆时针进行排序。Its extremely important to maintain a consistent ordering of points !
from scipy.spatial import distance as distimport numpy as npimport mathdef cos_dist(a, b): if len(a) != len(b): return None part_up = 0.0 a_sq = 0.0 b_sq = 0.0 print a, b print zip(a, b) for a1, b1 in zip(a, b): part_up += a1*b1 a_sq += a1**2 b_sq += b1**2 part_down = math.sqrt(a_sq*b_sq) if part_down == 0.0: return None else: return part_up / part_down# this function is confined to rectangledef order_points(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 # now that we have the top-left coordinate, use it as an # anchor to calculate the Euclidean distance between the # top-left and right-most points; by the Pythagorean # theorem, the point with the largest distance will be # our bottom-right point D = dist.cdist(tl[np.newaxis], rightMost, "euclidean")[0] (br, tr) = rightMost[np.argsort(D)[::-1], :] # return the coordinates in top-left, top-right, # bottom-right, and bottom-left order return np.array([tl, tr, br, bl], dtype="float32")def order_points_quadrangle(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 # now that we have the top-left and bottom-left coordinate, use it as an # base vector to calculate the angles between the other two vectors vector_0 = np.array(bl-tl) vector_1 = np.array(rightMost[0]-tl) vector_2 = np.array(rightMost[1]-tl) angle = [np.arccos(cos_dist(vector_0, vector_1)), np.arccos(cos_dist(vector_0, vector_2))] (br, tr) = rightMost[np.argsort(angle), :] # return the coordinates in top-left, top-right, # bottom-right, and bottom-left order return np.array([tl, tr, br, bl], dtype="float32")
参考:
http://www.pyimagesearch.com/2016/03/21/ordering-coordinates-clockwise-with-python-and-opencv/
https://www.coder4.com/archives/3826
阅读全文
0 0
- 四边形坐标顺时针排序
- 利用坐标筛选四边形(凸四边形,边不交叉)
- 已知四边形四个顶点坐标,求面积
- Directx 绘制一四边形 并且绕Y轴顺时针匀速转动
- 二值化轮廓图像轮廓点坐标顺时针获取matlab程序
- 坐标点排序问题
- 多点坐标排序算法
- sort 对坐标 排序
- 坐标点排序
- 二维坐标排序
- Lan Xiang's Square nyoj 1099(已经四边形四点坐标 如何判断正方形)
- 怎样对平面中的点进行顺时针或者逆时针排序
- POJ-1696 极坐标排序
- 刷清橙OJ--A1077.坐标排序
- Mysql根据坐标字符串排序
- python 脚本 ---坐标排序连线
- AutoCAD点排序(x坐标从小到大,y坐标从小到大)
- 四边形不等式
- 做一个接口,以xml报文的方式传递数据
- eclipse 提示错误The method of type must override a superclass method 的解决办法
- 线程互斥锁、信号量sem_t、bufsem.c
- 【自用】javanote170721(小结)
- 利用JMX统计远程JAVA进程的CPU和Memory
- 四边形坐标顺时针排序
- windows通过命令行打开任意程序
- c++11学习
- websphere服务一:访问端口修改
- Handler、Looper、Message、MessageQueue基础流程分析
- if
- c语言那些细节之a+1和&a+1的区别
- 接口安全处理
- Mac下面搭建PHP+Android+iOS开发环境