• OpenCV入门(C++/Python)- 使用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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    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 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();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    用颜色线标注图像

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

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

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

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

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

    从点 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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    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();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    绘制圆

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

    circle(image, center_coordinates, radius, color, thickness)
    
    • 1
    • 与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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    绘制实心圆

    刚刚完成了对带有红色圆圈的图像的标注。如果现在想用纯色填充这个圆圈怎么办?这很简单。只需将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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    绘制矩阵

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

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

    在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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    绘制椭圆

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

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

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

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

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

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    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
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

    绘制带轮廓和填充半椭圆

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

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    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();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    使用文本注释图像

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

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

    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

  • 相关阅读:
    Red Eye Camera开发日记之API 移植I2C 和关键接口函数
    基于matlab的强化学习QLearning路径规划性能仿真
    《C++ primer plus》精炼(OOP部分)——对象和类(7)
    读书笔记---蛤蟆先生去看心理医生
    阿里架构师整理一份企业级SSM架构实战文档,让你熟悉底层原理
    MSTP理论讲解
    SpringBoot配置文件优先级
    基于PHP实现微信客服欢迎语发送
    5.PHPwhile,foreach和for循环
    【Java】Spring boot快速上手(三)前后端分离实现小程序登录(接口篇)
  • 原文地址:https://blog.csdn.net/weixin_42010722/article/details/128198359