Image Resizing using Seam Carving with OpenCV and Streamlit

Mohit Singh
4 min readJul 18, 2023

--

You Can Check out Github Code

GitHub Code : https://github.com/Mohit888-R/image_resize_ML/tree/main

Introduction

Image resizing is a fundamental task in computer vision and image processing. Traditional resizing techniques, such as scaling, can lead to distortion and loss of important image content. Seam carving is an advanced technique that intelligently removes or adds seams (narrow strips of pixels) from the image to resize it while preserving essential content and aspect ratios. In this tutorial, we will implement an image resizing application using seam carving in Python, leveraging the power of OpenCV and Streamlit.

Requirements

Before we proceed, ensure you have the following libraries installed:

  • OpenCV: For image processing tasks
  • NumPy: For array manipulations
  • Streamlit: For building interactive web applications

You can install them using pip:

pip install opencv-python numpy streamlit

The Seam Carving Algorithm

The seam carving algorithm consists of the following steps:

  1. Energy Map Calculation: Compute the energy map of the input image. The energy map represents the importance of each pixel in the image, where high energy values indicate significant content.
  2. Finding Optimal Seams: Identify the seams with the lowest energy along which the image will be resized. These seams pass through low-energy regions and preserve important features.
  3. Seam Removal/Addition: Remove or add the identified seams from/to the image, resizing it accordingly.

Implementation Steps

  1. Import the necessary libraries: OpenCV, NumPy, and Streamlit.
  2. Define the seam_carve function that takes an image and new width and height as inputs and resizes the image using the seam carving algorithm.
  3. Read and display the input image using Streamlit’s file_uploader and st.image functions.
  4. Use Streamlit’s slider widget to allow the user to select the desired width and height for the resized image.
  5. Apply the seam_carve function on the uploaded image with the selected dimensions and display the resized image using st.image.

Full Code Implementation

# Import the necessary libraries
import cv2
import numpy as np
import streamlit as st

# Define the seam_carve function
def seam_carve(img, new_width, new_height):
img = img.astype(np.float64)
for i in range(int(img.shape[1] - new_width)):
energy_map = cv2.cvtColor(img.astype(np.uint8), cv2.COLOR_BGR2GRAY)
energy_map = cv2.Sobel(energy_map, cv2.CV_64F, 1, 0) ** 2 + \
cv2.Sobel(energy_map, cv2.CV_64F, 0, 1) ** 2
min_energy_map = np.zeros_like(energy_map)
min_energy_map[0] = energy_map[0]
for row in range(1, energy_map.shape[0]):
for col in range(energy_map.shape[1]):
if col == 0:
min_energy_map[row, col] = energy_map[row, col] + \
min(min_energy_map[row - 1, col],
min_energy_map[row - 1, col + 1])
elif col == energy_map.shape[1] - 1:
min_energy_map[row, col] = energy_map[row, col] + min(
min_energy_map[row - 1, col - 1],
min_energy_map[row - 1, col])
else:
min_energy_map[row, col] = energy_map[row, col] + min(
min_energy_map[row - 1, col - 1],
min_energy_map[row - 1, col],
min_energy_map[row - 1, col + 1])
seam_mask = np.ones_like(img[:, :, 0])
col = np.argmin(min_energy_map[-1])
for row in reversed(range(img.shape[0])):
seam_mask[row, col] = 0
if col == 0:
col = np.argmin(min_energy_map[row - 1, col:col + 2])
elif col == img.shape[1] - 1:
col = np.argmin(
min_energy_map[row - 1, col - 1:col + 1]) + col - 1
else:
col = np.argmin(
min_energy_map[row - 1, col - 1:col + 2]) + col - 1
img = cv2.resize(img, (new_width, new_height))
seam_mask = cv2.resize(seam_mask, (new_width, new_height))
for channel in range(img.shape[2]):
img[:, :, channel] = np.multiply(img[:, :, channel], seam_mask)
img = img.astype(np.uint8)
return img

# Streamlit Web Application
st.title("Image Resizing using Seam Carving")

# Upload an image
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])

# If image is uploaded
if uploaded_file is not None:
img = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
st.image(img, clamp=True, channels="RGB")

# Select desired width and height using sliders
new_width = st.slider('Pick a width', 100, img.shape[1], img.shape[1])
new_height = st.slider('Pick a height', 0, img.shape[0], img.shape[0])

# Apply seam_carve function on the image with selected dimensions
resized_img = seam_carve(img, new_width, new_height)

# Display the resized image
st.image(resized_img, clamp=True, channels="RGB")

In this tutorial, we implemented an image resizing application using the seam carving technique with OpenCV and Streamlit. Seam carving allows us to resize images while preserving important content, resulting in visually pleasing resized images. The interactive web application allows users to upload their images and resize them with ease, demonstrating the power and flexibility of seam carving in image processing. Experiment with different images and resizing dimensions to observe the effectiveness of this technique in preserving crucial image details.

The code provided implements an Image Resizing application using Seam Carving with OpenCV and Streamlit. Seam carving is an advanced technique that intelligently removes or adds seams (narrow strips of pixels) from the image to resize it while preserving essential content and aspect ratios.

The seam_carve function takes an image and new width and height as inputs and resizes the image using the seam carving algorithm. It calculates the energy map of the image, identifies optimal seams with low energy, and removes or adds these seams to resize the image.

The Streamlit web application allows users to upload an image, view the original image, and select the desired width and height using sliders. Upon selecting the dimensions, the seam_carve function is applied to the uploaded image, and the resized image is displayed on the web application.

Seam carving offers a powerful alternative to traditional image scaling, preserving important features in the resized image. The interactive nature of the web application allows users to experiment with different images and resizing dimensions, demonstrating the flexibility and effectiveness of the seam carving technique.

By following the step-by-step guide and running the provided code, users can experience the benefits of seam carving and its impact on image resizing, making it an essential tool for various image processing tasks.

Greetings,

Don’t forget to follow along as we dive into the fascinating world of Image Resizing using Seam Carving! This innovative technique will open up new possibilities for image processing and resizing, preserving essential content with remarkable precision. Stay tuned and join us on this exciting journey! Happy learning and resizing!

Best regards,
Mohit Singh

--

--

Mohit Singh
Mohit Singh

Written by Mohit Singh

Full Stack Software Developer | Notion Consultant | Productivity Learner

No responses yet