ubuntu22.04@laptop OpenCV Get Started: 006_annotating_images

2024-02-11 12:04

本文主要是介绍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.jpg0 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.jpg0 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定制化安装

这篇关于ubuntu22.04@laptop OpenCV Get Started: 006_annotating_images的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/699727

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

C/C++中OpenCV 矩阵运算的实现

《C/C++中OpenCV矩阵运算的实现》本文主要介绍了C/C++中OpenCV矩阵运算的实现,包括基本算术运算(标量与矩阵)、矩阵乘法、转置、逆矩阵、行列式、迹、范数等操作,感兴趣的可以了解一下... 目录矩阵的创建与初始化创建矩阵访问矩阵元素基本的算术运算 ➕➖✖️➗矩阵与标量运算矩阵与矩阵运算 (逐元

C/C++的OpenCV 进行图像梯度提取的几种实现

《C/C++的OpenCV进行图像梯度提取的几种实现》本文主要介绍了C/C++的OpenCV进行图像梯度提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录预www.chinasem.cn备知识1. 图像加载与预处理2. Sobel 算子计算 X 和 Y

C/C++和OpenCV实现调用摄像头

《C/C++和OpenCV实现调用摄像头》本文主要介绍了C/C++和OpenCV实现调用摄像头,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录准备工作1. 打开摄像头2. 读取视频帧3. 显示视频帧4. 释放资源5. 获取和设置摄像头属性

c/c++的opencv图像金字塔缩放实现

《c/c++的opencv图像金字塔缩放实现》本文主要介绍了c/c++的opencv图像金字塔缩放实现,通过对原始图像进行连续的下采样或上采样操作,生成一系列不同分辨率的图像,具有一定的参考价值,感兴... 目录图像金字塔简介图像下采样 (cv::pyrDown)图像上采样 (cv::pyrUp)C++ O

c/c++的opencv实现图片膨胀

《c/c++的opencv实现图片膨胀》图像膨胀是形态学操作,通过结构元素扩张亮区填充孔洞、连接断开部分、加粗物体,OpenCV的cv::dilate函数实现该操作,本文就来介绍一下opencv图片... 目录什么是图像膨胀?结构元素 (KerChina编程nel)OpenCV 中的 cv::dilate() 函

qtcreater配置opencv遇到的坑及实践记录

《qtcreater配置opencv遇到的坑及实践记录》我配置opencv不管是按照网上的教程还是deepseek发现都有些问题,下面是我的配置方法以及实践成功的心得,感兴趣的朋友跟随小编一起看看吧... 目录电脑环境下载环境变量配置qmake加入外部库测试配置我配置opencv不管是按照网上的教程还是de

python+OpenCV反投影图像的实现示例详解

《python+OpenCV反投影图像的实现示例详解》:本文主要介绍python+OpenCV反投影图像的实现示例详解,本文通过实例代码图文并茂的形式给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前言二、什么是反投影图像三、反投影图像的概念四、反向投影的工作原理一、利用反向投影backproj