Site icon DataFlair

Drawing Polylines, Convex Polylines, Arrowed Lines using OpenCV

drawing polylines covex polylines arrowed line using opencv

Machine Learning courses with 110+ Real-time projects Start Now!!

In the captivating realm of computer vision, the art of visual expression knows no bounds. A universe of imaginative possibilities is accessible thanks to OpenCV, the formidable image processing library.

In this article, we set out on an exciting quest to learn how to use OpenCV to create polylines, convex polylines, and arrowed lines. Join us as we delve into the realm of shapes and lines, discovering how these fundamental tools can add depth, clarity, and impact to your computer vision projects.

Let’s Draw some Complex shapes Using OpenCV.

Polylines consist of multiple line segments that connect a series of points in a continuous manner, creating a path that can be opened or closed. They are commonly used to represent complex shapes, contours, or outlines in computer graphics and image processing.

Regular lines, on the other hand, are simple straight paths that connect two points in space. They are represented by their two endpoints and are used for basic point-to-point connections. Polylines offer greater versatility by connecting multiple points to form more intricate shapes, while regular lines are limited to connecting just two points in a straight line.

Unleashing the Artistry of Polylines with OpenCV’s cv2.polylines()

In the enthralling world of computer vision, one of the essential techniques for shaping visual narratives is the use of polylines. These versatile and expressive constructs enable us to connect a sequence of points, creating intricate paths and outlines that can add structure and meaning to our images.

OpenCV, the eminent library for image processing, empowers us with the cv2.polylines() function, a powerful tool that allows us to effortlessly draw polylines on images. With its intuitive syntax and customizable parameters, cv2.polylines() opens up a world of artistic possibilities.

Let’s take a closer look at the syntax and parameters of cv2.polylines() to understand how to wield this artistic instrument :

Syntax :

cv2.polylines(img, pts, isClosed, color, thickness[, lineType[, shift]])

Parameters : 

Let’s illustrate the power of cv2.polylines() with a simple example. Suppose we have an image `img` and want to draw a blue square with a thickness of 2 pixels:

import cv2
import numpy as np

# Create a blank black image
img = np.zeros((400, 400, 3), dtype=np.uint8)

# Define the coordinates of the square
pts = np.array([[100, 100], [300, 100], [300, 300], [100, 300]], np.int32)

# Reshape the points to be a list of polylines
pts = pts.reshape((-1, 1, 2))

# Draw the blue square on the image
cv2.polylines(img, [pts], isClosed=True, color=(255, 0, 0), thickness=2)

# Display the image
cv2.imshow('Image with Blue Square', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Unleashing the Power of Filled Convex Polylines with OpenCV’s cv2.fillConvexPoly()

Let’s delve into the magic of cv2.fillConvexPoly() with a glimpse into its syntax and essential parameters:

Syntax:

cv2.fillConvexPoly(img, points, color[, lineType[, shift]])

Parameters:

import cv2
import numpy as np

# Create a blank black image
width, height = 400, 400
img = np.zeros((height, width, 3), dtype=np.uint8)

# Define the vertices of the filled polygon
vertices = np.array([[100, 100], [300, 100], [300, 300], [200, 350], [100, 300]], np.int32)

# Reshape the vertices to be a 1x5 array
vertices = vertices.reshape((-1, 1, 2))

# Draw the filled polygon in red color (BGR format)
cv2.fillConvexPoly(img, vertices, color=(0, 0, 255))

# Display the image
cv2.imshow('Filled Polygon', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Let’s check out some cool OpenCV marker types

#include <opencv2/imgproc.hpp>

A possible set of marker types used for the cv::drawMarker function

Enumerator
MARKER_CROSS 

Python: cv.MARKER_CROSS

A crosshair marker shape.
MARKER_TILTED_CROSS 

Python: cv.MARKER_TILTED_CROSS

A 45 degree tilted crosshair marker shape.
MARKER_STAR 

Python: cv.MARKER_STAR

A star marker shape, combination of cross and tilted cross.
MARKER_DIAMOND 

Python: cv.MARKER_DIAMOND

A diamond marker shape.
MARKER_SQUARE 

Python: cv.MARKER_SQUARE

A square marker shape.
MARKER_TRIANGLE_UP 

Python: cv.MARKER_TRIANGLE_UP

An upwards pointing triangle marker shape.
MARKER_TRIANGLE_DOWN 

Python: cv.MARKER_TRIANGLE_DOWN

A downwards pointing triangle marker shape.

Mastering the Art of Drawing Arrowed Lines with OpenCV’s cv2.arrowedLine()

In the captivating realm of computer vision and image processing, the skill of drawing arrowed lines takes center stage, offering a powerful and visually expressive technique.

OpenCV, the esteemed library in this domain, equips us with a remarkable tool – cv2.arrowedLine() – that empowers us to effortlessly create arrows with precision and style.

The cv2.arrowedLine() function is your gateway to drawing arrowed lines on images, providing a clear and dynamic way to highlight directions and relationships.

With its simple syntax and customizable parameters, this function unlocks a world of possibilities, allowing us to craft visually engaging compositions with ease.

Let’s explore the magic of cv2.arrowedLine() with a glimpse into its syntax and essential parameters:

Syntax:

cv2.arrowedLine(img, pt1, pt2, color, thickness[, line_type[, tipLength]])

Parameters:

Let’s embark on an artistic journey with a captivating example of cv2.arrowedLine(). Picture creating a dynamic arrow to indicate direction on a blank canvas:

import cv2
import numpy as np

# Create a blank white image
width, height = 400, 400
img = np.ones((height, width, 3), dtype=np.uint8) * 255

# Define the starting and ending points of the arrow
start_point = (100, 200)
end_point = (300, 200)

# Draw the arrowed line on the image in blue color with a thickness of 2 pixels and a tipLength of 0.3
cv2.arrowedLine(img, start_point, end_point, color=(255, 0, 0), thickness=2, tipLength=0.3)

# Display the image
cv2.imshow('Dynamic Arrow', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Summary

In this captivating journey through computer vision, we’ve explored the artistic wonders of OpenCV’s drawing capabilities. From crafting intricate polylines to elegant convex shapes and guiding the eye with dynamic arrowed lines, we’ve harnessed the power of visual expression.

With OpenCV’s cv2.polylines(), we shaped mesmerizing paths and contours, opening doors to endless possibilities.cv2.fillConvexPoly() enriched our canvas with solid shapes and captivating outlines, adding depth to our creations. Through cv2.arrowedLine(), we directed attention with precision, bringing clarity and focus to our visual narratives.

As you continue your creative adventure, let your imagination soar and paint your visions onto the canvas of computer vision. With OpenCV’s artistic tools at your fingertips, the possibilities are as vast as your imagination.

May your future creations be filled with elegance, flair, and the magic that only visual storytelling can convey. Happy drawing, and may your visual masterpieces inspire and captivate all who behold them!”

Exit mobile version