Image Manipulation OpenCV Python

Rate this post

Image manipulation has become an essential aspect of modern-day technology. From social media filters to medical image analysis, image manipulation has become a vital tool for various industries. OpenCV is an open-source computer vision library that enables image processing and manipulation in real-time. Python, on the other hand, is a high-level programming language that is versatile and has gained popularity due to its simplicity and ease of use. In this blog post, we will explore how to manipulate images using OpenCV and Python.

Read and Write Images OpenCV

Before we can start manipulating images, we need to be able to read and write them using Python. OpenCV provides functions to read and write images in various formats, such as JPEG, PNG, and BMP.

To read an image, we can use the cv2.imread() function. This function takes the file path as input and returns a NumPy array representing the image.

import cv2

# Read the image
img = cv2.imread('image.jpg')

# Display the image
cv2.imshow('Image', img)
cv2.waitKey(0)

To write an image, we can use the cv2.imwrite() function. This function takes the file path and the image array as inputs and writes the image to the specified file.

import cv2

# Read the image
img = cv2.imread('image.jpg')

# Flip the image
img = cv2.flip(img, 1)

# Write the flipped image to a file
cv2.imwrite('flipped_image.jpg', img)

Resizing Image OpenCV Python

Resizing images is a common operation in image processing. OpenCV provides a function to resize images, cv2.resize(). This function takes the image array and the new dimensions as inputs and returns the resized image.

import cv2

# Read the image
img = cv2.imread('image.jpg')

# Resize the image
resized_img = cv2.resize(img, (640, 480))

# Display the resized image
cv2.imshow('Resized Image', resized_img)
cv2.waitKey(0)

Rotate Image OpenCV

Rotating images is another common operation in image processing. OpenCV provides a function to rotate images, cv2.rotate(). This function takes the image array and the rotation angle as inputs and returns the rotated image.

import cv2

# Read the image
img = cv2.imread('image.jpg')

# Rotate the image 90 degrees clockwise
rotated_img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)

# Display the rotated image
cv2.imshow('Rotated Image', rotated_img)
cv2.waitKey(0)

Flipping Images

Flipping images is a simple operation in image processing. OpenCV provides a function to flip images, cv2.flip(). This function takes the image array and the flip code as inputs and returns the flipped image.

import cv2

# Read the image
img = cv2.imread('image.jpg')

# Flip the image horizontally
flipped_img = cv2.flip(img, 1)

# Display the flipped image
cv2.imshow('Flipped Image', flipped_img)

Cropping Images

Cropping images is a way to extract a specific part of an image. OpenCV provides a function to crop images, img[y:y+h, x:x+w]. This function takes the image array, the x and y coordinates of the top-left corner, and the width and height of the region to crop as inputs and returns the cropped image.

import cv2

# Read the image
img = cv2.imread('image.jpg')

# Crop a region from the image
cropped_img = img[100:300, 200:400]

# Display the cropped image
cv2.imshow('Cropped Image', cropped_img)
cv2.waitKey(0)

Adding Text to Images

Adding text to images is a common task in computer vision and image processing. OpenCV provides a function to add text to images, cv2.putText(). This function takes the image array, the text to add, the coordinates of the bottom-left corner of the text, the font type, font scale, font color, and thickness as inputs and returns the image with the added text.

import cv2

# Read the image
img = cv2.imread('image.jpg')

# Add text to the image
cv2.putText(img, 'Hello, World!', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

# Display the image with the added text
cv2.imshow('Image with Text', img)
cv2.waitKey(0)

Apply Filters to Images OpenCV Python

Filters are used to modify the appearance of images. OpenCV provides several functions to apply filters to images, such as blurring, sharpening, and edge detection. Let’s take a look at some examples.

Blurring an image using Gaussian Blur

import cv2

# Read the image
img = cv2.imread('image.jpg')

# Apply Gaussian Blur to the image
blurred_img = cv2.GaussianBlur(img, (11, 11), 0)

# Display the blurred image
cv2.imshow('Blurred Image', blurred_img)
cv2.waitKey(0)

Sharpening an image using Unsharp Masking

import cv2
import numpy as np

# Read the image
img = cv2.imread('image.jpg')

# Apply Unsharp Masking to the image
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
sharpened_img = cv2.filter2D(img, -1, kernel)

# Display the sharpened image
cv2.imshow('Sharpened Image', sharpened_img)
cv2.waitKey(0)

Converting Images to Grayscale

Converting images to grayscale is a common preprocessing step in computer vision and image processing. OpenCV provides a function to convert images to grayscale, cv2.cvtColor(). This function takes the image array and the color space conversion code as inputs and returns the converted image.

import cv2

# Read the image
img = cv2.imread('image.jpg')

# Convert the image to grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Display the grayscale image
cv2.imshow('Grayscale Image', gray_img)
cv2.waitKey(0)

References

Leave a Reply

Your email address will not be published. Required fields are marked *