Giter VIP home page Giter VIP logo

image_resize_ml's Introduction

image_resize_ML

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.

    Basic division of code :

    Importing Libraries

    import cv2
    import numpy as np
    import streamlit as st
    

    Define the seam_carve Function

    def seam_carve(img, new_width, new_height):
    return img
    

    Streamlit Web Application

    st.title("Image Resizing using Seam Carving")
    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
    if uploaded_file is not None:
      img = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
      st.image(img, clamp=True, channels="RGB")
      
      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])
    
      resized_img = seam_carve(img, new_width, new_height)
    
      st.image(resized_img, clamp=True, channels="RGB")
    

    we explored the powerful concept of Seam Carving for image resizing, implementing an Image Resizing application using OpenCV and Streamlit. The seam_carve function efficiently calculated the energy map, identified optimal seams, and preserved essential content during the resizing process. The interactive Streamlit web application allowed users to upload images, select desired dimensions, and visualize the resized images in real-time. Seam Carving provides a sophisticated approach to maintain image integrity while achieving precise and visually appealing results, making it a valuable tool for various image processing tasks.

    image_resize_ml's People

    Contributors

    mohit888-r avatar

    Watchers

     avatar

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google โค๏ธ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.