Function for drawing images in opencv

Introduction to Common Functions

A brief introduction to C++ functions in OpenCV for drawing different graphics:

  1. Draw a rectangular box:

    cv::rectangle(image, cv::Point pt1, cv::Point pt2, cv::Scalar color, int thickness);
    • image: target image
    • pt1 and pt2: diagonal vertex coordinates of the rectangle
    • color: Color of the rectangle
    • thickness: The thickness of the line. If it is a negative number, it means filling the rectangle.
  2. Draw text:

    cv::putText(image, text, cv::Point org, int fontFace, double fontScale, cv::Scalar color, int thickness);
    • image: target image
    • text: The text to be drawn
    • org: starting coordinates of the text
    • fontFace: font type
    • fontScale: font scaling factor
    • color: text color
    • thickness: The thickness of the line
  3. Draw a circle:

    cv::circle(image, cv::Point center, int radius, cv::Scalar color, int thickness);
    • image: target image
    • center: circle center coordinates
    • radius: Radius
    • color: The color of the circle
    • thickness: The thickness of the line. If it is a negative number, it means the filled circle
  4. Draw an ellipse:

    cv::ellipse(image, cv::Point center, cv::Size axes, double angle, double startAngle, double endAngle, cv::Scalar color, int thickness);
    • image: target image
    • center: ellipse center coordinates
    • axes: the size of the major and minor axes
    • angle: ellipse rotation angle
    • startAngle and endAngle: the starting and ending angles of the ellipse
    • color: The color of the ellipse
    • thickness: The thickness of the line. If it is a negative number, it means filling the ellipse.
  5. Draw line segments:

    cv::line(image, cv::Point pt1, cv::Point pt2, cv::Scalar color, int thickness);
    • image: target image
    • pt1 and pt2: the starting and ending coordinates of the line segment
    • color: The color of the line segment
    • thickness: The thickness of the line
  6. Draw a polygon:

    cv::polylines(image, std::vector<std::vector<cv::Point>> contours, bool isClosed, cv::Scalar color, int thickness);
    • image: target image
    • contours: Vertex coordinates of polygons
    • isClosed: Whether to close the polygon
    • color: The color of the polygon
    • thickness: The thickness of the line
  7. Draw a filled polygon:

    cv::fillPoly(image, std::vector<std::vector<cv::Point>> contours, cv::Scalar color);
    • image: target image
    • contours: Vertex coordinates of polygons
    • color: fill color
  8. Draw an arrow:

    cv::arrowedLine(image, cv::Point pt1, cv::Point pt2, cv::Scalar color, int thickness, int line_type, int tipLength);
    • image: target image
    • pt1 and pt2: the starting and ending coordinates of the arrow
    • color: The color of the arrow
    • thickness: The thickness of the line
    • line_type: type of line
    • tipLength: The tip length of the arrow

Test example

Using the OpenCV library in C++ can easily draw various graphics and operations such as rectangular boxes, text, circles, ellipses, line segments, and polygons on images. Here is some sample code to demonstrate how to perform these operations on an image:

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main() {
    //Create a blank image
    cv::Mat image = cv::Mat::zeros(400, 400, CV_8UC3);

    //Draw a rectangle on the image
    cv::rectangle(image, cv::Point(50, 50), cv::Point(150, 150), cv::Scalar(0, 0, 255), 2);

    // Draw text on the image
    cv::putText(image, "OpenCV", cv::Point(160, 180), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 255), 2);

    //Draw a circle on the image
    cv::circle(image, cv::Point(200, 200), 50, cv::Scalar(0, 255, 0), 2);

    //Draw an ellipse on the image
    cv::ellipse(image, cv::Point(300, 300), cv::Size(60, 40), 45, 0, 360, cv::Scalar(255, 0, 0), 2);

    //Draw a line segment on the image
    cv::line(image, cv::Point(50, 200), cv::Point(150, 300), cv::Scalar(255, 0, 255), 2);

    // Draw a polygon (pentagon) on the image
    std::vector<cv::Point> star_points;
    star_points.push_back(cv::Point(250, 50));
    star_points.push_back(cv::Point(265, 100));
    star_points.push_back(cv::Point(305, 100));
    star_points.push_back(cv::Point(280, 130));
    star_points.push_back(cv::Point(295, 175));
    star_points.push_back(cv::Point(250, 150));
    star_points.push_back(cv::Point(205, 175));
    star_points.push_back(cv::Point(220, 130));
    star_points.push_back(cv::Point(195, 100));
    star_points.push_back(cv::Point(235, 100));
    cv::polylines(image, star_points, true, cv::Scalar(255, 255, 0), 2);

    // Draw a Bayesian curve on the image
    cv::Mat curve_image = cv::Mat::zeros(400, 400, CV_8UC3);
    std::vector<cv::Point> curve_points;
    for (int x = 0; x < curve_image.cols; x + + ) {
        int y = 200 - static_cast<int>(50 * sin(x * 0.05));
        curve_points.push_back(cv::Point(x, y));
    }
    cv::polylines(curve_image, curve_points, false, cv::Scalar(0, 255, 255), 2);
    
        //Create a subimage and draw the subimage onto the image
        //Create sub-image (ROI)
        cv::Rect roi_rect(100, 100, 200, 150); // Create a 200x150 sub-image at (100, 100)
        cv::Mat roi = image(roi_rect);

        //Draw a rectangle on the subimage
        cv::rectangle(roi, cv::Point(20, 20), cv::Point(180, 130), cv::Scalar(0, 0, 255), 2);

        // Draw text on the subimage
        cv::putText(roi, "Subimage", cv::Point(40, 40), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 255), 2);

    // display image
    cv::imshow("Shapes", image);
    cv::imshow("Sin Curve", curve_image);

    //wait for key press
    cv::waitKey(0);

    return 0;
}

The above example code creates a 400×400 blank image, and then draws a rectangular box, text, circle, ellipse, line segment and polygon (pentagonal star) on the image, as well as a sin curve subgraph. You can modify the position, color, size and other parameters of the graphics as needed. Please make sure that the OpenCV library is correctly included in your C++ project and the compilation environment is configured.