python opencv根据坐标信息绘制矩形框
python opencv根据坐标信息绘制矩形框
1.函数封装
'''
函数描述:对给定的图片和坐标信息在图片上标框,并在框的上方标注出该框的名称
函数参数:img_file_path=图片的绝对路径,new_img_file_path=保存后的绝对路径,points=[(str,[b0,b1,b2,b3])]
返回值:无返回值
注意事项:坐标[b0,b1,b2,b3]依次为左上角和右下角的坐标
'''
def draw_rectangle_by_point(img_file_path,new_img_file_path,points):
image = cv2.imread(img_file_path)
for item in points:
print("当前字符:",item)
point=item[1]
first_point=(int(point[0])*2,int(point[1])*2)
last_point=(int(point[2])*2,int(point[3])*2)
# first_point = (point[0] * 2, point[1] * 2)
# last_point = (point[2]* 2, point[3] * 2)
print("左上角:",first_point)
print("右下角:",last_point)
cv2.rectangle(image, first_point, last_point, (0, 255, 0), 1)#在图片上进行绘制框
cv2.putText(image, item[0], first_point, cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5, color=(255,0,0), thickness=1)#在矩形框上方绘制该框的名称
cv2.imwrite(new_img_file_path, image)
2.测试样例
if __name__ == '__main__':
#points=[('8F.', [66.72106971740723, 193.4539794921875, 80.52160511016845, 207.30389404296875]), ('Zhongshan', [241.5278513590495, 193.4539794921875, 282.9294575373332, 207.30389404296875])]
points=[('No. 18', [89.72196203867594, 193.4539794921875, 117.3230328241984, 207.30389404296875]),
('Zhongshan Dist.', [241.5278513590495, 193.4539794921875, 310.5305283228556, 207.30389404296875])]
draw_rectangle_by_point(r'E:\wm_workspace\day2_2_24\images\16.pdf.png',"new.jpg",points)
3.结果展示
最新文章
- Mybatis注解
- 7.动态Sql语句
- KMP算法图文详解(为什么是next[0]=
- jkd8新特性 StreamAPi流
- Linux下查看内存泄露的命令
- SQL Compare数据库比较工具 完全破解+使用教程
- 拉格朗日方法求最优解
- Caused by: com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.channel.AbstractChannel$AnnotatedC
- nacos2.X集群错误:com.alibaba.nacos.shaded.io.grpc.StatusRuntimeException: UNAVAILABLE: io exception
- Python分布式爬虫原理
- 目标检测详解
- 简单计算器(PTA)
- 音乐推荐系统系列
- Java中带返回值的线程池Future
- 将列表(含字典)数据写入Excel
- C++结构体(结构体创建,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数,const变量使用)
- ROS2 基础概念 参数