opencv
  1. opencv-drawing-functions

Drawing Functions - ( Drawing and Image Processing in OpenCV )

Heading h2

Syntax

The cv2 module in OpenCV provides various functions for drawing on images:

cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]])
cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]])
cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

Example

Here's an example of drawing a line on an image:

import cv2

img = cv2.imread('image.jpg')
cv2.line(img, (0, 0), (200, 200), (255, 0, 0), 5)

cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

The output is an image with a blue line drawn from (0, 0) to (200, 200).

Explanation

The above example uses the cv2.line() function to draw a line on the input image. The arguments to the function are:

  • img: the input image on which the line is to be drawn
  • (0, 0) and (200, 200): the starting and ending points of the line, respectively
  • (255, 0, 0): the color of the line, which is blue in this case
  • 5: the thickness of the line, in pixels

Similarly, the other drawing functions in OpenCV can be used to draw circles, rectangles, ellipses, and text on images.

Use

Drawing functions in OpenCV are useful in various image processing applications like computer vision, object detection, and image recognition. They can be used to draw regions of interest, bounding boxes, landmarks, and text on images.

Important Points

  • OpenCV provides various drawing functions like cv2.line(), cv2.circle(), cv2.rectangle(), cv2.ellipse(), and cv2.putText()
  • These functions take the input image, the coordinates of the shape/line/point/text to be drawn, and other arguments like color, thickness, font, etc.
  • Drawing functions are useful in various image processing applications like object detection, image recognition, and computer vision
  • Drawing on images can also be useful in debugging and visualizing intermediate results in deep learning and machine learning models.

Summary

Drawing functions in OpenCV are essential for different applications in image processing and computer vision. They make image processing easier and efficient by allowing the developer to manipulate and annotate images. The OpenCV library permits drawing circles, lines, rectangles, and ellipses on images easily. Through this method, OpenCV simplifies the process of visualizing intermediate results while debugging deep learning models and machine learning models.

Published on: