OpenCV入门(C++/Python)- 使用OpenCV标注图像(六)

news2024/10/7 20:34:58

使用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 <opencv2/opencv.hpp>
#include <iostream>
// Using namespaces to nullify use of c::function(); syntax and std::function(); syntax
using namespace std;
using namespace cv;
int main()
{
    // Read Images
    Mat img = imread("sample.jpg");
    // Display Image
    imshow("Original Image", img);
    waitKey();
    // Print Error message if image is null
    if (img.empty())
        {
            cout << "Could not read image" << endl;
        }
    // Draw line on image
    Mat 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 ( x 1 , y 1 ) A(x_1,y_1) A(x1,y1)到点 B ( x 2 , y 2 ) 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 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", 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 image
   Mat circle_image = img.clone();
   // define the center of circle
   Point circle_center(415,190);
   // define the radius of circle
   int radius = 100;
   // Draw a circle using the circle() Function
   circle(circle_image, circle_center, radius, Scalar(0, 0, 255), 3, 8, 0);
   // Display the result
   imshow("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 image
   Mat Filled_circle_image = img.clone();
   // define the center of circle
   Point circle_center(415,190);
   // define the radius of the circle
   int radius = 100;
   //Draw a Filled Circle using the circle() Function
   circle(Filled_circle_image, circle_center, radius, Scalar(255, 0, 0), -1, 8, 0);
   // display the output image
   imshow("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 image
   Mat rect_image = image.clone();
   // Define the starting and end points for the rectangle
   Point start_point(300,115);
   Point end_point(475,225);
   // Draw a rectangle using the rectangle() function
   rectangle(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 image
   Mat imageEllipse = img.clone();
   // define the center point of ellipse
   Point ellipse_center(415,190);
   // define the major and minor axes of the ellipse
   Point axis1(100, 50);
   Point axis2(125, 50);
   // Draw an ellipse using the ellipse() function
   //Horizontal
   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);
   // display the output
   imshow("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 image
   Mat halfEllipse = image.clone();
   // define the center of half ellipse
   Point ellipse_center(415,190);
   //define the axis point
   Point axis1(100, 50);
   // draw the Half Ellipse, just the outline
   ellipse(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 code
   ellipse(halfEllipse, ellipse_center, axis1, 0, 0, 180, Scalar(0, 0, 255), -2, 8, 0);
   // display the output
   imshow("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 image
   Mat imageText = img.clone();
   // Write text using putText() function
   putText(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);

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/66060.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

并查集介绍

文章目录&#xff1a;并查集原理并查集实现并查集的类结构并查集的合并统计集合数量并查集原理 在一些应用问题中&#xff0c;需要将 n 个不同的元素划分成一些不相交的集合。开始时&#xff0c;每个元素自成一个单元素集合&#xff0c;然后按照一定的规律将归于同一组元素的集…

《找对英语学习方法的第一本书》

简 述: 此书写于二十年前&#xff0c;结合我自身情况参照&#xff0c;有了一种理论指导&#xff0c;可在众多学习方法中有效抉择&#xff0c;亦能在不同阶段更换不同策略。本文为读后的一个简要归纳和札记。 文章目录第一章&#xff1a;爱之愈深、误之愈切第二章&#xff1a;我…

李宏毅2022《机器学习/深度学习》——学习笔记(5)

文章目录优化方法CNNCNN和全连接神经网络的区别感受野共享参数CNN和全连接神经网络的总结PoolingCNN流程自注意力机制自注意力机制解决的问题输入是一组向量的例子输入是一组向量时输出的可能自注意力机制核心思想自注意力机制具体细节Self-attention和CNN的关系参考资料优化方…

网络安全之从原理看懂XSS

01、XSS的原理和分类 跨站脚本攻击XSS(Cross Site Scripting)&#xff0c;为了不和层叠样式表(Cascading Style Sheets&#xff0c;CSS)的缩写混淆 故将跨站脚本攻击缩写为XSS&#xff0c;恶意攻击者往Web页面里插入恶意Script代码&#xff0c;当用户浏览该页面时&#xff0c…

七周成为数据分析师 | 业务

为什么业务重要&#xff1f; 唯有理解业务&#xff0c;才能建立业务数据模型 一.经典业务分析指标 模型未动&#xff0c;指标先行 如果你不能衡量它&#xff0c;你就无法增长它 指标建立的要点 ①核心指标 ②好的指标应该是比率 ③好的指标应该能带来显著效果 ④好的指…

民办二本程序员阿里、百度、平安等五厂面经,5 份 offer(含真题)

昨天小休&#xff0c;一位高中同学联系了我&#xff0c;说是要请我吃饭&#xff0c;有这种好事&#xff0c;我当然是毫不犹豫的答应了啦&#xff01; 等等...会不会是找我借钱的&#xff1f; 好慌&#xff0c;怎么办&#xff1f;已经答应过去了。 在后面的交谈中&#xff0c;…

Word控件Spire.Doc 【图像形状】教程(12) 如何在C#中旋转word文档上的形状

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下&#xff0c;轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具&#xff0c;专注于创建、编辑、转…

【C语言程序设计】实验 3

目录 1. 水仙花数 2. 五位回文数 3. 输入x&#xff0c;计算y 4. 百分制改为等级制 5. 同构数 6. 月份天数 7. 加一天后日期&#xff08;条件&#xff09; 8. 计算服装款&#xff08;条件&#xff09; 1. 水仙花数 【问题描述】输入一个3位正整数&#xff0c;判断该…

数据可视化之基础图表

一 前言 数据图表则是用来表现数据的一类图表&#xff0c;用来帮助用户理解数据。在这类图表中&#xff0c;以三大类图表最为常用 —— 柱状图&#xff08;条形图&#xff09;、折线图、饼图。据非官方统计&#xff0c;数据图表使用率占所有图表的类型的62%。所以&#xff0c;…

Docker之MySQL_GROUP_REPLICATION组复制(MGR)、宕机节点恢复和Spirngboot整合

三台服务器修改hosts文件 vim /etc/hosts追加内容 192.168.1.11 node1 192.168.1.12 node2 192.168.1.13 node3修改hostname vim /etc/hostname重启网络使配置文件生效 systemctl restart network三台服务器拉取MySQL镜像 docker pull mysql:8.0.23创建配置文件夹 …

PDF文档转TXT怎么转?你不知道的几种方法

PDF文档转TXT怎么转&#xff1f;我们经常需要处理PDF文件&#xff0c;根据不同的要求&#xff0c;我们经常需要将PDF文件进行转换&#xff0c;虽然PDF文件相对于其他大多数文件来说体积已经很小了&#xff0c;但是TXT文件会比PDF文件体积更小一些&#xff0c;这样我们不仅可以节…

Python工程师Java之路(w)数据库连接池Druid

概述 初阶数据库访问的步骤是【创建连接>执行SQL>关闭连接】&#xff0c;有如下不足&#xff1a; 1、创建数据库连接会浪费时间 2、大量访问时&#xff0c;频繁 GC 会导致CPU负载过高 3、如果改为不关闭连接&#xff0c;则会长期占用内存对此&#xff0c;引入“缓冲池”…

物联网各类数据如何轻松获取?秘诀就在定制文件推送服务

当前&#xff0c;数字经济已成为我国经济发展的重要驱动力。随着物联网的蓬勃发展&#xff0c;海量数据伴随着终端联网在各行各业涌现&#xff0c;越来越多的企业已然察觉隐藏在数字中的金矿&#xff0c;加入到数字化转型行列中&#xff0c;通过数据挖掘实现精细化运营&#xf…

高蛋白过敏我们该如何缓解?教你几招远离过敏吃喝无忌

许多朋友回应说&#xff0c;吃海鲜.牛肉、羊肉等高蛋白食物会发生过敏反应&#xff0c;要么脸红肿&#xff0c;要么长痘痘。看着他们贪婪的食物&#xff0c;他们只能避免吃真的很痛苦。为什么现在人们的生活条件越来越好&#xff0c;生活环境也显著改善&#xff0c;但过敏性疾病…

USB插座外壳接地的处理和emi,esd考虑

外壳是否接地&#xff0c;从理想电路环境&#xff08;没有干扰&#xff0c;也不释放干扰&#xff09;和电路原理来说&#xff0c;接和不接没有任何差异&#xff0c;也不会影响正常功能。 但是实际的电子产品的工作环境&#xff0c;是一个处于被各种干扰包围的复杂的电磁场环境&…

[附源码]JAVA毕业设计人才库构建研究(系统+LW)

[附源码]JAVA毕业设计人才库构建研究&#xff08;系统LW&#xff09; 项目运行 环境项配置&#xff1a; Jdk1.8 Tomcat8.5 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&a…

使用icacls命令设置目录及其子目录、文件的所有权限

以前一直使用cacls命令来设置权限&#xff0c;前两天输入这个命令的时候&#xff0c;却发现了一行提示&#xff1a; “注意: 不推荐使用 Cacls&#xff0c;请使用 Icacls。” 如图&#xff1a; 于是研究了一下 Icacls 这个命令。。 先放上微软官方文档&#xff1a; https:/…

单场直播销售额破7亿,11月的抖音带货风向是什么?

双11走过14年&#xff0c;今年的双11有些特别。我们发现&#xff0c;各个平台在交易额战报的发布上都变得更加保守&#xff0c;而无论是天猫还是京东&#xff0c;均首次未公布具体的交易额。在消费市场出现波动的当下&#xff0c;双11正在经历转变&#xff0c;从重视成交额&…

基于遗传算法的二进制图像重建(Matlab代码实现)

目录 &#x1f4a5;1 概述 &#x1f4da;2 运行结果 &#x1f389;3 参考文献 &#x1f4a5;1 概述 图像分辨率是评价图像成像系统的---项重要技术指标.图像分辨率又分为图像的空间分辨率、灰度分辨率和频谱分辨率等.在实际应用中,受到各种因素的限制,通过现有条件要达到所需…

JDK19都出来了~是时候梳理清楚JDK的各个版本的特性了【JDK9特性讲解】

JDK各个版本特性讲解-JDK9特性 lecture&#xff1a;波哥 一、JDK版本特性 JAVA8 及之前,版本都是特性驱动的版本更新,就是有重大的特性产生,然后进行更新 JAVA9开始,JDK开始以时间为驱动进行更新,以半年为周期,到时即更新,三年出一个长期支持版,其他都是短暂的版本 目前的长期支…