Skip to main content

This module provides a simple yet powerful mechanism to resize images using Seam Carving Algorithm.

Project description

pyCAIR Logo

pyCAIR is a content-aware image resizing(CAIR) library based on Seam Carving for Content-Aware Image Resizing paper.

Table of Contents

  1. How CAIR works
  2. Understanding the research paper
  3. Project structure and explanation
  4. Installation
  5. Usage
  6. Demo
  7. Screenshots
  8. Todo

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.

Result7 Result8
DP Matrix Backtracking with minimum energy

Intutive explanation of research paper

Notes1

Notes2

Notes3

Notes4

Project structure and explanation

Directory structure:

pyCAIR (root directory)
  | - images/
  | - results /
  | - sequences/ (zipped in repository)
  | - videos/
  | - notdoneyet.py
  | - imgtovideos.py
  | - opencv_generators.py
  | - seam_carve.py
  | - helpers.py

File: notdoneyet.py

  • 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.

File: imgtovideos.py

  • generateVideo() - pass each image path to vid() for video generation.

  • **vid() **- writes each input image to video buffer for creating a complete video.

File: opencv_generators.py

  • 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.

File: seam_carve.py

  • 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.

File: helpers.py

  • writeImage() - stores the images in results directory.

  • writeImageG() - stores intermediate generated sequence of images in sequences directory.

  • createFolder() - self explanatory

  • getFileExtension() - self explanatory

Other folders:

  • 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

'''
It runs the entire code and returns final results
'''
from pyCAIR import user_input
user_input(alignment, scale, seam, input_image, generate_sequences)

'''
It generates the energy map
'''
from pyCAIR import generateEnergyMap
generateEnergyMap(image_name, file_extension, file_name)

'''
It generates color maps
'''
from pyCAIR import generateColorMap
generateColorMap(image_name, file_extension, file_name)

'''
It converts sequence of images generated to video
'''
from pyCAIR import generateVideo
generateVideo()

'''
It returns all the paths where images are present for generating video
'''
from pyCAIR import getToProcessPaths
getToProcessPaths()

'''
It returns seams, cropped image for an image
'''
from pyCAIR import cropByColumn
seam_img, crop_img = cropByColumn(image, display_seams, generate, lsit, scale_c, fromRow)

'''
It returns seams, cropped image for an image
'''
from pyCAIR import cropByRow
seam_img, crop_img = cropByRow(image, display_seams, generate, lsit, scale_c)

'''
It returns created folder
'''
from pyCAIR import createFolder
f = createFolder(folder_name)

'''
It returns extension of file
'''
from pyCAIR import getFileExtension
f = getFileExtension(file_name)

'''
It writes image to specified folder
'''
from pyCAIR import writeImage
f = writeImage(image, args)

In Action

Video1

Video2

Screenshots

Results for Image 1:

Result0 Result1 Result2
Original Image Grayscale Energy Map
Result3 Result4
Color Map Winter Color Map Hot
Result5 Result6
Seams for Columns Columns Cropped
Result7 Result8
Seams for Rows Rows Cropped

Results for Image 2:

Result0 Result1 Result2
Original Image Grayscale Energy Map
Result3 Result4
Color Map Winter Color Map Hot
Result5 Result6
Seams for Columns Columns Cropped
Result7 Result8
Seams for Rows Rows Cropped

Todo

  • Implement Seam Algorithm
  • Generate energy maps and color maps for image
  • Display Vertical Seams
  • Display Horizontal Seams
  • Crop Columns
  • Crop Rows
  • Use argparse for Command Line Application
  • Store subsamples in different directories for crop and seam respectively
  • Generate video/gif from sub-samples
  • Provide a better Readme
  • Provide examples for usage
  • Generate unittests for each functions
  • Add Continous Integration Services(Travis, Coveralls)
  • Add badges
  • Provide better project description on PyPI
  • Documentation using Spinx
  • Integrate object detection using YOLOv2
  • Identify most important object (using probability of predicted object)
  • Invert energy values of most important object
  • Re-apply Seam Carve and compare results

License

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyCAIR-0.1.13.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

pyCAIR-0.1.13-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file pyCAIR-0.1.13.tar.gz.

File metadata

  • Download URL: pyCAIR-0.1.13.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyCAIR-0.1.13.tar.gz
Algorithm Hash digest
SHA256 91eca5a51fc0933b16ccf5f6524ccf8feeaa15c3e706acd3ee838985b6eac221
MD5 f34d9db202b83f9e41a8821ebe27c604
BLAKE2b-256 e85603a8b036b3f70107b3555fa4d240cdc9b595579e1793507a963633fe32b3

See more details on using hashes here.

File details

Details for the file pyCAIR-0.1.13-py3-none-any.whl.

File metadata

File hashes

Hashes for pyCAIR-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 02ef7439625d5395cf7e9eb40c028b82157985637dbb5dad39ca0fc1a5a38d41
MD5 629c659aadbf9206edbe539157caecdc
BLAKE2b-256 a5a9da1b46aa9e436099875afa542e93edb5edf47e6ac1dd9dcfb8429ae4563e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page