How to use cvrectangle In Python
To draw a rectangle in Opencv Python.
Syntax: cv2.rectangle(image, start_point, end_point, color, thickness)
Parameters:
- image: It is the image on which line is to be drawn.
- start_point: It is the starting coordinates of line. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
- end_point: It is the ending coordinates of line. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
- color: It is the color of line to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color.
- thickness: It is the thickness of the line in px.
Return Value: It returns an image.
Example 1: Without opaque
Python3
import cv2 image = cv2.imread( 'test.jpg' ) overlay = image.copy() # Rectangle parameters x, y, w, h = 10 , 10 , 300 , 300 # A filled rectangle cv2.rectangle(overlay, (x, y), (x + w, y + h), ( 0 , 200 , 0 ), - 1 ) cv2.imshow( "some" , overlay) cv2.waitKey( 0 ) cv2.destroyAllWindows() |
Output:
Example 2: With opaque
For adding opacity we are going to use cv2.addWeighted. This helps us in adding 2 images with different weights of alpha.
Python3
import cv2 image = cv2.imread( 'test.jpg' ) overlay = image.copy() # Rectangle parameters x, y, w, h = 10 , 10 , 300 , 300 # A filled rectangle cv2.rectangle(overlay, (x, y), (x + w, y + h), ( 0 , 200 , 0 ), - 1 ) alpha = 0.4 # Transparency factor. # Following line overlays transparent rectangle # over the image image_new = cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0 ) cv2.imshow( "some" , image_new) cv2.waitKey( 0 ) cv2.destroyAllWindows() |
Output:
How to create a semi transparent shape Python-OpenCV
In this article, we will see how to create a semi-transparent shape python OpenCV.
Sometimes we need transparency in our outputs. It gives our outputs their own unique style. Today, we will see how you can do it easily with OpenCV python. Here, we are going to cover 3 different methods of shape and they are:
- Using Rectangular shape.
- Using Line.
- and, Using Circle.
Note: The concept will be the same for any other shape or image.