Introduction to Common Functions
A brief introduction to C++ functions in OpenCV for drawing different graphics:
-
Draw a rectangular box:
cv::rectangle(image, cv::Point pt1, cv::Point pt2, cv::Scalar color, int thickness);
image
: target imagept1
andpt2
: diagonal vertex coordinates of the rectanglecolor
: Color of the rectanglethickness
: The thickness of the line. If it is a negative number, it means filling the rectangle.
-
Draw text:
cv::putText(image, text, cv::Point org, int fontFace, double fontScale, cv::Scalar color, int thickness);
image
: target imagetext
: The text to be drawnorg
: starting coordinates of the textfontFace
: font typefontScale
: font scaling factorcolor
: text colorthickness
: The thickness of the line
-
Draw a circle:
cv::circle(image, cv::Point center, int radius, cv::Scalar color, int thickness);
image
: target imagecenter
: circle center coordinatesradius
: Radiuscolor
: The color of the circlethickness
: The thickness of the line. If it is a negative number, it means the filled circle
-
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 imagecenter
: ellipse center coordinatesaxes
: the size of the major and minor axesangle
: ellipse rotation anglestartAngle
andendAngle
: the starting and ending angles of the ellipsecolor
: The color of the ellipsethickness
: The thickness of the line. If it is a negative number, it means filling the ellipse.
-
Draw line segments:
cv::line(image, cv::Point pt1, cv::Point pt2, cv::Scalar color, int thickness);
image
: target imagept1
andpt2
: the starting and ending coordinates of the line segmentcolor
: The color of the line segmentthickness
: The thickness of the line
-
Draw a polygon:
cv::polylines(image, std::vector<std::vector<cv::Point>> contours, bool isClosed, cv::Scalar color, int thickness);
image
: target imagecontours
: Vertex coordinates of polygonsisClosed
: Whether to close the polygoncolor
: The color of the polygonthickness
: The thickness of the line
-
Draw a filled polygon:
cv::fillPoly(image, std::vector<std::vector<cv::Point>> contours, cv::Scalar color);
image
: target imagecontours
: Vertex coordinates of polygonscolor
: fill color
-
Draw an arrow:
cv::arrowedLine(image, cv::Point pt1, cv::Point pt2, cv::Scalar color, int thickness, int line_type, int tipLength);
image
: target imagept1
andpt2
: the starting and ending coordinates of the arrowcolor
: The color of the arrowthickness
: The thickness of the lineline_type
: type of linetipLength
: 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.