In this Python OpenCV article i want to show you Drawing Geometric Shapes in Python OpenCV, basically we are going to Draw line, rectangle, circle and text in this post. so OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. It is designed to help developers create real time computer vision applications. OpenCV provides different functions and tools to process and manipulate images and videos, perform object detection and recognition, track moving objects, extract features from images and many more. it is written in C++ and has bindings for many other programming languages, including Python, Java and MATLAB. OpenCV is widely used in academic research, robotics, autonomous vehicles and different industries including healthcare, security and entertainment.
This is the complete source code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import cv2 import numpy as np import cv2 # Drawing Shapes def ImageProcessing(): image = np.zeros((512, 512, 3), np.uint8) cv2.line(image, (20,200), (200,20), (0,0,255),5) cv2.rectangle(image, (200,60), (20,200), (255,0,0), 3) cv2.circle(image, (80,80), 50, (0,255,0), 4) mytext = "Hello World" cv2.putText(image, mytext, (100,300), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255) ) cv2.imshow('Black Image', image) cv2.waitKey(0) cv2.destroyAllWindows() ImageProcessing() |
in the above code first we have imported cv2 and numpy , after that we have created ImageProcessing() method and in that method we have created an empty image using numpy, now that we have created an empty image it is time to create our first line in our empty image, you can use cv2.line for Drawing line in OpenCV. we need to give some parameters, the first and the second one is the start and ending point for x and y positions, the third one is the color we are using BGR color, for this line we have given red color line. the same process can be done for all shapes like rectangle and circle also text.
Run your code and this will be the output
This is another example for Python OpenCV Drawing Geometric Shapes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) # Draw a line cv2.line(img,(0,0),(511,511),(255,0,0),5) # Draw a rectangle cv2.rectangle(img,(384,0),(510,128),(0,255,0),3) # Draw a circle cv2.circle(img,(447,63), 63, (0,0,255), -1) # Draw an ellipse cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1) # Draw a polygon pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32) pts = pts.reshape((-1,1,2)) cv2.polylines(img,[pts],True,(0,255,255)) # Display the image cv2.imshow("Shapes",img) cv2.waitKey(0) cv2.destroyAllWindows() |
np.zeros((512,512,3), np.uint8) line creates black image with width and height of 512 pixels and three channels (BGR).
cv2.line function draws line from the point (0,0) to (511,511) with blue color and thickness of 5 pixels.
cv2.rectangle function draws rectangle with green color and thickness of 3 pixels.
cv2.circle function draws red circle with center at (447,63) and radius of 63 pixels.
cv2.ellipse function draws an ellipse with center at (256,256), major axis length of 100 pixels, minor axis length of 50 pixels, and an angle of 0 degrees.
cv2.polylines function draws polygon with yellow color.
and finally cv2.imshow function displays the image in window named “Shapes” and cv2.waitKey waits until a key is pressed and cv2.destroyAllWindows closes all windows.
This is just basic example of what can be achieved with OpenCV’s drawing functions. you can combine these functions to draw more complex shapes or use them in conjunction with other OpenCV functions to achieve different image processing tasks.
If you run the code you will see this output
Also you can check Python GUI Development Tutorials in the below link.
- PyQt5 GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development Tutorials
- Kivy GUI Development Tutorials
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email