In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to draw all kinds of geometric figures in Python OpenCV. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
one。 Draw a line
In OpenCV, to draw a straight line, you need to get the start and end coordinates of the line, and call the cv2.line () function to achieve this function. The prototype of the function is as follows:
Img = line (img, pt1, pt2, color [, thickness [, lineType [, shift])
-img represents the image that needs to be drawn
-pt1 represents the coordinates of the first point of the segment
-pt2 represents the coordinates of the second point of the segment
-color represents the line color. You need to pass in a RGB tuple, such as (255pj0j0) for blue.
-thickness indicates line thickness
-lineType indicates the type of line
-shift represents the number of decimal places in point coordinates
The following code draws a line, creates a black image through np.zeros (), and then calls cv2.line () to draw the line, including the starting coordinates and color, weight.
#-*-coding:utf-8-*-# By:Eastmountimport cv2import numpy as np# create a black image img = np.zeros ((256 cv2.destroyAllWindows 256), np.uint8) # draw a straight line cv2.line (img, (0 0), (255255), (55255155), 5) # display image cv2.imshow ("line", img) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()
The output is shown in figure 3-1. A straight line is drawn from coordinates (0) to (255255) with a color of (55255155) and a thickness of 5.
two。 Draw a rectangle
In OpenCV, drawing a rectangle is achieved through the cv2.rectangle () function, which is prototyped as follows:
Img = rectangle (img, pt1, pt2, color [, thickness [, lineType [, shift])
-img represents the image that needs to be drawn
-pt1 represents the position coordinates of the upper-left corner of the rectangle
-pt2 represents the coordinates of the lower right corner of the rectangle
-color represents the color of the rectangle
-thickness indicates the thickness of the border
-lineType indicates the type of line
-shift represents the number of decimal places in point coordinates
The following code draws a rectangle, creates a black image through np.zeros (), and then calls cv2.rectangle () to draw the rectangle.
#-*-coding:utf-8-*-# By:Eastmountimport cv2import numpy as np# creates a black image img = np.zeros ((256 cv2.destroyAllWindows), np.uint8) # draws a rectangular cv2.rectangle (img, (20), (150250), (255), (2) # displays the image cv2.imshow ("rectangle", img) # waits for cv2.waitKey (0) cv2.destroyAllWindows ()
The output is shown in figure 3-2, with the coordinates of the upper left corner (20) and the lower right corner (150250). The color of the drawn rectangle is blue (255 line 0), and the thickness is 2.
three。 Draw a circle
In OpenCV, drawing a rectangle is achieved through the cv2.rectangle () function, which is prototyped as follows:
Img = circle (img, center, radius, color [, thickness [, lineType [, shift])
-img indicates that an image of a circle needs to be drawn
-center represents center coordinates
-radius represents the radius of the circle
-color represents the color of the circle
-thickness, if positive, indicates the thickness of the circle outline; negative thickness indicates that a filled circle is to be drawn
-lineType represents the boundary type of the circle
-shift represents the number of decimal places in central coordinates and radius values
The following code is to draw a circle.
#-*-coding:utf-8-*-# By:Eastmountimport cv2import numpy as np# create a black image img = np.zeros ((256 circle 256 cv2.destroyAllWindows 3), np.uint8) # draw a circular cv2.circle (img, (100100), 50, (255 555 cv2.destroyAllWindows 0), 4) # display image cv2.imshow ("circle", img) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()
The output is shown in figure 3-3, which draws a circle with a radius of 50, a color of (255, 255) and a thickness of 4, where the circle is (100100).
Note that if the thickness is set to "- 1", the circle drawn is solid, as shown in figure 3-4.
Cv2.circle (img, (100100), 50, (255, 255),-1)
four。 Draw an ellipse
In OpenCV, it is complicated to draw an ellipse, so it is necessary to input several more parameters, such as the position coordinates of the center point, the length of the major axis and the minor axis, the angle of the ellipse rotating counterclockwise and so on. The prototype of the cv2.ellipse () function is as follows:
Img = ellipse (img, center, axes, angle, startAngle, endAngle, color [, thickness [, lineType [, shift]])
-img indicates that an image of an ellipse needs to be drawn
-center represents ellipse center coordinates
-axes represents the length of the axis (short radius and long radius)
-angle indicates the angle of deflection (rotation counterclockwise)
-startAngle represents the angle of the starting angle of the arc (rotates counterclockwise)
-endAngle represents the angle of the arc termination angle (rotates counterclockwise)
-color represents the color of the line
If thickness is positive, it indicates the thickness of the outline of the ellipse; a negative value indicates that you want to draw a filled ellipse
-lineType represents the boundary type of the circle
-shift represents the number of decimal places in central coordinates and axis values
Here is the code to draw an ellipse.
#-*-coding:utf-8-*-# By:Eastmountimport cv2import numpy as np# create a black image img = np.zeros ((256pj256), np.uint8) # draw an ellipse # the center of the ellipse (120100) the major axis and the minor axis are (100prime50) # the angle of deflection is 2' the angle of the starting angle of the arc 0 the angle of the angle of the end of the arc is 36' color (255prime0255) line thickness 2cv2.ellipse (img, (120,100), (100meme 50), 20,0360 (255,0,255), 2) # display image cv2.imshow ("ellipse", img) # waiting for cv2.waitKey (0) cv2.destroyAllWindows ()
The output result is shown in figure 3-5, the center of the ellipse is (120100), the major axis is 100, the minor axis is 50, the deflection angle is 20, the angle of the starting angle of the arc is 0, and the angle of the end angle of the arc is 360, which represents a complete ellipse. The color drawn is (255par 0255) and the thickness is 2.
five。 Draw polygon
In OpenCV, call the cv2.polylines () function to draw polygons, which need to specify the coordinates of each vertex, and build polygons from these points. The function prototype is as follows:
Img = polylines (img, pts, isClosed, color [, thickness [, lineType [, shift])
-img represents the image to be drawn
-center represents a polygonal curve array
-isClosed indicates whether the drawn polygon is closed, and False indicates that it is not closed.
-color represents the color of the line
-thickness indicates line thickness
-lineType represents the boundary type
-shift represents the number of decimal places in vertex coordinates
Here is the code to draw a polygon.
#-*-coding:utf-8-*-# By:Eastmountimport cv2import numpy as np# create a black image img = np.zeros ((256 ellipse 256), np.uint8) # draw polygons pts = np.array ([[10Power80], [120580], [120200], [30250]]) cv2.polylines (img, [pts], True, (255,255,255), 5) # display image cv2.imshow ("ellipse", img) # waiting to display cv2.waitKey (0) cv2.destroyAllWindows ()
The output is shown in figure 3-6, and the polygons drawn are closed white shapes.
The following code is to draw a pentagram polygon.
#-*-coding:utf-8-*-# By:Eastmountimport cv2import numpy as np# creates a black image img = np.zeros (512) np.zeros), np.uint8) # draws polygons pts = np.array ([[50,190,380,420], [255,50], [120,420], [450,190]]) cv2.polylines (img, [pts], True, (0,255,255), 10) # display image cv2.imshow ("ellipse") Img) # wait for cv2.waitKey (0) cv2.destroyAllWindows ()
The output is shown in figure 3-7, which connects the five vertices to the left to form a yellow pentagram.
six。 Draw text
In OpenCV, call the cv2.putText () function to add the corresponding text, and the function prototype is as follows:
Img = putText (img, text, org, fontFace, fontScale, color [, thickness [, lineType [, bottomLeftOrigin]])
-img represents the image to be drawn
-text represents the text to be drawn
-org indicates the position to be drawn, the lower left corner of the text string in the image
-fontFace indicates font type. For more information, please see see cv::HersheyFonts.
-fontScale represents the font size and is calculated as a scale factor multiplied by the font-specific base size
-color indicates the color of the font
-thickness indicates the weight of the font
-lineType represents the boundary type
-bottomLeftOrigin if true, the origin of the image data is in the lower left corner, otherwise it is in the upper left corner
Here is the code to draw the text.
#-*-coding:utf-8-*-# By:Eastmountimport cv2import numpy as np# creates a black image img = np.zeros ((256 cv2.imshow), np.uint8) # drawing text font = cv2.FONT_HERSHEY_SIMPLEXcv2.putText (img,'I love Pythony I love Huawei images, (10,100), font, 0.5, (255,255,0), 2) # display image cv2.imshow ("polylines") Img) # wait for cv2.waitKey (0) cv2.destroyAllWindows ()
The output is shown in figure 3-8, with the text drawn as "I love python I love Huawei!".
This is the end of this article on "how to draw all kinds of geometric figures in Python OpenCV". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.