giangbang / Seam-carving

Seam carving Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Seam carving

Python implementation of seam carving.

Libraries

  • OpenCV
  • numpy

Seam carving idea

The idea of seam carving is to remove unnoticeable pixels that blend with their surroundings. => Need an energy function, e.g gradient, to measure importance of pixels.

Given an energy function, the first strategy is to remove an equal number of low energy pixels from every row. However, this will destroy the image content by zig zag effect.

Another possible strategy is to remove whole columns with the lowest energy.

Seam carving is a resizing strategy somewhat in between the two above, it's less restrictive than column removal and preserve content better than pixel removal.

Seam expanding

To enlarge an image, one can insert new seams instead of removing them as in seam carving. Naturally, optimal seams are inserted until the desired size is reached.

Unfortunately, this approach does not work as it creates a stretching artifact by choosing the same seam. Another idea is to find the first k seams for removal, and then duplicate them instead of removing.

Object removal

Given the mask of the object, the algorithm firstly uses seam carving to remove the mask region and expands it to the orginal size. Energy of pixels within mask region are subtracted to attract seams to travel through them.

More examples

Other energy functions:

Code structure

seamCarving.py: all the main code are put in here.

  • findSeamSlow: finding coordinates of one optimal seam. Using dynamic programing approach, traverse all vertices in a DAG in topological order.
  • findSeamFast: Vectorization version of findSeamSlow.
  • seamCarve: remove n optimal seams from image, return removed pixel mask and resulted image.
  • seamExpand: expand n optimal seams from image, values of the inserted seams equal to values of pixels on the seams.
  • seamExpandNaive: expand the optimal seam n times.
  • removeObject: remove object from image, given mask of that image.

About

Seam carving Python


Languages

Language:Python 100.0%