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’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Freelancers to easily and efficiently build Computer Vision Software.
Learn to build Computer Vision Software easily and efficiently.
This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.Sign UP for FREE
Detect and Track any Object (Full Videocourse)
You can Build Software to detect and track any Object even if you have a basic programming knowledge.