当前位置: 首页 > 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.lryc.cn/news/593750.html

相关文章:

  • Redisson RLocalCachedMap 核心参数详解
  • 基于 OpenCV 的 Haar 级联人脸检测模型对比研究 —— 以典型应用场景验证为例 毕业论文——仙盟创梦IDE
  • 智能体上下文选择-记忆工具知识
  • 工程图矢量化 笔记 | potrace ezdxf svgpathtools | png转svg保存dxf用matplotlib画出来
  • Qt笔记整理(1)
  • 高速SAR架构ADC选型设计
  • LVS——nat模式
  • 分立元件线性稳压器12V转5VMultisim仿真
  • 最终分配算法【论文材料】
  • 力扣24:两两交换链表中的节点
  • [FFmpeg] 输入输出访问 | 管道系统 | AVIOContext 与 URLProtocol | 门面模式
  • 外观设计模式
  • 零基础学习性能测试第二章-linux服务器监控:CPU监控
  • Redis字符串操作指南:从入门到实战应用
  • SQLShift:一款异构数据库存储过程迁移工具
  • c++ 基本语法易错与技巧总结
  • 模型的评估与选择
  • 【52】MFC入门到精通——(CComboBox)下拉框选项顺序与初始化不一致,默认显示项也不一致
  • yolov8-pos/yolov11-pos openvino C++部署
  • bash方式启动模型训练
  • OpenCV特征点提取算法orb、surf、sift对比
  • 相机参数的格式与作用
  • 算法基础知识总结
  • MYSQL 第一次作业
  • 量子计算与AI融合的技术突破与实践路径
  • scalelsd 笔记 线段识别 本地部署 模型架构
  • 【面试题】大厂高压面经实录丨第三期
  • SpringBoot服装推荐系统实战
  • 石子问题(区间dp)
  • 泛型机制详解