Exploring Top 10 Exciting Functions of OpenCV (cv2)

Aman Keshari
3 min readAug 12, 2023

OpenCV (Open Source Computer Vision Library) is a versatile tool that empowers developers to perform a wide range of image processing tasks. Whether you’re working on computer vision projects or simply looking to manipulate images, OpenCV offers a plethora of functions to help you achieve your goals. In this blog post, we’ll delve into the 10 most exciting functions of OpenCV and provide working code examples for each.

1. Image Loading and Display

The cv2.imread() function allows you to load an image from your file system. After loading the image, you can use cv2.imshow() to display it in a window.

import cv2
image = cv2.imread('image.jpg')
cv2.imshow('Loaded Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. Image Resize

The cv2.resize() function lets you resize an image while maintaining its aspect ratio. This is useful for preparing images of different sizes for further processing.

resized_image = cv2.resize(image, (new_width, new_height))
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. Image Grayscale Conversion

Converting an image to grayscale is a common preprocessing step. The cv2.cvtColor() function facilitates this conversion.

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

4. Image Blurring

Image blurring is useful for noise reduction or creating artistic effects. The cv2.GaussianBlur() function applies a Gaussian blur to the image.

blurred_image = cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

5. Edge Detection

Edge detection is essential for identifying object boundaries. The cv2.Canny() function implements the Canny edge detection algorithm.

edges = cv2.Canny(image, threshold1, threshold2)
cv2.imshow('Edge Detection', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

6. Object Detection with Haarcascades

The cv2.CascadeClassifier() combined with detectMultiScale() can be used to detect objects such as faces in images using pre-trained Haarcascade models.

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray_image, scaleFactor, minNeighbors)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('Face Detection', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

7. Image Rotation

The cv2.getRotationMatrix2D() and cv2.warpAffine() functions allow you to rotate images by a specified angle.

rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)
rotated_image = cv2.warpAffine(image, rotation_matrix, (width, height))
cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

8. Contour Detection

Contours are the boundaries of objects in an image. The cv2.findContours() function identifies these contours, which can be subsequently drawn.

contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
cv2.imshow('Contour Detection', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

9. Image Thresholding

Thresholding converts grayscale images to binary images by setting pixel values above a threshold to a certain value and below to another.

thresholded_image = cv2.threshold(gray_image, threshold_value, max_value, cv2.THRESH_BINARY)
cv2.imshow('Thresholded Image', thresholded_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

10. Hough Line Transform

The Hough Line Transform detects lines within an image. The cv2.HoughLines() function can be used for standard line detection.

lines = cv2.HoughLines(edges, rho, theta, threshold)
for line in lines:
rho, theta = line[0]
# … Compute line endpoints …
cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.imshow('Hough Lines', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Conclusion:
OpenCV’s rich array of functions enables developers and researchers to tackle various image processing challenges with ease. From basic operations like loading and displaying images to advanced tasks like object detection and line recognition, OpenCV provides the tools needed to bring your computer vision projects to life. By mastering these functions, you can unlock new possibilities and create impressive applications in the world of image processing.

--

--