1、导入模块

import numpy as np

2、存储所需要确定位置的四个坐标点

# 所需要确定位置的四个坐标
coordinate = [[2570, 1948], [2391, 1919], [2411, 1792], [2591, 1821]] # 常规矩形坐标
# coordinate = [[0, 1], [1, 0], [1, 2], [2, 1]] # 非常规(菱形)矩形坐标,用以验证代码
coordinate = np.array(coordinate)

3、求出中心坐标点

center = coordinate[0]
for _ in range(1, 4):
center = center + coordinate[_]
center = center / 4

4、找出x轴小于中心坐标点的点 left_coordinate

coordinate_temp = coordinate.copy() # 复制一份坐标,避免原坐标被破坏
left_coordinate = [] # 存储x轴小于中心坐标点的点
delete_index = [] # 将 x轴小于中心坐标点的点 存储进left_coordinate
for _ in range(4):
if(coordinate[_][0] < center[0]):
left_coordinate.append(coordinate[_])
delete_index.append(_)
# 将存储进 left_coordinate 的元素从coordinate_temp中删除
coordinate_temp = np.delete(coordinate_temp, delete_index, axis=0)

5、确定四个坐标所处位置 'left_top', 'right_top', 'right_bottom', 'left_bottom'

思路:
若 len(left_coordinate) == 2:
比较left_coordinate中的坐标,其y轴小于中心坐标点的,确定为 左下,另一个确定为左上
比较coordinate_temp中的坐标,其y轴小于中心坐标点的,确定为 右下,另一个确定为右上
若 len(left_coordinate) == 1:
确定该点为左下
比较coordinate_temp中的坐标,找出其x轴大于中心坐标点的,确定为右上,并删除该点,
比较剩下两点的y轴,大的确定为左上,小的确定为右下

left_coordinate_temp = left_coordinate.copy() # 避免程序过程因为left_coordinate的变动而导致最初的条件判断错误

if(len(left_coordinate_temp) == 2):
delete_index = [] for _ in range(2):
if(left_coordinate[_][1] < center[1]):
left_bottom = left_coordinate[_]
delete_index.append(_)
break left_coordinate = np.delete(left_coordinate, delete_index, axis=0)
left_top = left_coordinate[0]
for _ in range(2):
if(coordinate_temp[_][1] < center[1]):
right_bottom = coordinate_temp[_]
coordinate_temp = np.delete(coordinate_temp, [_], axis=0)
break
right_top = coordinate_temp[0] elif(len(left_coordinate_temp) == 1):
left_bottom = left_coordinate[0]
delete_index = [] for _ in range(3):
if(coordinate_temp[_][0] == center[0] and coordinate_temp[_][1] > center[1]):
left_top = coordinate_temp[_]
delete_index.append(_)
if(coordinate_temp[_][0] == center[0] and coordinate_temp[_][1] < center[1]):
right_bottom = coordinate_temp[_]
delete_index.append(_) coordinate_temp = np.delete(coordinate_temp, delete_index, axis=0)
right_top = coordinate_temp[0]

6、检验定位效果

print(left_top, right_top)
print(left_bottom, right_bottom)

运行结果:

Python之根据四个坐标确定其位于左上下右上下的更多相关文章

  1. Python 基础语法(四)

    Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...

  2. 初学 Python(十四)——生成器

    初学 Python(十四)--生成器 初学 Python,主要整理一些学习到的知识点,这次是生成器. # -*- coding:utf-8 -*- ''''' 生成式的作用: 减少内存占有,不用一次性 ...

  3. Python第二十四天 binascii模块

    Python第二十四天 binascii模块 binascii用来进行进制和字符串之间的转换 import binascii s = 'abcde' h = binascii.b2a_hex(s) # ...

  4. Python/MySQL(四、MySQL数据库操作)

    Python/MySQL(四.MySQL数据库操作) 一.数据库条件语句: case when id>9 then ture else false 二.三元运算: if(isnull(xx)0, ...

  5. python学习第四讲,python基础语法之判断语句,循环语句

    目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...

  6. Python第十四天 序列化 pickle模块 cPickle模块 JSON模块 API的两种格式

    Python第十四天 序列化  pickle模块  cPickle模块  JSON模块  API的两种格式 目录 Pycharm使用技巧(转载) Python第一天  安装  shell  文件 Py ...

  7. python学习第四次笔记

    python学习第四次记录 列表list 列表可以存储不同数据类型,而且可以存储大量数据,python的限制是 536870912 个元素,64位python的限制是 1152921504606846 ...

  8. 【转】python 历险记(四)— python 中常用的 json 操作

    [转]python 历险记(四)— python 中常用的 json 操作 目录 引言 基础知识 什么是 JSON? JSON 的语法 JSON 对象有哪些特点? JSON 数组有哪些特点? 什么是编 ...

  9. 第四百零五节,centos7下搭建sentry错误日志服务器,接收python以及Django错误,

    第四百零五节,centos7下搭建sentry错误日志服务器,接收python以及Django错误, 注意:版本,不然会报错 Docker >=1.11Compose >1.6.0 通过d ...

随机推荐

  1. time模块 random模块

    time模块 time.sys等模块是C语言实现的,内置到了python解释器的.而不是py文件. 导入模块的时候,优先到python解释器,然后才会找py文件. #时间戳 #计算 # print(t ...

  2. Python Tricks —— 使用 pywinrm 远程控制 Windows 主机

    启用 WinRM 远程服务: winrm quickconfig 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不 ...

  3. LinuX操作系统基础------>初始并安装系统

    b了解LinuX操作系统的来历和发展史 掌握虚拟机(Vbox)的安装方法 掌握CentOS6.5的安装方法 操作系统: 操作系统简称OS ,是管理计算机硬件与软件资源的计算机程序,同时也是计算机系统的 ...

  4. 重写ThreadPoolTaskExecutor

    目录 主类开启异步注解 创建线程池配置类 创建线程池实现类 创建一个测试类Controller 创建异步Service方法 定义异步的实现类 ThreadPoolExecutor:JDK内置线程池实现 ...

  5. 将vscode打造成强大的C/C++ IDE

    一.安装 你可以直接从微软官网下载,如果你想要一个纯净的vscode(微软官方的有一项商标.一个插件库.一个 C# 调试器以及遥测),可以手动编译https://github.com/microsof ...

  6. Vulhub Docker环境部署

    1:安装Linux 2:安装Docker : curl -s https://get.docker.com/ | sh 3:安装Docker-compose curl -s https://boots ...

  7. vue实现自定义表格列

    在我们开发PC端的项目使用表单时,尤其是crm系统,应该经常会遇到这样的需求, 用户需要根据设置来自定义显示列. 查了element的官方文档, 并没有此类组件, 所以手动封装了一个简单的组件, 希望 ...

  8. mapper.xml文件中传入list参数报错 ‘ ’附近有语法错误

    mapper.xml文件中传入list参数,使用foreach循环遍历值,但是在遍历的过程中出错了,具体代码如下所示 mapper.xml <select id="selectByCo ...

  9. 使用Python语言通过PyQt5和socket实现UDP服务器

    前言 最近做了一个小软件,记录一下相关内容. 已有条件 现在已有一个硬件设备作为客户端(暂称其为"电路"). 基于SIM卡,电路可以通过UDP协议传输数据(程序已经内置在电路中), ...

  10. 2020-04-06:为什么HashMap不一直使用红黑树?

    红黑树的阈值是8,当链表大于等于8时链表变成了红黑树结构,大大减少了查找的时间. 当长度低于6时会由红黑树转成链表,TreeNodes占用空间是普通Nodes的两倍,所以只有当bin包含足够多的节点时 ...