Python OpenCV Drawing Geometric Shapes

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

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

Python OpenCV Drawing Geometric Shapes
Python OpenCV Drawing Geometric Shapes

 

 

 

 

 

 

This is another example for Python OpenCV Drawing Geometric Shapes

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

Python OpenCV Drawing Geometric Shapes
Python OpenCV Drawing Geometric Shapes

 

 

 

 

Also you can check Python GUI Development Tutorials in the below link.

 

 

 

 

Also you can watch the complete video for this article

 

 

 

Leave a Comment

Share via
Copy link
Powered by Social Snap