Python之根据四个坐标确定其位于左上下右上下
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之根据四个坐标确定其位于左上下右上下的更多相关文章
- Python 基础语法(四)
Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...
- 初学 Python(十四)——生成器
初学 Python(十四)--生成器 初学 Python,主要整理一些学习到的知识点,这次是生成器. # -*- coding:utf-8 -*- ''''' 生成式的作用: 减少内存占有,不用一次性 ...
- Python第二十四天 binascii模块
Python第二十四天 binascii模块 binascii用来进行进制和字符串之间的转换 import binascii s = 'abcde' h = binascii.b2a_hex(s) # ...
- Python/MySQL(四、MySQL数据库操作)
Python/MySQL(四.MySQL数据库操作) 一.数据库条件语句: case when id>9 then ture else false 二.三元运算: if(isnull(xx)0, ...
- python学习第四讲,python基础语法之判断语句,循环语句
目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...
- Python第十四天 序列化 pickle模块 cPickle模块 JSON模块 API的两种格式
Python第十四天 序列化 pickle模块 cPickle模块 JSON模块 API的两种格式 目录 Pycharm使用技巧(转载) Python第一天 安装 shell 文件 Py ...
- python学习第四次笔记
python学习第四次记录 列表list 列表可以存储不同数据类型,而且可以存储大量数据,python的限制是 536870912 个元素,64位python的限制是 1152921504606846 ...
- 【转】python 历险记(四)— python 中常用的 json 操作
[转]python 历险记(四)— python 中常用的 json 操作 目录 引言 基础知识 什么是 JSON? JSON 的语法 JSON 对象有哪些特点? JSON 数组有哪些特点? 什么是编 ...
- 第四百零五节,centos7下搭建sentry错误日志服务器,接收python以及Django错误,
第四百零五节,centos7下搭建sentry错误日志服务器,接收python以及Django错误, 注意:版本,不然会报错 Docker >=1.11Compose >1.6.0 通过d ...
随机推荐
- excel-填充
问题[1]:需要将一列元素的空全部填充为NULL 选定列->F5定位(推荐先定位行总数)->再次F5定位(选空值)->在选定后的一个框内输入NULL->ctrl+enter 完 ...
- 群晖系统如何通过Video Station套件管理NAS中的视频
一.PC端观看视频 1.在NAS套件中心找到Video Station套件,安装套件 2.设置video套件别名,便于后期使用,控制面板----应用程序门户----video Station 3.选中 ...
- 秦九韶算法 & 三分法
前言 今天考试出了一个题 郭郭模拟退火骗了75分 于是再次把咕咕了好久的模退提上日程 如果进展顺利 明后天应该会开爬山算法和模退的博客笔记 今天先把今天考试的正解学习一下--三分法 引入 老规矩上板子 ...
- java web Session会话技术(原理图解+功能+与Cookie的区别+基本使用)
java web Session会话技术(原理图解+功能+与Cookie的区别+基本使用) 这是我关于会话技术的第二篇文章,对 Cookie有不了解的兄弟可以点击下方的Cookie跳转 Cookie链 ...
- http接口封装mqtt协议
前言 .Net Core 3.1 WebApi 列出了mqtt客户端的封装目的是为了了解运作机制 1.封装mqtt客户端 mqtt底层协议基于MQTTnet 版本2.8.5 github地址 实例化[ ...
- Python多进程队列间传递对象
前言 在python 需要在队列中传递对象, 会出现进程不能正常退出的情况. 其原因是因为 在父进程 向子进程传入的Queue对象不对, Queue对象正常是子进程之间的信息传递, 而当我在父进程 创 ...
- matlab使用libsvm入门教程——使用matlab安装配置libsvm以及一个svm分类实例
前言 此教程专注于刚入门的小白, 且博客拥有时效性, 发布于2019年3月份, 可能后面的读者会发现一些问题, 欢迎底下评论出现的问题,我将尽可能更新解决方案. 我开始也在如何安装libsvm上出现了 ...
- 看DLI服务4核心如何提升云服务自动化运维
摘要:今天我们来说说DLI是如何实现监控告警来提升整体运维能力,从而为客户更好的提供Serverless的DLI. DLI是支持多模引擎的Serverless大数据计算服务,免运维也是其作为Serve ...
- 面试题——20+Vue面试题整理
0.那你能讲一讲MVVM吗? MVVM是Model-View-ViewModel缩写,也就是把MVC中的Controller演变成ViewModel. Model层代表数据模型,View代表UI组件, ...
- Elasticsearch第五篇:PlainElastic.Net 操作 Elasticsearch
再次强调,我安装的Elasticsearch 版本是 7.8.0 ,C# 操作 Elasticsearch 的驱动有 NEST.Elasticsearch.net .PlainElastic.Net ...