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. 嵌入式linux简介

    嵌入式linux系统应用非常广泛,涵盖各行各业,基于ARM.mips等微处理器架构的硬件平台.基于嵌入式linux系统的设备已经深入生活中各个角落,随处可见.   我们常说的嵌入式linux系统,其实 ...

  2. Java泛型是什么?实战demo

    1. 概述 泛型在java中有很重要的地位,在面向对象编程及各种设计模式中有非常广泛的应用. 什么是泛型?为什么要使用泛型? 泛型,即“参数化类型”.一提到参数,最熟悉的就是定义方法时有形参,然后调用 ...

  3. Web 开发必须掌握的三个技术:Token、Cookie、Session

    在Web应用中,HTTP请求是无状态的.即:用户第一次发起请求,与服务器建立连接并登录成功后,为了避免每次打开一个页面都需要登录一下,就出现了cookie,Session. Cookie Cookie ...

  4. Java 多态 接口继承等学习笔记

    Super关键字 1.子类可以调用父类声明的构造方法 : 语法:在子类的构造方法中使用super关键字  super(参数列表) 2.操作被隐藏的成员变量(子类的成员变量和父类的成员变量重名的说法)和 ...

  5. IndexFlatL2、IndexIVFFlat、IndexIVFPQ三种索引方式示例

    上文针对Faiss安装和一些原理做了简单说明,本文针对标题所列三种索引方式进行编码验证. 首先生成数据集,这里采用100万条数据,每条50维,生成数据做本地化保存,代码如下: import numpy ...

  6. label smoothing

  7. Java—io流之打印流、 commons-IO

    打印流 打印流根据流的分类: 字节打印流  PrintStream 字符打印流  PrintWriter /* * 需求:把指定的数据,写入到printFile.txt文件中 * * 分析: * 1, ...

  8. Flutter 容器 (1) - Center

    Center容器用来居中widget import 'package:flutter/material.dart'; class AuthList extends StatelessWidget { ...

  9. 怎么写简历,简历才不会被丢到非洲&#127757;

    前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 最近的三歪朋友圈可以看到很多的字节.腾讯的同学都 ...

  10. CSS动画实例:升空的气球

    1.制作一个气球 设页面中有<div class="balloon"></div>,为. balloon设置样式规则如下: .balloon { heigh ...