Mastering the Basics OpenCV: Exploring Image Smoothing, Resizing, and Cropping Techniques

Mastering the Basics OpenCV: Exploring Image Smoothing, Resizing, and Cropping Techniques

Unleash the Power of OpenCV: Master Image Smoothing, Resizing, and Cropping Techniques!

ยท

3 min read

Introduction

In this blog post, we delve into the world of image processing and explore the versatile techniques of image smoothing, resizing, and cropping. Discover how these fundamental techniques can transform your images, improve visual quality, and optimize them for various applications. Learn where and why these techniques are used in fields such as photography, web design, graphic design, computer vision, and more. Whether you're a beginner or a seasoned professional, this comprehensive guide will equip you with the knowledge and skills to enhance your images and unlock their full potential.


Image Smoothing

import cv2
# Load the image
image = cv2.imread("input_image.jpg")
# Apply Gaussian blur for image smoothing
blurred_image = cv2.GaussianBlur(image, (5, 5), 0)
# Display the original and blurred images
cv2.imshow("Original Image", image)
cv2.imshow("Blurred Image", blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the line blurred_image = cv2.GaussianBlur(image, (5, 5), 0), we are applying the Gaussian blur technique to the image using the cv2.GaussianBlur() function.

Here's a breakdown of the function's arguments:

  • image: This is the input image on which we want to apply the Gaussian blur. It should be a NumPy array representing the image.

  • (5, 5): (In simple words it's a neighbourhood pixel average to blurr the pixels) This is kernel size or the size of the blurring filter. In this case, we are using a 5x5 kernel. A larger kernel size will result in a stronger blur effect, while a smaller kernel size will produce a milder blur.

  • 0: This is the standard deviation of the Gaussian distribution in the x-direction. By setting it to 0, we allow OpenCV to automatically calculate the standard deviation based on the kernel size. Alternatively, you can specify a non-zero value if you want to use a specific standard deviation.

The function returns the blurred image, which is then assigned to the variable blurred_image. This variable can be used for further processing or display purposes.

By adjusting the kernel size and the standard deviation, you can control the strength of the blurring effect applied to the image.


Image Resizing

import cv2
# Load the image
image = cv2.imread("input_image.jpg")
# Define the desired width and height for resizing
desired_width = 800
desired_height = 600
# Resize the image using the desired width and height
resized_image = cv2.resize(image, (desired_width, desired_height))
# Display the original and resized images
cv2.imshow("Original Image", image)
cv2.imshow("Resized Image", resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In line resized_image = cv2.resize(image, (desired_width, desired_height)) resizes the image to a new size specified by desired_width and desired_height.


Image Cropping Techniques

import cv2
# Load the image
image = cv2.imread("input_image.jpg")
# Define the coordinates for cropping (x, y, width, height)
x = 100
y = 100
width = 300
height = 200
# Crop the image using the defined coordinates
print(image.shape)
cropped_image = image[y:y+height, x:x+width]
print(cropped_image.shape)

# Display the original and cropped images
cv2.imshow("Original Image", image)
cv2.imshow("Cropped Image", cropped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

[y:y+height, x:x+width]: This part represents array slicing in Python. It specifies the region of interest (ROI) within the image that we want to crop.

  • y:y+height represents the vertical range of the ROI. The y value indicates the starting pixel in the y-axis (top), and y+height represents the ending pixel in the y-axis (bottom) of the ROI.

  • x:x+width represents the horizontal range of the ROI. The x value indicates the starting pixel in the x-axis (left), and x+width represents the ending pixel in the x-axis (right) of the ROI.

To understand this better I used print(image.shape)and print(cropped_image.shape) so that you can see the difference in the resolution between the original image and cropped image


Catch me up on my socials: https://bento.me/harshitpy and I will meet you in the next one ๐Ÿ‘‹

Thank you so much for reading ๐Ÿ’–

You can support me here BuyMeACoffee

Did you find this article valuable?

Support Harshit Jain by becoming a sponsor. Any amount is appreciated!

ย