A fast Python implementation of Seam Carving for Content-Aware Image Resizing (2007), including the improved energy algorithm described in Improved Seam Carving for Video Retargeting (2008).
- OpenCV
- scipy
- numba
- numpy
python seam_carving.py (-resize | -remove) -im <IM_PATH> -out <OUTPUT_IM_NAME>
[-mask <MASK_PATH>] [-rmask <REMOVAL_MASK_PATH>] [-dy <DY>] [-dx <DX>]
[-vis] [-hremove] [-backward_energy]
The program is run via the command-line. There are two modes of operations: resize
or remove
. The former is for resizing an image vertically or horizontally and the latter is for removing an object as specified by a mask.
For both modes:
-im
: The path to the image to be processed.-out
: The name for the output image.-mask
: (Optional) The path to the protective mask. The mask should be binary and have the same size as the input image. White areas represent regions where no seams should be carved (e.g. faces).-vis
: If present, display a window while the algorithm runs showing the seams as they are removed.-backward_energy
: If present, use the backward energy function (i.e. gradient magnitude) instead of the forward energy function (default).
For resizing:
-dy
: Number of horizontal seams to add (if positive) or subtract (if negative). Default is 0.-dx
: Number of vertical seams to add (if positive) or subtract (if negative). Default is 0.
For object removal:
-rmask
: The path to the removal mask. The mask should be binary and have the same size as the input image. White areas represent regions to be removed.-hremove
: If present, perform seam removal horizontally rather than vertically. This will be more appropriate in certain contexts.
There are some additional constants defined at the top of the code seam_carving.py
that may be modified.
- The code currently downsizes any images with width larger than 500 pixels to 500 pixels for super fast carving. To change this, change the value of
DOWNSIZE_WIDTH
, or setSHOULD_DOWNSIZE
toFalse
to disable downsizing completely. - Seams are visualized as a bluish-white color; change the color by changing the
SEAM_COLOR
array (in BGR format).
The input image is on the left and the result of the algorithm is on the right.
Animated gif:
In general, forward energy gives better results than backward energy. The result of resizing using backward energy (left) and forward energy (right) is shown below.
For more information on how the algorithm works, see my blog post.
Many parts of the code are adapted/optimized versions of functionality from other implementations: