Smart Image Resizing in Python

How does it work

  • An energy map and a grayscale format of image is generated from the provided image.
  • Seam Carving algorithm tries to find the not so useful regions in image by picking up the lowest energy values from energy map.
  • With the help of Dynamic Programming coupled with backtracking, seam carving algorithm generates individual seams over the image using top-down approach or left-right approach.(depending on vertical or horizontal resizing)
  • By traversing the image matrix row-wise, the cumulative minimum energy is computed for all possible connected seams for each entry. The minimum energy level is calculated by summing up the current pixel with the lowest value of the neighboring pixels from the previous row.
  • Find the lowest cost seam from the energy matrix starting from the last row and remove it.
  • Repeat the process iteratively until the image is resized depending on user specified ratio.
DP Matrix
Backtracking with minimum energy

Intutive explanation of research pap

Project structure and explanation

Directory structure:

  • user_input() -
    Parameters:
  • Alignment: Specify on which axis the resizing operation has to be performed.
  • Scale Ratio: Floating point operation between 0 and 1 to scale the output image.
  • Display Seam: If this option isn’t selected, the image is only seamed in background.
  • Input Image
  • Generate Sequences: Generate intermediate sequences to form a video after all the operations are performed.
  • generateVideo() — pass each image path to vid() for video generation.
  • vid() — writes each input image to video buffer for creating a complete video.
  • generateEnergyMap() — utilised OpenCV inbuilt functions for obtaining energies and converting image to grayscale.
  • generateColorMap() — utilised OpenCV inbuilt functions to superimpose heatmaps on the given image.
  • getEnergy() — generated energy map using sobel operators and convolve function.
  • getMaps() — implemented the function to get seams using Dynamic Programming. Also, stored results of minimum seam in seperate list for backtracking.
  • drawSeam() — Plot seams(vertical and horizontal) using red color on image.
  • carve() — reshape and crop image.
  • cropByColumn() — Implements cropping on both axes, i.e. vertical and horizontal.
  • cropByRow() — Rotate image to ignore repeated computations and provide the rotated image as an input to cropByColumn function.
  • writeImage() — stores the images in results directory.
  • writeImageG() — stores intermediate generated sequence of images in sequences directory.
  • createFolder() — self explanatory
  • getFileExtension() — self explanatory
  • images/ — stores the input images for testing.
  • videos/ — stores the videos generated from the intermediate sequences.
  • results/ — stores the final results.
  • sequences/ — stores the intermediate sequences generated.

Installation

Usage

https://gist.github.com/647087ceadf955741074df83aed73390

In Action

Screenshots

Results for Image 1:

Results for Image 2:

License

This software is licensed under the GNU General Public License v3.0 © Chirag Shah

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store