I N F O A R Y A N

OPENCV-PYTHON | Lines, Shapes, Polygons, Text | Aliasing in OpenCV | Figure and text in opencv

In the world of computer vision, OpenCV stands as a powerful library that provides a plethora of tools for image and video processing. OpenCV, a versatile computer vision library, isn’t just for mundane image processing tasks. It’s a canvas for unleashing creativity and exploring the world of figure formations. In this blog post, we’ll dive into the realm of artistic expression using OpenCV, creating captivating figures with just a few lines of code.

Flow of The Article:

  1. Developing Different Figure using OpenCV Python.
  2. Developing Polygon Figures using OpenCV.
  3. Understanding the text functions in OpenCV with Python Code.
  4. Conclusion

 

The Code for all written below can be found here.

 

The Canvas

Let’s start by creating a canvas—a blank slate for our artistic endeavors. A 500×500 pixel canvas with three color channels sets the stage for our figure formations.

import cv2
import numpy as np

# Creating a canvas 500×500 (Three channels)
canvas = np.zeros((500,500,3))

Figure 1: The Dynamic Line

Our first creation is a dynamic line, zigzagging across the canvas. We employ the cv2.line function, drawing a green line with a thickness of 2 pixels.

# Drawing a line
cv2.line(canvas, (0,0), (100,100), (0,255,0), 2, cv2.LINE_4)

Figure 2: The Solid Rectangle

Moving on, let’s fill a rectangular region with a vibrant red hue. The cv2.rectangle function comes into play, creating an eye-catching filled rectangle.

# Drawing a rectangle
cv2.rectangle(canvas, (200,200), (250,270), (0,0,255), -1)

Figure 3: The Circular Elegance

Circles add a touch of elegance to our canvas. With the cv2.circle function, we draw a blue circle with a radius of 10 pixels and a thickness of 3 pixels.

# Drawing a circle
cv2.circle(canvas, (250,250), 10, (255,0,0), 3)

Figure 4: The Arrowed Path

Finally, we venture into the realm of arrowed lines, adding a dynamic element to our creation. The cv2.arrowedLine function allows us to draw an arrowed line from one point to another.

# Drawing an arrowed line
cv2.arrowedLine(canvas, (400,400), (400,500), (255,255,255), tipLength=0.3)

Figure 5: Drawing a Colorful Polygon on a Black Canvas with OpenCV

This concise code snippet utilizes OpenCV and NumPy to create a visually striking polygon on a black canvas. The canvas, with three layers, serves as the backdrop for the polygon formed by connecting specified points. The cv2.polylines function is employed to draw the polygon with a green color and a thickness of 3 pixels. The resulting masterpiece is then displayed using cv2.imshow.

#Import the required libraries
import cv2
import numpy as np

#Making a empty BLACK canvas
#Here canvas is of three layers
canvas = np.zeros((300,300,3))

#Required points we need to join
pts = np.array([[250, 5], [220, 80], [280, 80],[100,100],[250,250]], np.int32)

# Reshape the points to shape (number_vertex, 1, 2)
pts = pts.reshape((-1, 1, 2))

# Draw this polygon
#Here Boolean True and Flase determine if the figure is closed
cv2.polylines(canvas, [pts], True,(0,255,0), 3)

#Showing the canvas
cv2.imshow(‘winname’, canvas)
cv2.waitKey(20000)

Exploring Font Styles

In this code snippet, you are exploring the diverse font styles offered by OpenCV to add a creative touch to textual elements in computer vision projects. Utilizing an 800×500 canvas, you showcase various fonts by rendering the text “THIS IS OPENCV!” in different styles and positions. The code not only serves as a practical guide to implementing fonts in OpenCV but also encourages experimentation with the aesthetics of text, allowing developers and enthusiasts to infuse their projects with visual flair.

#Import teh basic libraries
import cv2
import numpy as np
 
 
#Creating a canvas of 800X500 (three Channels)
canvas = np.zeros((800,500))
 
 
#List of all fonts 
fonts = [cv2.FONT_HERSHEY_COMPLEX,
cv2.FONT_HERSHEY_DUPLEX, 
cv2.FONT_HERSHEY_PLAIN, 
cv2.FONT_HERSHEY_SIMPLEX, 
cv2.FONT_HERSHEY_TRIPLEX,
cv2.FONT_HERSHEY_COMPLEX_SMALL,
cv2.FONT_HERSHEY_COMPLEX,
cv2.FONT_HERSHEY_SCRIPT_COMPLEX,
cv2.FONT_HERSHEY_SCRIPT_SIMPLEX]
 
position = (10, 30)
for i in range(0, 8):
cv2.putText(canvas,”THIS IS OPENCV !”, position, fonts[i], 1, (255,255,255), 2, cv2.LINE_AA)
position = (position[0], position[1] + 30)
cv2.putText(canvas,”THIS IS OPENCV!”.lower(), position, fonts[i],  1, (255,255,255), 2, cv2.LINE_AA)
position = (position[0], position[1] + 30)
 
#Displaying the canvas
cv2.imshow(‘winname’, canvas)
cv2.waitKey(20000)

Conclusion

To sum up, the journey into figure formations using OpenCV showcases the library’s versatility beyond traditional computer vision tasks. The provided code elegantly crafts diverse shapes on a canvas, emphasizing the artistic potential of OpenCV. Understanding key algorithms like Bresenham’s and incorporating features like resizing enriches the learning experience. Embrace this blend of art and technology to infuse creativity into your computer vision projects.

OpenCV Creative Coding, Computer Vision Art, Visual Arts with Code, OpenCV Algorithms, OpenCV Python, Python OpenCV, Aryan Verma, Infoaryan, Infoaryan.com, Computer Vision Projects, OpenCV projects, OpenCV Python Projects, Image Processing Projects, Image Processing with openCV

OpenCV Text Rendering, Font Styles, Computer Vision Creativity, Visual Design in OpenCV.