Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use OpenCV to draw graphics

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of "how to draw graphics with OpenCV". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to draw graphics with OpenCV" can help you solve the problem.

1. Draw a straight line

The drawing line function is cv::line, and the full form of the function is as follows

Void line (InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0); / * @ param img Image @ param pt1 Segment Endpoint 1@param pt2 Segment Endpoint 2@param color Segment Color @ param thickness Line width @ Type of param lineType Line @ decimal offset of param shift Point coordinates * /

Color can be constructed using cv::Scalar, but the order in which parameters are passed is BGR, and it is more intuitive to use CV_RGB macros, passing in RGB order

LineType values include LINE_4, LINE_8 and LINE_AA, which represent 4 connection lines, 8 connection lines, and anti-aliasing lines, respectively. Straight lines are generated by different algorithms (or FILLED=-1, which can be filled directly).

Shift is the bit offset of the binary representation of the pointing coordinates, each plus 1 coordinate value minus half, the experimental results, I do not know whether the understanding is correct, just use the default value of 0

Drawing a red line with a lineweight of 1 between two points is defined as a function

Void DrawLine (const cv::Mat& destImg, const cv::Point& pt1, const cv::Point& pt2) {cv::line (destImg, pt1, pt2, CV_RGB (255,0,0), 1);}

The following example draws a straight line between the two mouse clicks, and you can press the Enter key to save the image.

Cv::Mat glossary image / original image cv::Mat galleeditPointsBash / edited image std::vector GroueditPointsBash / point of the graph being drawn std::vector GoblinesBash / all segments void RedrawAllLines () {g_originImage.copyTo (g_editImage); / / restore the background image for (int I = 0; I)

< g_lines.size(); i++) { if (g_lines[i].size() >

= 2) {DrawLine (g _ lines [I] [0], g _ lines [I] [1]) } void OnDrawLineMouseEvent (int event, int x, int y, int flags, void* userdata) {if (event = = cv::EVENT_LBUTTONDOWN) {if (g_editPoints.size () > 0) {/ / add to the list of line segments after the mouse is pressed at the second point and redraw the image g_editPoints.push_back (cv::Point (x, y)) G_lines.push_back (g_editPoints); RedrawAllLines (); g_editPoints.clear (); imshow ("image", g_editImage);} else {g_editPoints.push_back (cv::Point (x, y)) / / first point}} else if (event = = cv::EVENT_MOUSEMOVE) {if (g_editPoints.size () > 0) {/ / during mouse movement, draw a straight line to the mouse position, but the current mouse point is not added to the g_editPoints RedrawAllLines () DrawLine [g _ editPoints.size ()-1], cv::Point (x, y); imshow ("image", g_editImage);}} int main (int argc, char * * arv) {g_originImage = cv::imread ("walkers.jpg"); g_originImage.copyTo (g_editImage); cv::namedWindow ("image") Imshow ("image", g_editImage); cv::setMouseCallback ("image", OnDrawLineMouseEvent); int key = cv::waitKey (0); while (key! = 27) {if (key = = 13) {cv::imwrite ("testsave.png", g_editImage);} key = cv::waitKey (0);} return 0;} 2, draw a circle

Cv::circle, a function for drawing circles

Void circle (InputOutputArray img, Point center, int radius,const Scalar& color, int thickness = 1 param img lineType = LINE_8, int shift = 0); / * @ param img image @ param center center @ param radius radius @ param color line color @ param thickness lineweight, if less than 0, fill the offset of the circle @ param lineType line type @ param shift center and radius value * /

Draw a circle with two points, the first point is the center of the circle, the distance between the two points is the radius of the circle, defined as a function DrawCircle

Void DrawCircle (const cv::Mat& destImg, const cv::Point& pt1, const cv::Point& pt2) {cv::Point deltaPt = pt2-pt1; float radius = cv::sqrt (deltaPt.x*deltaPt.x + deltaPt.y*deltaPt.y); cv::circle (destImg, pt1, radius, CV_RGB (255,0,0), 1);}

The following example implements a circle drawn above with two mouse clicks. The main function is the same as drawing a straight line, as long as the mouse event callback is changed to OnDrawCircleMouseEvent.

Std::vector gears / all circles void RedrawAllCircles () {g_originImage.copyTo (g_editImage); / / restore the background image for (int I = 0; I)

< g_circles.size(); i++) { if (g_circles[i].size() >

= 2) {DrawCircle (g_editImage, gcircles [I] [0]);} void OnDrawCircleMouseEvent (int event, int x, int y, int flags, void* userdata) {if (event = = cv::EVENT_LBUTTONDOWN) {if (g_editPoints.size () > 0) {g_editPoints.push_back (x, y)) G_circles.push_back (g_editPoints); RedrawAllCircles (); g_editPoints.clear (); imshow ("image", g_editImage);} else {g_editPoints.push_back (cv::Point (x, y)) / / first point}} else if (event = = cv::EVENT_MOUSEMOVE) {if (g_editPoints.size () > 0) {RedrawAllCircles (); DrawCircle (g_editImage, geditPoints [g _ editPoints.size ()-1], cv::Point (x, y)); imshow ("image", g_editImage) 3. Draw an ellipse

The function cv::ellipse for drawing an ellipse has two forms, one of which is defined as follows

Void ellipse (InputOutputArray img, const RotatedRect& box, const Scalar& color,int thickness = 1, int lineType = LINE_8); / * @ param img image @ param box can adjust the rectangle @ param color line color @ param thickness line weight of the rotation angle, and fill the ellipse @ param lineType line type if less than 0 * /

An ellipse is drawn inside a rectangle composed of two points, which is defined as the function DrawEllipse. The rotation of the rectangle is not considered here, and it is fixed to 0.

Void DrawEllipse (const cv::Mat& destImg, const cv::Point& pt1, const cv::Point& pt2) {cv::Point2f center = cv::Point2f ((pt1.x + pt2.x) / 2.0, (pt1.y + pt2.y) / 2.0); cv::Point2f size = cv::Point2f (cv::abs (pt2.x-pt1.x), cv::abs (pt2.y-pt1.y)) Cv::ellipse (destImg,cv::RotatedRect (center, size, 0), CV_RGB (255,0,0), 1;}

The following example implements drawing an ellipse between two mouse clicks. The main function, like drawing a straight line, changes the mouse event callback to OnDrawEllipseMouseEvent.

Std::vector glossy images / all ellipses void RedrawAllEllipses () {g_originImage.copyTo (g_editImage); / / restore the background image for (int I = 0; I)

< g_ellipses.size(); i++) { if (g_ellipses[i].size() >

= 2) {DrawEllipse (g_editImage, geses [I] [0]);}} void OnDrawEllipseMouseEvent (int event, int x, int y, int flags, void* userdata) {if (event = = cv::EVENT_LBUTTONDOWN) {if (g_editPoints.size () > 0) {g_editPoints.push_back (cv::Point (x, y)) G_ellipses.push_back (g_editPoints); RedrawAllEllipses (); g_editPoints.clear (); imshow ("image", g_editImage);} else {g_editPoints.push_back (cv::Point (x, y)) / / first point}} else if (event = = cv::EVENT_MOUSEMOVE) {if (g_editPoints.size () > 0) {RedrawAllEllipses (); DrawEllipse (g_editImage, geditPoints [g _ editPoints.size ()-1], cv::Point (x, y)); imshow ("image", g_editImage) 4. Draw a rectangle

The function cv::rectangle for drawing a rectangle has two forms, one of which is defined as follows

Void rectangle (InputOutputArray img, Rect rec,const Scalar& color, int thickness = 1 param rec int lineType = LINE_8, int shift = 0); / * @ param img image @ param rec rectangular coordinates @ param color line color @ param thickness lineweight, if less than 0, fill the ellipse @ param lineType line type @ param shift point coordinate offset * /

Draw a rectangle between two points, defined as the function DrawRectangle

Void DrawRectangle (const cv::Mat& destImg, const cv::Point& pt1, const cv::Point& pt2) {cv::rectangle (destImg, cv::Rect (pt1, pt2), CV_RGB (255,0,0), 1);}

The following example draws a rectangle between two mouse clicks. The main function, like drawing a straight line, changes the mouse event callback to OnDrawRectangleMouseEvent.

Std::vector Ganganglespace / all rectangular void RedrawAllRectangles () {g_originImage.copyTo (g_editImage); / / restore the background image for (int I = 0; I)

< g_rectangles.size(); i++) { if (g_rectangles[i].size() >

= 2) {DrawRectangle (g_editImage, ggangangles [I] [0]);}} void OnDrawRectangleMouseEvent (int event, int x, int y, int flags, void* userdata) {if (event = = cv::EVENT_LBUTTONDOWN) {if (g_editPoints.size () > 0) {g_editPoints.push_back (cv::Point (x, y)) G_rectangles.push_back (g_editPoints); RedrawAllRectangles (); g_editPoints.clear (); imshow ("image", g_editImage);} else {g_editPoints.push_back (cv::Point (x, y)) / / first point}} else if (event = = cv::EVENT_MOUSEMOVE) {if (g_editPoints.size () > 0) {RedrawAllRectangles (); DrawRectangle (g_editImage, geditPoints [g _ editPoints.size ()-1], cv::Point (x, y)); imshow ("image", g_editImage) 5. Draw polygon outline

The function cv::polylines for drawing polygons has two forms, one of which is defined as follows

Void polylines (InputOutputArray img, InputArrayOfArrays pts,bool isClosed, const Scalar& color,int thickness = 1, int lineType = LINE_8, int shift = 0); / * @ param img image @ param pts polygon coordinate array @ param isClosed whether to draw closed polygons @ param color line color @ param thickness line weight @ param lineType line type @ param shift point coordinate offset * /

The pts here is a 2-dimensional array that represents multiple polygons. The following functions are implemented to draw multiple polygons and single polygons respectively.

Void DrawMultiPolys (const cv::Mat& destImg, const std::vector& points,bool bClose) {cv::polylines (destImg, points, bClose, CV_RGB (255,0,0), 1);} void DrawOnePoly (const cv::Mat& destImg, const std::vector& points,bool bClose) {if (points.size () > = 2) {std::vector polyPoints; polyPoints.push_back (points); DrawMultiPolys (destImg,polyPoints,bClose);}}

The following example draws polygons where there are multiple mouse clicks. The main function, like drawing a straight line, changes the callback of mouse events to OnDrawPolyMouseEvent.

Std::vector glossy polyspace / all polygons void RedrawAllPolys () {g_originImage.copyTo (g_editImage); / / restore the background image DrawMultiPolys. } void OnDrawPolyMouseEvent (int event, int x, int y, int flags, void* userdata) {if (event = = cv::EVENT_LBUTTONDOWN) {if (g_editPoints.size () > 0) {g_editPoints.push_back (cv::Point (x, y)); RedrawAllPolys (); DrawOnePoly (g_editImage, g_editPoints, false) / / the polygons being drawn should be drawn separately, and imshow ("image", g_editImage) cannot be closed;} else {g_editPoints.push_back (cv::Point (x, y)) / / first point}} else if (event = = cv::EVENT_MOUSEMOVE) {if (g_editPoints.size () > 0) {RedrawAllPolys (); DrawOnePoly / / the polygons being drawn should be drawn separately, and cannot be closed DrawLine (g_editImage, geditPoints [g _ editPoints.size ()-1], cv::Point (x, y)); / / draw a straight line imshow ("image", g_editImage) to the mouse position }} else if (event = = cv::EVENT_RBUTTONDOWN) {/ / right-click to end polygon drawing and add to g_polys g_polys.push_back (g_editPoints); RedrawAllPolys (); g_editPoints.clear (); cv::imshow ("image", g_editImage);}} 6, draw fill polygon

The draw fill polygon function cv::fillPoly has two forms, one of which is defined as follows

Void fillPoly (InputOutputArray img, InputArrayOfArrays pts,const Scalar& color, int lineType = LINE_8, int shift = 0 Point offset = Point ()); / * @ param img image @ param pts polygon coordinate array @ param color line color @ param lineType line type @ param shift point coordinate offset @ param offset offset of all polygon points * /

Drawing a filled polygon is similar to drawing a polygon outline, as long as you replace polylines with fillpoly

Void DrawMultiPolys (const cv::Mat& destImg, const std::vector& points) {cv::fillPoly (destImg, points,CV_RGB (255,0,0));} void DrawOnePoly (const cv::Mat& destImg, const std::vector& points) {if (points.size () > = 2) {std::vector polyPoints; polyPoints.push_back (points); DrawMultiPolys (destImg,polyPoints);}}

If you combine all the above functions in a certain way, you can draw a variety of shapes on the image and save them.

This is the end of the content about "how to draw graphics with OpenCV". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report