How To Pass Text To Drawing In Python
Contents
- 1 Introduction
- 2 Rectangle in OpenCV Python : cv2.rectangle()
- 2.ane Syntax
- 3 Examples of cv2.Rectangle() in Python OpenCV
- 3.ane Import Required Libraries
- 3.2 Function to Create Blank Image
- 3.iii Example – 1 : Draw Simple Rectangle with cv2.rectangle()
- iii.4 Example – 2 : Draw Filled Rectangle with cv2.rectangle()
- 3.5 Example – three : Draw Rectangle on Image with cv2.rectangle()
- 4 Determination
Introduction
In this tutorial, nosotros are going to show you how we can describe rectangles in OpenCV Python by using cv2.rectangle() function. Rectangles are commonly used to create a bounding box for object detection hence yous should know how to create them in OpenCV Python. Nosotros volition prove y'all the syntax of cv2.rectangle() forth with few examples for a better understanding for beginners.
- Also Read – YOLOv4 Object Detection Tutorial with Image and Video : A Beginners Guide
- Too Read –Introduction to YOLOv5 Object Detection with Instance
Rectangle in OpenCV Python : cv2.rectangle()
OpenCV Python has got a rectangle() function that can be used quite easily to create rectangular shapes on an image. Allow us run across its syntax beneath –
Syntax
cv2.rectangle(prototype, pt1, pt2, color, thickness)
- image – It is the prototype on which the rectangle has to be drawn.
- pt1 – Vertex of the rectangle at the elevation left corner.
- pt2 – Vertex of the rectangle at the bottom correct corner.
- color – Information technology is the color of the rectangle line in RGB.
- thickness – It is the thickness of the line.
It does non return anything and draws the rectangle on the original image in place, this means the rectangle drawn on the image will be permanent.
Examples of cv2.Rectangle() in Python OpenCV
Import Required Libraries
Before going through the examples let us start import some of the required libraries equally shown below –
In [1]:
import cv2 import numpy equally np import matplotlib.pyplot as plt %matplotlib inline
Role to Create Bare Image
We will create a utility part that creates an empty image of the size 512×512 and 3 colour channels on which we shall depict rectangles for examples.
In [two]:
def generate_blank_image(): return np.ones(shape=(512,512,3), dtype=np.int16)
Let us see this part in activity by calling it beneath.
In [3]:
img = generate_blank_image() plt.imshow(img)
Out[3]:
<matplotlib.image.AxesImage at 0x1e14d0dc9b0>
Example – one : Describe Simple Rectangle with cv2.rectangle()
In this example, we are going to describe a simple rectangle on the blank image whose line is blood-red in color and line thickness is 10.
Nosotros generate the empty image and pass information technology to cv2.rectangle() along with the 2 vertices coordinates of the rectangle, rgb value of carmine (255,0,0) and thickness = 10.
In [4]:
img1 = generate_blank_image() cv2.rectangle(img1, pt1=(400,200), pt2=(100,50), color=(255,0,0), thickness=10) plt.imshow(img1)
Out[iv]:
<matplotlib.prototype.AxesImage at 0x1e14d3e0ac8>
Example – 2 : Draw Filled Rectangle with cv2.rectangle()
In this example, we are going to depict a rectangle on the blank epitome which is filled with blue color.
To fill the rectangle nosotros use the thickness = -1 in the cv2.rectangle() function.
In [5]:
img2 = generate_blank_image() cv2.rectangle(img2, pt1=(400,300), pt2=(100,100), color=(255,255,0), thickness= -ane) plt.imshow(img2)
Out[5]:
<matplotlib.paradigm.AxesImage at 0x1e14e640ba8>
Example – 3 : Describe Rectangle on Image with cv2.rectangle()
In this instance, nosotros are using a real image of true cat and cartoon the rectangle over it in cherry colour. We have manually calculated the values of pt1 and pt2 and then that the rectangle is drawn properly on the true cat's face.
In [six]:
img3 = cv2.imread('true cat.jpg') cv2.rectangle(img3, pt1=(400,100), pt2=(grand,700), colour=(255,0,0), thickness=10) plt.imshow(img3)
Out[6]:
- Also Read – Quick Guide for Cartoon Circle in OpenCV Python using cv2.circle() with Examples
Determination
Promise you found this short tutorial on how to draw rectangles in OpenCV quite helpful. We covered the syntax of the cv2 rectangle() function forth with different types of examples for a amend understanding of beginners.
Reference – OpenCV documentation
Source: https://machinelearningknowledge.ai/learn-to-draw-rectangle-in-opencv-python-with-cv2-rectangle-with-examples/
Posted by: proctorgoicerouth.blogspot.com
0 Response to "How To Pass Text To Drawing In Python"
Post a Comment