Drawing and writing on images – OpenCV 3.4 with python 3 Tutorial 3
In this video we are going to learn how to draw and write on images. You can read below a really quick explanation of the code.
Fore more details and a complete explanation of the entire code you can check the video and I will guide you trough the code step by step.
First we import the libraries opencv and numpy, then we load the image.
import cv2 import numpy as np image = cv2.imread("red_panda.jpg") shape = image.shape
We then define the colors. The colors are in BGR format, it means that there are three channels, in order: blue, green and red.
blue = (255, 0, 0) red = (0, 0, 255) green = (0, 255, 0) violet = (180, 0, 180) yellow = (0, 180, 180) white = (255, 255, 255)
We draw on the image in order: a line, a circle, a rectangle, an ellipse and a polygon.
cv2.line(image, (50, 30), (450, 35), blue, thickness=5) cv2.circle(image, (240, 205), 23, red, -1) cv2.rectangle(image, (50, 60), (450, 95), green, -1) cv2.ellipse(image, (250, 150), (80, 20), 5, 0, 360, violet, -1) points = np.array([[[140, 230], [380, 230], [320, 250], [250, 280]]], np.int32) cv2.polylines(image, [points], True, yellow, thickness=3)
We also put some text on the image.
We first define the font with which we want to display the text and then with the function cv2.putText we display it on the screen.
font = cv2.FONT_HERSHEY_COMPLEX cv2.putText(image, "Panda", (20, 180), font, 4, white)
Finally we show the image on the screen.
cv2.imshow("red panda", image) cv2.waitKey(0) cv2.destroyAllWindows()
Hi there, I’m the founder of Pysource.
I help Companies, Freelancers and Students to learn easily and efficiently how to apply visual recognition to their projects.
For Consulting/Contracting Services, check out this page.