ubuntu22.04@laptop OpenCV Get Started: 006_annotating_images

本文详细介绍了在Ubuntu22.04环境下使用OpenCV4.9.0进行图像注解的方法,包括线、圆、矩形、椭圆和文字的绘制,以及C++和Python示例代码。重点讲解了如何在C++和Python中实现这些功能,适合初学者逐步学习和实践。
摘要由CSDN通过智能技术生成

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 划线

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

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 画圆

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 矩形

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 椭圆

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 文字

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定制化安装

6. 补充

学习是一种过程,对于前面章节学习讨论过的,就不在文中重复了。

有兴趣了解更多的朋友,请从《ubuntu22.04@laptop OpenCV Get Started》开始,一个章节一个章节的了解,循序渐进。

  • 25
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值