当前位置: 首页 > news >正文

opencv圖片標注

功能

        使用python + opencv, 將文字信息標注在圖片中

        同一張圖片中涉及多次標注

        文字大小為標注框的0.3倍

import cv2
import os
import numpy as npdef draw_annotations(image, annotations):"""在图片上绘制标注框和文本annotations: 标注列表,格式为[[x1,x2,y1,y2], text, color]"""# 创建图片副本,避免修改原始图片img_copy = image.copy()for annotation in annotations:coords, text, color = annotationx1, x2, y1, y2 = coordsbox_height = y2 - y1# 设置字体大小为框高度的0.3倍 # 确保字体大小至少为0.1,避免太小无法显示font_scale = max(0.3 * box_height / 40, 0.2)# 设置字体类型font = cv2.FONT_HERSHEY_SIMPLEXthickness = max(int(font_scale * 1.5), 4)  # 根据字体大小调整线宽# 计算文本尺寸(text_width, text_height), baseline = cv2.getTextSize(text, font, font_scale, thickness)# 计算框内右上角位置(带5像素内边距)text_x = x2 - text_width - 5text_y = y1 + text_height + 5# 确保文本位置在框内if text_x < x1:text_x = x1 + 5if text_y > y2:text_y = y2 - 5# 绘制边界框cv2.rectangle(img_copy, (x1, y1), (x2, y2), color, thickness)# 绘制文本背景(提高可读性)cv2.rectangle(img_copy,(text_x - 2, text_y - text_height - 2),(text_x + text_width + 2, text_y + 2),color, -1)  # -1表示填充矩形# 绘制文本(白色)cv2.putText(img_copy, text, (text_x, text_y),font, font_scale, (0,0,0), thickness)return img_copy# 主函数
def main():# 示例数据tt = [{'filename': 'D:\\LABELAOI\\Test\\S_right_CAM2.jpg','info': [[(271, 393, 264, 518), 'CELOGO  0.95', (0, 255, 0)]]},{'filename': 'D:\\LABELAOI\\Test\\25732137Q0003-T3N0CV10J812126_20250320192533_L_main_CAM_basler.jpg','info': [[(3778, 4327, 212, 700), '1TUF    0.96', (0, 255, 0)],[(2176, 2501, 2431, 2568), 'ZCELOGO 0.94', (0, 255, 0)],[(4135, 5341, 2535, 3054), '312X 0.97', (0, 255, 0)],[(861, 1369, 315, 528), '15100-30874000 1.00', (0, 255, 0)]]}]for item in tt:# 读取图片image = cv2.imread(item['filename'])# 绘制标注annotated_image = draw_annotations(image, item['info'])# 创建输出文件名output_filename = item['filename'].replace('.jpg', '_annotated.jpg')# 保存结果cv2.imwrite(output_filename, annotated_image)print(f"标注图片已保存为: {output_filename}")if __name__ == "__main__":import datetimestarttime = datetime.datetime.now()main()endtime = datetime.datetime.now()print(f"the process run time:{endtime - starttime}")

        效果如下圖:

                标注图片已保存为: D:\LABELAOI\Test\S_right_CAM2_annotated.jpg
标注图片已保存为: D:\LABELAOI\Test\25732137Q0003-                        T3N0CV10J812126_20250320192533_L_main_CAM_basler_annotated.jpg
the process run time:0:00:00.792136

http://www.dtcms.com/a/287274.html

相关文章:

  • 【办公类-107-02】20250719视频MP4转gif(削减MB)
  • 【Project】kafka+flume+davinci广告点击实时分析系统
  • LangGraph教程6:LangGraph工作流人机交互
  • 黄山派lvgl8学习笔记(1)画一个一个全白的背景
  • 【C++】入门阶段
  • Spring Cloud Gateway与Envoy Sidecar在微服务请求路由中的架构设计分享
  • 最新版vscode 连接ubuntu 18.04 保姆级教程
  • 前端面试专栏-工程化:27.工程化实践(CI/CD、代码规范)
  • Jenkins 实现项目的构建和发布
  • Rust Web 全栈开发(十):编写服务器端 Web 应用
  • 触发器的创建
  • Django母婴商城项目实践(七)- 首页数据业务视图
  • QT窗口(8)-QFileDiag
  • 大模型 Function Call 的实现步骤及示例详解
  • MySQL EXPLAIN深度解析:优化SQL性能的核心利器
  • iOS 通知机制及底层原理
  • Java 大视界 -- Java 大数据机器学习模型在自然语言处理中的对话系统多轮交互优化与用户体验提升(351)
  • 【设计模式C#】状态模式(用于解决解耦多种状态之间的交互)
  • 微服务学习(六)之分布式事务
  • 微服务的编程测评系统-网关-身份认证-redis-jwt
  • Spring Boot 与微服务详细总结
  • Error:HTTP Status 405 - HTTP method POST is not supported by this URL
  • Javascript进程和线程通信
  • Uniapp之键盘弹窗
  • day43 CNN及Grad-CAM实战——以Intel Image Classification为例
  • JAVA中的Collections 类
  • [论文阅读] 人工智能 + 软件工程 | 强化学习在软件工程中的全景扫描:从应用到未来
  • ABP VNext + Temporal:分布式工作流与 Saga
  • 当OT遇见IT:Apache IoTDB如何用“时序空间一体化“破解工业物联网数据孤岛困局
  • 时序数据库选型实战:Apache IoTDB技术深度解析