A fast, scalable, and extensive implementation of the Poisson Image Editing paper by Perez et al. 2003.
- Poisson image editing functionalities
- Seamless cloning - importing gradients
- Seamless cloning - mixing gradients
- Seamless tiling
- Texture flattening
- Local illumination change
- Local color change
- Supports all sparse linear system solvers in
scipy.sparse.linalg
- Acceleration of large input problems using multigrid approaches
- An Object-Oriented Programming approach for Poisson image editing - see branch
oop
for more details- All editing functions inherit the abstract
PoissonImageEditor
class
- All editing functions inherit the abstract
There are many open source Python implementations of Poisson image editing. However, most implementations only focus on image blending, while ignoring other Poisson image editing applications listed in the paper. This implementation aims to faithfully reproduce all experiments and results presented in the paper. The following table shows the implemented functionalities:
Src. 1 | Src. 2 | Src. 3 | This | |
---|---|---|---|---|
Seamless cloning | ✅ | ✅ | ✅ | ✅ |
Seamless tiling | ❌ | ❌ | ❌ | ✅ |
Texture flattening | ✅ | ❌ | ❌ | ✅ |
Local illumination change | ❌ | ❌ | ❌ | ✅ |
Local color change | ❌ | ❌ | ❌ | ✅ |
Furthermore, this implementation is signifacantly faster and scales much better than others. The following table shows the profiled run time of seamless cloning on different datasets (in seconds):
Src. 1 | Src. 2 | Src. 3 | This | |
---|---|---|---|---|
test1 | 32.064 | 312.930 | 6.755 | 1.799 |
test2 | 13.634 | 59.875 | 1.775 | 1.389 |
test3 | 36.575 | 565.466 | 3.401 | 1.818 |
test4 | 19.866 | 42.592 | 1.542 | 1.419 |
The following figure shows the scaling performance of this implementation compared to that of Src. 3. For fair comparison, both implementations are modified to use the same solver scipy.sparse.linalg.spsolve
.
By using multigrid solvers, editing a 1080p image can be done in less than 30 seconds, a 6.7x speedup compared to the current fastest open source Python implementation:
Src. 3 | This (multigrid approach) | |
---|---|---|
1080p image | 134.896 | 20.814 |
To run all experiments using the given datasets (contains testing images in the paper and this README file), run
./run_experiments.sh
To test on your own dataset, run
python3 seamless_cloning.py --help
data_dir
: Folder that contains the input image files. The folder should contain image files namedmask
,target
, andsource
. The file extension of the files can be arbitrary, as long as the files are valid image files.grayscale
: Whether to perform blending on the grayscale images.solver
: Linear solver to use when solving the poisson blending problem. The value ofsolver
should either be function names in thescipy.sparse.linalg
library, or"multigrid"
. Default isspsolve
.gradient_mixing_mode
: Method to mix source and target image gradients.max
implements 3. Seamless cloning - Mixing gradients section in the paper, whilealpha
+gradient_mixing_alpha == 1.0
implements 3. Seamless cloning - Importing gradients section.gradient_mixing_alpha
: Alpha to blend source and target image gradients. Has an effect only whengradient_mixing_mode == "alpha"
.
To test on your own dataset, run
python3 seamless_tiling.py --help
To test on your own dataset, run
python3 texture_flattening.py --help
use_edge
: Useedge.*
edge map image file in the folder specified in thedata_dir
folder. If this flag is not set, then computes the edge map from provided source image using Canny edge detector and binary dilation.canny_threshold
: Thresholding parameters for Canny edge detector. You can play with this parameter for different flattening results. See the documentation for more information.edge_dilation_kernel
: Kernel size to dilate detected edges. The kernel is a square box filter filled with ones.
To test on your own dataset, run
python3 local_illumination_change.py --help
data_dir
: Folder that contains the input image files. The folder should contain image files namedmask
andsource
. The file extension of the files can be arbitrary, as long as the files are valid image files. The illumination of the regions specified by the mask will be modified.
To test on your own dataset, run
python3 local_color_change.py --help
mode
: Color change mode. Ifmode == "gray_background"
, then pixels outside the masked region will be converted to grayscale. Ifmode == "color_change"
, the hue of the masked region is increased by the value specified bychange_hue
parameter.change_hue
: Value added to the hue channel of the masked region.
Source | Target | Mask | Result |
---|---|---|---|
Texture | Naive tile | Seamless tile |
---|---|---|
Source | Mask | Edge | Flattened |
---|---|---|---|
Source | Mask | Modified |
---|---|---|
Source | Green-ish | Blue-ish | Gray background |
---|---|---|---|
- Faster solvers:
spsolve
,cgs
,bicg
minres
gives bad results.- If you want to use conjugate gradient solvers, use
bicg
,bicgstab
orcgs
. Do not usesolver == "cg"
since the A matrix is not hermitian (or symmetric since A is real). - Iterative least-squares solvers
lsqr
,lsmr
tend to be much slower.