其他分享
首页 > 其他分享> > 图像几何形状绘制

图像几何形状绘制

作者:互联网

绘制圆形

circle(img, center, radius, color, thickness)

绘制矩形

// 原型一
rectangle(img, pt1, pt2, color, thickness)

// 原型二
rectangle(img, rec, color, thickness)

Rect变量在 OpenCV 4 中表示矩形的含义,与Point、Vec3b等类型相同,都是在图像处理中常用的类型。Rect表示的是矩形左上角像素坐标,以及矩形的长和宽,该类型定义的格式为Rect(像素的x坐标,像素的y坐标, 矩形的宽,矩形的高),其中可以存放的数据类型也分别为int型(Rect2i或者Rect)、double类型(Rect2d)和float类型(Rect2f)

绘制直线

line(img, pt1, pt2, color, thickness)

绘制椭圆

ellipse(img, center, axes, angle, startAngle, endAngle, color, thickness)

:通过椭圆弧起始和终止角度,可以绘制完整的椭圆或一部分椭圆弧

添加文本

putText(img, text, org, fontFace, fontScale, color, thickness)

示例代码

#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
    Mat_<Vec3b> img = imread("/home/kslas/OpenCV/sample.jpg");
    imshow("Original Image", img);

    /* Draw the image from point A to B */
    Mat_<Vec3b> line_image = img.clone();
    Point pointA(200,80);
    Point pointB(450,80);
    line(line_image, pointA, pointB, Scalar(255, 255, 0), 3);
    imshow("Line on Image", line_image);

    /* Draw a circle using the circle() Function */
    Mat_<Vec3b> circle_image = img.clone();
    // define the center of circle
    Point circle_center(415,190);
    // define the radius of circle
    int radius = 100;
    circle(circle_image, circle_center, radius, Scalar(0, 0, 255), 3);
    imshow("Circle on Image", circle_image);

    Mat_<Vec3b> Filled_circle_image = img.clone();
    circle(Filled_circle_image, circle_center, radius, Scalar(255, 0, 0), -1);
    imshow("Filled Circle on Image", Filled_circle_image);

    /* Draw a rectangle using the rectangle() function */
    Mat_<Vec3b> rect_image = img.clone();
    // Define the starting and end points for the rectangle
    Point start_point(300,115);
    Point end_point(475,225);
    rectangle(rect_image, start_point, end_point, Scalar(0,0,255), 3);
    imshow("Rectangle on Image", rect_image);

    /* Draw an ellipse using the ellipse() function */
    Mat_<Vec3b> 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);
    //Horizontal
    ellipse(imageEllipse, ellipse_center, axis1, 0, 0, 360, Scalar(255, 0, 0), 3);
    // Vertical
    ellipse(imageEllipse, ellipse_center, axis2, 90, 0, 360, Scalar(0, 0, 255), 3);
    imshow("Ellipses on Image", imageEllipse);

    /* draw the Half Ellipse, just the outline */
    Mat_<Vec3b> halfEllipse = img.clone();
    ellipse(halfEllipse, ellipse_center, axis1, 0, 180, 360, Scalar(255, 0, 0), 3);
    // 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);
    imshow("Half-Ellipses on Image", halfEllipse);

    /* Write text using putText() function */
    Mat_<Vec3b> imageText = img.clone();
    putText(imageText, "I am a Happy dog!", Point(50,350), FONT_HERSHEY_COMPLEX, 1.5, Scalar(250,225,100));
    imshow("Text on Image", imageText);

    waitKey(0);
    destroyAllWindows();
    return 0;
}

运行结果:

标签:center,img,image,图像,Scalar,几何,circle,绘制,ellipse
来源: https://www.cnblogs.com/TNTksals/p/15839592.html