OpenCV入门(C++/Python)- 使用OpenCV标注图像(六)
创始人
2024-03-21 16:48:22
0

使用OpenCV标注图像

  • 用颜色线标注图像
  • 绘制圆
  • 绘制实心圆
  • 绘制矩阵
  • 绘制椭圆
  • 绘制带轮廓和填充半椭圆
  • 使用文本注释图像

为图像和视频添加标注的目的不止一个,包含:

  • 向视频中添加信息
  • 在对象检测的情况下,在对象周围绘制边界框,
  • 用不同颜色的像素以进行图像分割

一旦有了标注图像,标注视频帧似乎也同样简单。这是因为视频中的每一帧都被表示为图像。我们将在这里演示如何用几何形状和文本标注图像,示例代码如下:
Python

# Import dependencies
import cv2
# Read Images
img = cv2.imread('sample.jpg')
# Display Image
cv2.imshow('Original Image',img)
cv2.waitKey(0)
# Print error message if image is null
if img is None:print('Could not read image')
# 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)

C++

// Import dependencies
#include 
#include 
// Using namespaces to nullify use of c::function(); syntax and std::function(); syntax
using namespace std;
using namespace cv;
int main()
{// Read ImagesMat img = imread("sample.jpg");// Display Imageimshow("Original Image", img);waitKey();// Print Error message if image is nullif (img.empty()){cout << "Could not read image" << endl;}// Draw line on imageMat imageLine = img.clone();Point pointA(200,80);Point pointB(450,80);line(imageLine, pointA, pointB, Scalar(255, 255, 0), 3, 8, 0);imshow("Lined Image", imageLine);waitKey();
}

用颜色线标注图像

在上面示例中,使用OpenCV中的line()函数,用颜色线标注图像。在调用line()函数之前,使用以下命令创建原始图像的副本:

  • Python中的copy()函数
  • C++中的clone()函数

副本将确保对图像所做的任何更改都不会影响原始图像。在C++中,首先为原始图像的副本创建一个矩阵。

下面是line()函数的语法:

line(image, start_point, end_point, color, thickness)
  • 第一个参数是图像。
  • 接下来的两个参数是直线的起点和终点。

从点A(x1,y1)A(x_1,y_1)A(x1​,y1​)到点B(x2,y2)B(x_2,y_2)B(x2​,y2​)画一条线,其中A和B表示图像中的任意两点。

  • x轴表示图像的水平方向或列。
  • y轴表示图像的垂直方向或行。

如下代码所示:

  • 指定起点和终点,以在图像上水平绘制一条250像素长的线
  • 将其颜色指定为蓝色和绿色的混合,其厚度指定为3。

Python

#Make copy of the 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))
cv2.imshow('Image Line', imageLine)
cv2.waitKey(0)

C++

// Make copy of the imageMat imageLine = img.clone();// Draw the image from point A to BPoint pointA(200,80);Point pointB(450,80);line(imageLine, PointA, PointB, Scalar(255, 255, 0), 3, 8, 0);imshow("Lined Image", line_image);waitKey();

在这里插入图片描述

绘制圆

接下来,使用OpenCV中的circle()函数为图像添加一个圆。看看它的语法:

circle(image, center_coordinates, radius, color, thickness)
  • 与OpenCV中的所有绘图函数一样,第一个参数是图像。
  • 接下来的两个参数定义圆心及其半径的坐标。
  • 最后两个参数指定线条的颜色和粗细。

在本例中,对图像进行标注,并在美女的脸周围添加一个红色圆圈。然后使用imshow()函数显示带标注的图像。

Python

# Make a copy of image
imageCircle = img.copy()
# define the center of circle
circle_center = (415,450)
# define the radius of the circle
radius =100
#  Draw a circle using the circle() Function
cv2.circle(imageCircle, circle_center, radius, (0, 0, 255), thickness=3, lineType=cv2.LINE_AA) 
# Display the result
cv2.imshow("Image Circle",imageCircle)
cv2.waitKey(0)

C++

// Make a copy of imageMat circle_image = img.clone();// define the center of circlePoint circle_center(415,190);// define the radius of circleint radius = 100;// Draw a circle using the circle() Functioncircle(circle_image, circle_center, radius, Scalar(0, 0, 255), 3, 8, 0);// Display the resultimshow("Circle on Image", circle_image);waitKey();

在这里插入图片描述

绘制实心圆

刚刚完成了对带有红色圆圈的图像的标注。如果现在想用纯色填充这个圆圈怎么办?这很简单。只需将thickness参数更改为-1,如下代码所示。
Python

# make a copy of the original image
imageFilledCircle = img.copy()
# define center of the circle 
circle_center = (415,450)
# define the radius of the circle
radius =100
# draw the filled circle on input image
cv2.circle(imageFilledCircle, circle_center, radius, (255, 0, 0), thickness=-1, lineType=cv2.LINE_AA)
# display the output image 
cv2.imshow('Image with Filled Circle',imageFilledCircle)
cv2.waitKey(0)

C++

// make a copy of the original imageMat Filled_circle_image = img.clone();// define the center of circlePoint circle_center(415,190);// define the radius of the circleint radius = 100;//Draw a Filled Circle using the circle() Functioncircle(Filled_circle_image, circle_center, radius, Scalar(255, 0, 0), -1, 8, 0);// display the output imageimshow("Circle on Image", circle_image);waitKey();

在这里插入图片描述

绘制矩阵

现在,使用OpenCV中的rectangle()函数在图像上绘制一个矩形。查看其语法:

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

在rectangle()函数中,为矩形的角提供起点(左上)和终点(右下)。
现在通过这个示例代码,在美女的脸上用红色矩形标注图像。

Python

# make a copy of the original image
imageRectangle = img.copy()
# define the starting and end points of the rectangle
start_point =(350,400)
end_point =(500,550)
# draw the rectangle
cv2.rectangle(imageRectangle, start_point, end_point, (0, 0, 255), thickness= 3, lineType=cv2.LINE_8)
# display the output
cv2.imshow('imageRectangle', imageRectangle)
cv2.waitKey(0)

C++

// make a copy of the original imageMat rect_image = image.clone();// Define the starting and end points for the rectanglePoint start_point(300,115);Point end_point(475,225);// Draw a rectangle using the rectangle() functionrectangle(rect_image, start_point, end_point, Scalar(0,0,255), 3, 8, 0);imshow("Rectangle on Image", rect_image);waitKey();

在这里插入图片描述

绘制椭圆

使用OpenCV中的ellipse()函数在图像上绘制椭圆。ellipse()函数的语法与圆的语法非常相似。除了,您需要指定以下值而不是半径:

  • 椭圆的长轴和短轴长度
  • 旋转角度
  • 椭圆的起始角和终止角

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

在下面的示例代码中,使用以下内容对图像进行标注:

  • 水平蓝色椭圆
  • 垂直红色椭圆

OpenCV中的绘图功能非常相似,因此很容易掌握。此外,它们还提供可选参数,以便可以自由定义许多基本几何形状的位置和方向。

Python

# make a copy of the original image
imageEllipse = img.copy()
# define the center point of ellipse
ellipse_center = (415,190)
# define the major and minor axes of the ellipse
axis1 = (100,50)
axis2 = (125,50)
# draw the ellipse
#Horizontal
cv2.ellipse(imageEllipse, ellipse_center, axis1, 0, 0, 360, (255, 0, 0), thickness=3)
#Vertical
cv2.ellipse(imageEllipse, ellipse_center, axis2, 90, 0, 360, (0, 0, 255), thickness=3)
# display the output
cv2.imshow('ellipse Image',imageEllipse)
cv2.waitKey(0)

C++

// make a copy of the original imageMat imageEllipse = img.clone();// define the center point of ellipsePoint ellipse_center(415,190);// define the major and minor axes of the ellipsePoint axis1(100, 50);Point axis2(125, 50);// Draw an ellipse using the ellipse() function//Horizontalellipse(imageEllipse, ellipse_center, axis1, 0, 0, 360, Scalar(255, 0, 0), 3, 8, 0);// Verticalellipse(imageEllipse, ellipse_center, axis2, 90, 0, 360, Scalar(0, 0, 255), 3, 8, 0);// display the outputimshow("Ellipses on Image", imageEllipse);waitKey();

在这里插入图片描述

绘制带轮廓和填充半椭圆

在上面示例中,将前面的代码修改为:

  1. 只绘制蓝色椭圆的一半
  2. 将垂直红色椭圆更改为水平红色椭圆,该椭圆为半填充

为此,进行以下更改:

  • 将蓝色椭圆的endAngle设置为180度
  • 将红色椭圆的方向从90更改为0
  • 指定红色椭圆的起点和终点角度,分别为0和180
  • 将红色椭圆的厚度指定为负数

Python

# make a copy of the original image
halfEllipse = img.copy()
# define the center of half ellipse
ellipse_center = (415,190)
# define the axis point
axis1 = (100,50)
# draw the Incomplete/Open ellipse, just a outline
cv2.ellipse(halfEllipse, ellipse_center, axis1, 0, 180, 360, (255, 0, 0), thickness=3)
# if you want to draw a Filled ellipse, use this line of code
cv2.ellipse(halfEllipse, ellipse_center, axis1, 0, 0, 180, (0, 0, 255), thickness=-2)
# display the output
cv2.imshow('halfEllipse',halfEllipse)
cv2.waitKey(0)

C++

//make a copy of the original imageMat halfEllipse = image.clone();// define the center of half ellipsePoint ellipse_center(415,190);//define the axis pointPoint axis1(100, 50);// draw the Half Ellipse, just the outlineellipse(halfEllipse, ellipse_center, axis1, 0, 180, 360, Scalar(255, 0, 0), 3, 8, 0);// if you want to draw a Filled ellipse, use this line of codeellipse(halfEllipse, ellipse_center, axis1, 0, 0, 180, Scalar(0, 0, 255), -2, 8, 0);// display the outputimshow("Half-Ellipses on Image", halfEllipse);waitKey();

在这里插入图片描述

使用文本注释图像

最后,尝试用文本标注图像。为此,使用OpenCV中的putText()函数。看看它的语法,然后是参数:

putText(image, text, org, font, fontScale, color)
  • 通常,第一个参数是输入图像。
  • 下一个参数是我们要用来标注图像的实际文本字符串。
  • 第三个参数指定文本字符串左上角的起始位置。
  • 接下来的两个参数指定字体样式和比例。

OpenCV支持Hershey字体集合中的几种字体样式,以及斜体字体。查看此列表:

Column
FONT_ HERSHEY_SIMPLEX=0
FONT_ HERSHEY_PLAIN=1,
FONT_ HERSHEY_DUPLEX=2,
FONT_ HERSHEY_ COMPLEX=3,
FONT_ HERSHEY_TRIPLEX=4
FONT_HERSHEY_COMPLEX_SMALL=5,
FONT_ HERSHEY_SCRIPT_SIMPLEX=6
FONT_ HERSHEY_SCRIPT_COMPLEX=7
FONT_ITALIC=16
  • 字体比例是一个浮点值,用于向上或向下缩放字体的基本大小。根据图像的分辨率,选择适当的字体比例。
  • 最后一个必需的参数是颜色,它被指定为BGR三元组。 看看这段代码,了解如何实现这些参数以在图像上显示文本字符串。

Python

# make a copy of the original image
imageText = img.copy()
#let's write the text you want to put on the image
text = 'This is a black silk beauty!'
#org: Where you want to put the text
org = (50,350)
# write the text on the input image
cv2.putText(imageText, text, org, fontFace = cv2.FONT_HERSHEY_COMPLEX, fontScale = 1.5, color = (250,225,100))
# display the output image with text over it
cv2.imshow("Image Text",imageText)
cv2.waitKey(0)
cv2.destroyAllWindows()

C++

// make a copy of the original imageMat imageText = img.clone();// Write text using putText() functionputText(imageText, "This is a black silk beauty!", Point(50,350), FONT_HERSHEY_COMPLEX, 1.5, Scalar(250,225,100));imshow("Text on Image", imageText);waitKey(0);

在这里插入图片描述

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...