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

ubuntu22.04@laptop OpenCV Get Started: 006_annotating_images

ubuntu22.04@laptop OpenCV Get Started: 006_annotating_images

  • 1. 源由
  • 2. line/circle/rectangle/ellipse/text 应用Demo
  • 3 image_annotation
    • 3.1 C++应用Demo
    • 3.2 Python应用Demo
    • 3.3 重点过程分析
      • 3.3.1 划线
      • 3.3.2 画圆
      • 3.3.3 矩形
      • 3.3.4 椭圆
      • 3.3.5 文字
  • 4. 总结
  • 5. 参考资料

1. 源由

为图像和视频添加注释的目的不止一个,OpenCV使这个过程简单明了。

下来,一起看一如何使用它:

  1. 将信息添加到图像上
  2. 在对象检测的情况下,围绕对象绘制边界框
  3. 突出显示具有不同颜色的像素以进行图像分割

一旦学会了对图像进行注释,对视频帧的注释也是同样的道理。

2. line/circle/rectangle/ellipse/text 应用Demo

006_annotating_images是OpenCV进行注释的示例程序。

  • 划线
  • 画圆
  • 矩形
  • 椭圆
  • 文字

确认OpenCV安装路径:

$ find /home/daniel/ -name "OpenCVConfig.cmake"
/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
/home/daniel/OpenCV/opencv/build/OpenCVConfig.cmake
/home/daniel/OpenCV/opencv/build/unix-install/OpenCVConfig.cmake


$ export OpenCV_DIR=/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/

3 image_annotation

3.1 C++应用Demo

C++应用Demo工程结构:

006_annotating_images/CPP$ tree .
.
├── CMakeLists.txt
├── image_annotation.cpp
└── sample.jpg

0 directories, 3 files

C++应用Demo工程编译执行:

$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/image_annotation

3.2 Python应用Demo

Python应用Demo工程结构:

006_annotating_images/Python$ tree .
.
├── image_annotation.py
├── requirements.txt
└── sample.jpg

0 directories, 3 files

Python应用Demo工程执行:

$ workoncv-4.9.0
$ python image_annotation.py

3.3 重点过程分析

3.3.1 划线

  • line(image, start_point, end_point, color, thickness)

第一个参数:行 (高度, 自上而下递增)
第二个参数:列 (宽度, 自左往右递增)

C++:

// Draw line on image
Mat imageLine = img.clone();
// Draw the image from point  A to B
Point pointA(200,80);
Point pointB(450,80);
line(imageLine, pointA,  pointB, Scalar(255, 255, 0), 3, 8, 0);
imshow("Lined Image", imageLine);
waitKey();

Python:

# Draw line on image
imageLine = img.copy()
# Draw the image from point  A to B
pointA = (200,80)
pointB = (450,80)
cv2.line(imageLine, pointA, pointB, (255, 255, 0), thickness=3, lineType=cv2.LINE_AA)
cv2.imshow('Image Line', imageLine)
cv2.waitKey(0)

3.3.2 画圆

  • circle(image, center_coordinates, radius, color, thickness)

C++:

// Draw Circle on image
Mat imageCircle = img.clone();
int radius = 100;
Point circle_center(415,190);
circle(imageCircle, circle_center, radius, Scalar(0, 0, 255), 3, 8, 0);
imshow("Circle on Image", imageCircle);
waitKey();

// Draw Filled Circle
Mat Filled_circle_image = img.clone();
circle(Filled_circle_image, circle_center, radius, Scalar(255, 0, 0), -1, 8, 0);
imshow("Circle on Image", Filled_circle_image);
waitKey();

Python:

# Draw Circle on image
imageCircle = img.copy()
circle_center = (415,190)
radius =100
cv2.circle(imageCircle, circle_center, radius, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.imshow("Image Circle",imageCircle)
cv2.waitKey(0)

# Draw Filled Circle
Filled_circle_image = img.copy()
cv2.circle(Filled_circle_image, circle_center, radius, (255, 0, 0), thickness=-1, lineType=cv2.LINE_AA)
cv2.imshow('Image with Filled Circle',Filled_circle_image)
cv2.waitKey(0)

3.3.3 矩形

  • rectangle(image, start_point, end_point, color, thickness)

C++:

// Draw Rectangle
Mat imageRectangle = img.clone();
Point start_point(300,115);
Point end_point(475,225);
rectangle(imageRectangle, start_point, end_point, Scalar(0,0,255), 3, 8, 0);
imshow("Rectangle on Image", imageRectangle);
waitKey();

Python:

# Draw Rectangle
imageRectangle = img.copy()
start_point = (300,115)
end_point = (475,225)
cv2.rectangle(imageRectangle, start_point, end_point, (0, 0, 255), thickness= 3, lineType=cv2.LINE_8)   
cv2.imshow('imageRectangle', imageRectangle)
cv2.waitKey(0)

3.3.4 椭圆

  • ellipse(image, centerCoordinates, axesLength, angle, startAngle, endAngle, color, thickness)

C++:

// Draw Ellipse
Mat imageEllipse = img.clone();
// Horizontal
Point ellipse_center(415,190);
Point axis1(100, 50);
Point axis2(125, 50);
ellipse(imageEllipse, ellipse_center, axis1, 0, 0, 360, Scalar(255, 0, 0), 3, 8, 0);
// Vertical
ellipse(imageEllipse, ellipse_center, axis2, 90, 0, 360, Scalar(0, 0, 255), 3, 8, 0);
imshow("Ellipses on Image", imageEllipse);
waitKey();


Mat halfEllipse = img.clone();
// Half Ellipse
ellipse(halfEllipse, ellipse_center, axis1, 0, 180, 360, Scalar(255, 0, 0), 3, 8, 0);
// Filled ellipse
ellipse(halfEllipse, ellipse_center, axis1, 0, 0, 180, Scalar(0, 0, 255), -2, 8, 0);
imshow("halfEllipse", halfEllipse);
waitKey();

Python:

# Draw Ellipse
imageEllipse = img.copy()
ellipse_center = (415,190)
axis1 = (100,50)
axis2 = (125,50)
cv2.ellipse(imageEllipse, ellipse_center, axis1, 0, 0, 360, (255, 0, 0), thickness=3, lineType=cv2.LINE_AA)
cv2.ellipse(imageEllipse, ellipse_center, axis2, 90, 0, 360, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA)
cv2.imshow('ellipse Image',imageEllipse)
cv2.waitKey(0)


halfEllipse = img.copy()
# Half Ellipse
cv2.ellipse(halfEllipse, ellipse_center, axis1, 0, 180, 360, (255, 0, 0), thickness=3, lineType=cv2.LINE_AA)
# Filled ellipse
cv2.ellipse(halfEllipse, ellipse_center, axis1, 0, 0, 180, (0, 0, 255), thickness=-2, lineType=cv2.LINE_AA)
cv2.imshow('HalfEllipse',halfEllipse)
cv2.waitKey(0)

3.3.5 文字

  • putText(image, text, org, font, fontScale, color)

C++:

// Put Text on Image
Mat imageText = img.clone();
// org: Where you want to put the text
Point org(50,350);
putText(imageText, "I am a Happy dog!", org, FONT_HERSHEY_COMPLEX, 1.5, Scalar(0,255,0), 3, 8, false);
imshow("Text on Image", imageText);
waitKey(0);

Python:

# Put Text on Image
imageText = img.copy()
text = 'I am a Happy dog!'
# org: Where you want to put the text
org = (50,350)
cv2.putText(imageText, text, org, fontFace = cv2.FONT_HERSHEY_COMPLEX, fontScale = 1.5,
            color = (250,225,100),thickness =  3, lineType=cv2.LINE_AA)
cv2.imshow("Image Text",imageText)
cv2.waitKey(0)

4. 总结

通过以下5个API对图像进行操作,分别在图像上划线、画圆、矩形、椭圆,以及标注文字的方式进行注释。

  1. line(image, start_point, end_point, color, thickness)
  2. circle(image, center_coordinates, radius, color, thickness)
  3. rectangle(image, start_point, end_point, color, thickness)
  4. ellipse(image, centerCoordinates, axesLength, angle, startAngle, endAngle, color, thickness)
  5. putText(image, text, org, font, fontScale, color)

5. 参考资料

【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装


http://www.kler.cn/a/233822.html

相关文章:

  • 解决表格出现滚动条样式错乱问题
  • 前端知识点---Javascript的对象(Javascript)
  • 从 MySQL 5.7 到 8.0:理解 GROUP BY 的新规则与实战优化20241112
  • C# 集合与泛型
  • 自动驾驶3D目标检测综述(一)
  • 稀疏视角CBCT重建的几何感知衰减学习|文献速递-基于深度学习的病灶分割与数据超分辨率
  • Nginx管理配置中多种变量学习
  • 梯度提升树系列7——深入理解GBDT的参数调优
  • 【漏洞复现】狮子鱼CMS某SQL注入漏洞01
  • redis双写一致
  • Deepin基本环境查看(八)【系统安全:房、车、查房、查车】
  • 2.9日学习打卡----初学RabbitMQ(四)
  • Unity报错Currently selected scripting backend (IL2CPP) is not installed
  • 【数据存储+多任务爬虫】
  • Jupyter的全面探索:从入门到高级应用
  • 数据结构——5.4 树、森林
  • 模运算的变换公式
  • QListWidget组件功能
  • 被设计的面试题与设计性的回答
  • 配置VMware实现从服务器到虚拟机的一键启动脚本
  • 数据结构——5.3 二叉树的遍历和线索二叉树
  • 游戏竞赛中的时间压力与情绪管理:一场关于挑战、紧迫感与心态的深度探讨
  • 255.【华为OD机试真题】最小矩阵宽度(滑动窗口算法-JavaPythonC++JS实现)
  • 【微机原理与单片机接口技术】MCS-51单片机的引脚功能介绍
  • LabVIEW工业监控系统
  • 【Linux】构建模块