In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to draw graphics in Python OpenCV. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Background
Using OpenCV to draw graphics is a necessary skill. In the task of image, whether it is image detection or image recognition, we need to explain the processing results by drawing graphics and drawing words. This article will introduce the drawing of graphics in detail.
First, draw a line
Line (image, start point, end point, color, lineweight, line shape): generally, only the first four parameters are needed.
Code case:
Cv2.line (img, (20,100), (20,500), (0pj0255))
Draw a red line in the Y direction.
Draw a rectangle
Relatively speaking, this is the most widely used method. Often, the result of the target returned in the test project is (xrecedence) or (x1dy1) x2they2). Of course, what we need to draw here is the second form, and it is very simple to calculate the lower right corner through the upper left corner.
Cv2.rectangle (img, (x1), (x2), (0) 0255), 3) # the last two parameters are color and line length
Briefly show me the effect of drawing a rectangle with DeepFashion data:
Draw a circle
Drawing a circle is not much used, it is more about the operation of drawing a point, such as the task of a key point, which needs to display the key point on the target image; in fact, a point is a solid circle. Here we introduce the two forms of drawing a circle and drawing a point.
# function cv2.circle (img, (xPowery), radius length, (0Power0255),-1) / / draw a solid circle (point), and the final parameter is set to negative number cv2.circle (img, (xrecoery y), radius length, (0Power0255), 4) / / draw a hollow circle
Here is an effect of my drawing of key points:
Fourth, draw polygons
Drawing polygons is also very common in practical applications, such as segmentation and OCR tasks, often need to use multiple points to describe the outline of the target.
The function prototype is as follows:
Polylines (img, point set, whether closed loop, color... Note that the point set here must be 32-bit
Case code:
Pot = np.array ([(100,100), (200,200), (300,400)], np.int32) cv2.polylines (img, [pot], True, (0,0,255)) # if you output filled polygons, use the following apicv2.fillPoly (img, [pot], (0,0,255)) V. Draw the text
Drawing text is often used to mark categories in actual projects. for some result output, it can be drawn to the image in the way of text, which is easy to observe and verify.
Function prototype:
PutText (img, string, starting point, font, font size... )
Case code:
Cv2.putText (img, "Hello World", (100,100), font, 3, (0j0255)) VI. Mouse drawing
Implement the function:
Basic graphics can be drawn through the mouse: press l to draw lines, r to draw rectangles, c to draw circles
Import cv2import numpy as npstar = (0,0) select = 0img = np.zeros ((480,640,3), np.uint8) / / defines the mouse callback function def mouse_callback (event, x, y, flags, userdata): global star,select if (event & cv2.EVENT_LBUTTONDOWN = = cv2.EVENT_LBUTTONDOWN): star = (x Y) elif (event & cv2.EVENT_LBUTTONUP = = cv2.EVENT_LBUTTONUP): if select = = 0: cv2.line (img, star, (x, y), (0,0,255)) elif select = = 1: cv2.rectangle (img, star, (x, y), (0,0) Elif select = = 2: a = (x-star [0]) b = (y-star [1]) r = int ((a**2+b**2) * * 0.5) cv2.circle (img, star, r, (0,0,255)) else: print ('no shape') cv2.namedWindow ('drawshape') Cv2.WINDOW_NORMAL) cv2.setMouseCallback ('drawshape', mouse_callback,' 111') while True: cv2.imshow ('drawshape', img) key=cv2.waitKey (1) & 0xFF if key= = ord (' q'): break elif key= = ord ('l'): select = 0 elif key= = ord ('r'): select = 1 elif key= = ord ('c'): select = 2
The following picture is the result of my own random drawing, and you can create more interesting pictures.
On how to draw graphics in Python OpenCV to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can 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.