shapely包的使用

shapely包的使用1 Point LineStrings 共有的变量和方法 PointLineStr 引入包 fromshapely geometryimpo geometryimpo 共有的变量和方法 object area Returnsthear float oftheobject object

1. Point,LineStrings

共有的变量和方法

  • object.area
      Returns the area (float) of the object.

  • object.bounds
      返回对象的(minx,miny,maxx,maxy)元组(float类型)

  • object.length
      返回对象的长度

  • object.geom_type
      返回对象类型

  • object.distance(other)
      返回本对象和另一个对象的距离

  • object.representative_point()
      Returns a cheaply computed point that is guaranteed to be within the geometric object.

 import math from shapely.geometry import Point from shapely.geometry import LineString point = Point(0,0) point_2 = Point((0,0)) point_3 = Point(point) print(point.area) # 0 print(point.length) # 0 print(point.coords) print(list(point.coords)) # 
    # [(0.0, 0.0)] print(Point(0,0).distance(Point(0,1))) # 1.0 # h*w-h2*w2/ line = LineString([(0,0),(1,1),(1,2)]) print(line.area) # 0.0 print(line.coords) print(list(line.coords)) # 
    # [(0.0, 0.0), (1.0, 1.0), (1.0, 2.0)] print(line.bounds) # (0.0, 0.0, 1.0, 2.0) print(line.length) # 2.3095=sqrt(2)+1 print(math.sqrt(2)) # 1.30951 print(line.geom_type) # LineString 

2. 多边形

在这里插入图片描述

import numpy as np import shapely from shapely.geometry import Polygon, MultiPoint # 多边形 line1 = [2, 0, 2, 2, 0, 0, 0, 2] # 四边形四个点坐标的一维数组表示,[x,y,x,y....] a = np.array(line1).reshape(4, 2) # 四边形二维坐标表示 poly1 = Polygon(a).convex_hull # python四边形对象,会自动计算四个点,最后四个点顺序为: 左下 左上 右上 右下 左下 print(Polygon(a).convex_hull) # 可以打印看看是不是这样子 line2 = [1, 1, 4, 1, 4, 4, 1, 4] b = np.array(line2).reshape(4, 2) poly2 = Polygon(b).convex_hull print(poly2.convex_hull) union_poly = np.concatenate((a, b)) # 合并两个box坐标,变为8*2 print(union_poly) print(MultiPoint(union_poly).convex_hull) # 包含两四边形最小的多边形点 if not poly1.intersects(poly2): # 如果两四边形不相交 iou = 0 else: try: inter_area = poly1.intersection(poly2).area # 相交面积 print(inter_area) union_area_2 = poly1.area + poly2.area - inter_area print('union_area_2', union_area_2) # 12.0 union_area = MultiPoint(union_poly).convex_hull.area print('union_area', union_area) # 14.0 if union_area == 0: iou = 0 iou_1 = float(inter_area) / (union_area-inter_area) #错了 iou_2 = float(inter_area) / union_area iou_3=float(inter_area) /(poly1.area+poly2.area-inter_area) print(iou_1, iou_2, iou_3) # 0.007693 0.057142 0.033333 # 源码中给出了两种IOU计算方式,第一种计算的是: 交集部分/包含两个四边形最小多边形的面积 # 第二种: 交集 / 并集(常见矩形框IOU计算方式) iou = iou_3 except shapely.geos.TopologicalError: print('shapely.geos.TopologicalError occured, iou set to 0') iou = 0 print(a) print(iou) 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/203192.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午10:22
下一篇 2026年3月19日 下午10:22


相关推荐

  • Photoshop CC2019 快捷键壁纸 快捷键大全

    Photoshop CC2019 快捷键壁纸 快捷键大全篇幅较长,建议先收藏,以后有需要了随时可以查看一、工具箱(多种工具共用一个快捷键的可同时按【Shift】加此快捷键选取)矩形、椭圆选框工具【M】 移动工具【V】 套索、多边形套索、磁性套索【L】 魔棒工具【W】 裁剪工具【C】 切片工具、切片选择工具【K】 喷枪工具【J】 画笔工具、铅笔工具【B】 像皮图章、图案图章【S】 历史画笔工具、艺…

    2026年4月18日
    3
  • 如何使用keil 5 编写 51单片机 工程

    如何使用keil 5 编写 51单片机 工程目前我们通常编写51程序使用的是keil4,而好多编写STM32等单片机程序的使用keil5。那么如何在keil5中兼容51和STM32程序编写,省去切换版本的繁琐呢?很简单只需两步就可以完成。下面这个方法针对已破解keil5的stm32等一系列。这个肯定是最常见的,因为破解keil5然后编写32工程的教程一大把。1、首先下载编写51的相关东西。可以在官网上下载,例如百度keil官

    2022年5月24日
    53
  • mac navicat premium 激活码[最新免费获取]

    (mac navicat premium 激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月29日
    276
  • kindeditor编辑器问题

    kindeditor编辑器问题1 kindeditorht 代码过滤不能保存

    2026年3月18日
    2
  • 关于JavaScript 通讯加密的浅析(CryptoJs各种加密算法)

    关于JavaScript 通讯加密的浅析(CryptoJs各种加密算法)暂时支持的算法:MD5SHA-1SHA-256AESRabbitMARC4HMACHMAC-MD5HMAC-SHA1HMAC-SHA256PBKDF2相关的链接网址如下:http://blog.csdn.net/wangcunhuazi/article/details/41491995

    2022年5月2日
    55

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号