How to resize an image using OpenCV

This recipe helps you resize an image using OpenCV

Recipe Objective: How to resize an image using OpenCV?

Let us take this recipe to understand how to resize an image using OpenCV

Deep Learning Project for Text Detection in Images using Python

Step 1: Import the libraries and read the image

Let us first import the necessary libraries and read the images. The image that we are using here is the one shown below.

OpenCV_Logo.jpg 

import cv2
from matplotlib import pyplot as plt
image = cv2.imread('OpenCV_Logo.jpg')

Step 2: Resizing the Image

We can easily resize the image in two ways using the cv2.resize() function. One way is by mentioning the output dimension directly. Another way is by mentioning a scaling factor. The cv2.resize() function takes the following parameters

  • src: The image which is to be resized
  • dsize: The output dimension of the image
  • fx : (Optional) Scaling factor along the x-axis of the image
  • fy : (Optional) Scaling factor along the y axis of the image
  • interpolation: (Optional) The interpolation method which is to be used

There are various interpolation methods available in OpenCV. Some of them are listed below

  • cv2.INTER_NEAREST: Interpolation by using the nearest neighbor technique
  • cv2.INTER_LINEAR: Bilinear interpolation technique. This is extensively used for zooming. This is the default interpolation technique.
  • cv2.INTER_AREA: Performs resampling using the pixel-area relationship. This is Extensively used to shrink the image
  • cv2.INTER_CUBIC: Bicubic interpolation technique. This is extensively used for zooming
  • cv2.INTER_LANCZOS4: Lanczos interpolation technique.

bigger = cv2.resize(image, None , interpolation=cv2.INTER_CUBIC, fx=2, fy=2,)
smaller = cv2.resize(image, None, interpolation=cv2.INTER_AREA , fx=0.5, fy=0.5)
res = cv2.resize(image, (120,120), interpolation=cv2.INTER_LINEAR)

Step 3: Displaying the results

Let us plot the images using matplotlib subplots for a better comparison of results.

plt.figure(figsize=(13,6))
titles = ['Original Image',"2 times bigger","Size reduced to half","Resized to 120 x 120"]
images = [image, bigger, smaller, res]
plt.figure(figsize=(13,5))
for i in range(4):
    plt.subplot(2,2,i+1)
    plt.imshow(images[i])
    plt.title(titles[i])
plt.tight_layout()
plt.show()

Output:

resized.png

The difference in the sizes can be seen if we observe each image's x and y axes.

Download Materials

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.