opencv-python绘制多边形框

import numpy as np
import cv2
import matplotlib.pyplot as plt
import jsonlines
lines = []
# res_0.txt内容如下:
"""
{"type": "text", "bbox": [11, 729, 407, 847], "res": "", "img_idx": 0}
{"type": "text", "bbox": [442, 754, 837, 847], "res": "", "img_idx": 0}
{"type": "title", "bbox": [443, 705, 559, 719], "res": "", "img_idx": 0}
{"type": "figure", "bbox": [10, 1, 841, 294], "res": "", "img_idx": 0}
{"type": "figure_caption", "bbox": [70, 317, 707, 357], "res": "", "img_idx": 0}
{"type": "figure_caption", "bbox": [160, 317, 797, 335], "res": "", "img_idx": 0}
{"type": "table", "bbox": [453, 359, 822, 664], "res": "", "img_idx": 0}
{"type": "table", "bbox": [12, 360, 410, 716], "res": "", "img_idx": 0}
{"type": "table_caption", "bbox": [494, 343, 785, 356], "res": "", "img_idx": 0}
{"type": "table_caption", "bbox": [69, 318, 706, 357], "res": "", "img_idx": 0}
"""
with jsonlines.open("output/1/res_0.txt") as f:
    for line in f:
        h0,w0, h1, w1 = line['bbox']
        lines.append(np.array([[h0,w0],[h0,w1],[h1, w1],[h1,w0]]))
img = cv2.imread('ppstructure/docs/table/1.png')

for line in lines:
    cv2.polylines(img,
                  [line],
                  isClosed=True,
                  color=(255, 255, 0),
                  thickness=3)
cv2.imwrite('output/1/res_2.png', img)  # 保存图像文件

参考资料:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,绘制多边形和复合多边形有一些区别。 1. 多边形多边形是一个简单的封闭图形,由一系列的连续直线段组成,首尾相接。可以是三角形、四边形、五边形等等。绘制多边形时,只需要提供多边形的顶点坐标即可。 2. 复合多边形:复合多边形是由多个简单多边形组合而成的更复杂的图形。它可以由相邻的多个简单多边形共享一部分边界而形成。绘制复合多边形时,需要提供每个简单多边形的顶点坐标,并按照一定的顺序连接它们来构建复合多边形绘制多边形和复合多边形的方法在Python中略有不同。对于多边形,你可以使用OpenCV库中的`cv2.polylines()`函数来绘制。该函数接受一个包含顶点坐标的数组,并在图像上绘制多边形。 对于复合多边形,你需要将每个简单多边形的顶点坐标分别存储在一个数组中,并使用`cv2.polylines()`函数分别绘制它们。然后,你可以使用`cv2.fillPoly()`函数填充整个复合多边形的区域。 这是一个简单的示例代码,演示如何使用OpenCV绘制多边形和复合多边形: ```python import numpy as np import cv2 # 创建一个空白图像 image = np.zeros((400, 400, 3), dtype=np.uint8) # 绘制一个三角形 triangle = np.array([[100, 300], [200, 100], [300, 300]], np.int32) cv2.polylines(image, [triangle], True, (0, 255, 0), thickness=2) # 绘制一个矩形 rectangle = np.array([[50, 50], [50, 200], [200, 200], [200, 50]], np.int32) cv2.polylines(image, [rectangle], True, (0, 0, 255), thickness=2) # 绘制一个复合多边形 compound_polygon = [np.array([[250, 50], [350, 50], [350, 150], [250, 150]], np.int32), np.array([[270, 70], [330, 70], [330, 130], [270, 130]], np.int32)] cv2.polylines(image, compound_polygon, True, (255, 0, 0), thickness=2) cv2.fillPoly(image, compound_polygon, (255, 0, 0)) # 显示图像 cv2.imshow('Polygon and Compound Polygon', image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这段代码将创建一个400x400大小的空白图像,并使用`cv2.polylines()`函数绘制了一个三角形、一个矩形和一个复合多边形。你可以根据需要修改顶点坐标和颜色来绘制不同的多边形。最后,使用`cv2.imshow()`函数显示图像。 希望这能帮助到你!如有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值