OpenCV入门(C++/Python)- 使用OpenCV标注图像(六)
使用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 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();
绘制带轮廓和填充半椭圆
在上面示例中,将前面的代码修改为:
- 只绘制蓝色椭圆的一半
- 将垂直红色椭圆更改为水平红色椭圆,该椭圆为半填充
为此,进行以下更改:
- 将蓝色椭圆的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);
OpenCV入门(C++/Python)- 使用OpenCV标注图像(六)相关推荐
- 转载:【OpenCV入门教程之四】 ROI区域图像叠加初级图像混合 全剖析
[OpenCV入门教程之四] ROI区域图像叠加&初级图像混合 全剖析 浅墨_毛星云 2014-03-10 12:48:05 157425 收藏 19 最后发布:2014-03-10 12:4 ...
- OpenCV入门基础操作(二)----图像像素的处理
OpenCV入门基础操作(二)----图像像素的处理 像素处理 读取一个图像像素 修改像素值 代码案例 像素处理 读取一个图像像素 在读取图像的时候一般要用到如下的命令: 返回值=图像(位置参数), ...
- OpenCV入门 | 使用Python实现计算机视觉的第一步
不久前,我为一个大学项目训练了一个目标检测模型,但说实话,除了它需要很多计算力以及需要长时间观察我的训练模型,我不记得其他太更多的东西了. 最近我对这些话题重拾了兴趣,我决定重新开始学习,但这一次我会 ...
- 【OpenCV入门教程之一】 OpenCV 2.4.8 +VS2010的开发环境配置
目录(?)[-] 因为读研期间的研究方向是图像处理所以浅墨这段时间闭门研究了很多OpenCV和图像处理相关的知识与内容眼看自己积累到一定的程度了于是决定开始开设这个OpenCV系列专栏总结自己所学也分 ...
- 【OpenCV入门学习--python】图像的矩Image Moments
例子源于OpenCV官网–图像的矩 (https://docs.opencv.org/4.x/d0/d49/tutorial_moments.html) 使用OpenCV函数cv::moments: ...
- 【OpenCV入门学习--python】索贝尔算子Sobel operator提取边缘
例子源于OpenCV官网手册(https://docs.opencv.org/4.x/d2/d2c/tutorial_sobel_derivatives.html) 使用OpenCV函数Sobel() ...
- 【OpenCV入门学习--python】Image Segmentation with Distance Transform and Watershed Algorithm图像分割
例子源于OpenCV官网–基于距离变换和分水岭算法的图像分割 (https://docs.opencv.org/4.x/d2/dbd/tutorial_distance_transform.html) ...
- 【OpenCV入门学习--python】Anisotropic image segmentation by a gradient structure tensor
例子源于OpenCV官网–基于梯度结构张量的各向异性图像分割 (https://docs.opencv.org/4.x/d4/d70/tutorial_anisotropic_image_segmen ...
- opencv入门系列教学(五)图像的基本操作(像素值、属性、ROI和边框)
0.序言 每个图像是由一个个点组成的,而这些点可以表示为像素值的形式. 这篇博客里我们将学会: 访问像素值并修改它们 . 访问图像属性 . 设置感兴趣区域(ROI) . 分割和合并图像. 对于图像的基 ...
- 精简 opencv python_基于Python的OpenCV人脸检测!简直不要太简单!
一.文章概述 注意:本文只是人脸检测,人脸识别的实现请参见本人另一篇博客:基于OpenCV+TensorFlow+Keras实现人脸识别 本文将要讲述的是Python环境下如何用OpenCV检测人脸, ...
最新文章
- 他保送北大、读完博士选择回中学任教,“做科研太枯燥,自己更适合教书”...
- java代码分类_08 java代码块的概述和分类
- 有一种努力叫:靠 自 己!
- .net数据源控件绑定mysql_理解asp.net中DropDownList编辑数据源,绑定数据库数据。...
- 文本框输入即时Ajax搜索,JQuery+AJAX实现搜索文本框的输入提示功能
- python网络编程linux清华_Python网络编程篇之socket
- 轻松学SQL Server数据库pdf
- Java高并发编程详解-代码在本地
- 什么是在JavaScript中扩展错误的好方法?
- 如何利用Mac电脑制作让你脱颖而出的简历
- 【LeetCode-面试算法经典-Java实现】【136-Single Number(仅仅出现一次的数字)】
- ArrayList源码解读(jdk1.8)
- Android Gradle 完整指南(转)
- python下载微信公众号文章_Python 抓取微信公众号文章
- #1.生活小妙招-联想小新潮7000电脑摄像头打不开
- Windows10 下面一个非常快速而精悍的看图软件 - IrfanView
- mui-添加自定义图标(彩色)
- 翻译Computer Relaying for Power Systems-2nd Edition(Arun G. Phadke, James S. Thorp)
- 零基础如何入门学习电脑编程?
- 数据库候选关键词怎么求_关系模式中候选关键字的图论求解法
热门文章
- C/C++/Java/Go/Rust,Python喊你来打擂:3秒钟内统计出小于1亿的素数个数
- AOP 切点指定多个包
- 推荐BIG DATA Spark 的7本学习电子书籍(大牛专区)
- MySQL入门语法(视频学习笔记)
- 带你学MySQL系列 | 什么是数据定义语言(DDL)呢?
- 插件 | 蛋白序列集合功能注释快速完成 - Quick Protein Anno
- 硬件设计 之 CAN通信-DSView逻辑分析仪使用-CAN波形测试
- 步进电机控制,RPM与PPS单位关系分析
- AD软件设置过孔盖油与过孔开窗
- 2人同步听歌软件_安卓、IOS双端支持的这款“羞羞”软件!单身慎入啊.....