Python implementation of seam carving.
- OpenCV
- numpy
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.
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.
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.
Other energy functions:
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 offindSeamSlow
.seamCarve
: removen
optimal seams from image, return removed pixel mask and resulted image.seamExpand
: expandn
optimal seams from image, values of the inserted seams equal to values of pixels on the seams.seamExpandNaive
: expand the optimal seamn
times.removeObject
: remove object from image, given mask of that image.