Ocean
PoissonBlending.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_CV_ADVANCED_POISSON_BLENDING_H
9 #define META_OCEAN_CV_ADVANCED_POISSON_BLENDING_H
10 
12 
13 #include "ocean/base/Frame.h"
14 #include "ocean/base/Worker.h"
15 
17 
19 
20 namespace Ocean
21 {
22 
23 namespace CV
24 {
25 
26 namespace Advanced
27 {
28 
29 /**
30  * Provides image blending based on Poisson Blending, a seamless image composition algorithm from Perez, et al. ("Poisson Image Editing", 2003).
31  * This class implements seamless cloning of a region of a source image into a target image.<br>
32  * This is performed by minimizing the difference between the Laplacians of source and result image region while using the region border of target pixels as boundary condition.
33  * @ingroup cvadvanced
34  */
35 class OCEAN_CV_ADVANCED_EXPORT PoissonBlending
36 {
37  public:
38 
39  /**
40  * Performs Poisson Blending of the specified source frame into the specified target frame.
41  * Only source pixels defined by the specified source mask are inserted into the target frame.<br>
42  * The source frame in must not overlap with the border of the target frame for the specified insert position.<br>
43  * Source frame and source mask frame should have the same dimensions.<br>
44  * Only pixels with the specified mask value are considered to be inside of the source mask.
45  * @param source The source frame, pixel format must be 8 bit per color channel, must be valid
46  * @param sourceMask Mask of source frame, pixel format must be Y8, must be valid
47  * @param target The target frame into which the source frame is blended, the pixel format must be identical to the source frame, must be valid
48  * @param left Specifies the horizontal position of the left border of the inserted source frame in pixel, with range (-infinity, infinity)
49  * @param top Specifies the vertical position of the top border of the inserted source frame in pixel, with range (-infinity, infinity)
50  * @param maskValue The value of mask pixels to be considered inside of the source content that will be blended
51  * @param worker Optional worker object to distribute the computational load
52  */
53  static void poissonBlending(const Frame& source, const Frame& sourceMask, Frame& target, const int left, const int top, const uint8_t maskValue = 0xFFu, Worker* worker = nullptr);
54 
55  protected:
56 
57  /**
58  * Performs separate Poisson blending calculations for a range of color channels.
59  * @param indexLookup Index lookup frame for masked source pixels
60  * @param source The source frame, pixel format must be 8 bit per color channel
61  * @param target The target frame into which the source frame is blended, Pixel format must be identical to the source frame
62  * @param matrixA Sparse matrix representing the relations between neighbored source mask pixels
63  * @param sourceBoundingBox The source bounding box, must be valid
64  * @param targetLeft Left border of insert region in the target frame
65  * @param targetTop Top border of insert region in the target frame
66  * @param firstChannel Index of the first color channel to be handled, with range [0, target.channels() - channelCount]
67  * @param channelCount Number of color channels to be handled, with range [1, target.channels()]
68  */
69  static void poissonBlendingSubset(const Frame* indexLookup, const Frame* source, Frame* target, const SparseMatrixF* matrixA, const CV::PixelBoundingBox* sourceBoundingBox, const unsigned int targetLeft, const unsigned int targetTop, const unsigned int firstChannel, const unsigned int channelCount);
70 
71  /**
72  * Creates an index lookup frame for masked source pixels.
73  * Pixels are considered to be part of the mask if they have the specified mask value.
74  * @param sourceMask Frame mask for which an index lookup is created, pixel format should be 8bit grayscale
75  * @param sourceBoundingBox The source bounding box, must be valid
76  * @param maskValue value of pixels to be considered inside of the source mask
77  * @param indexLookup Receives the created index lookup frame
78  * @return The number of pixels within the mask
79  */
80  static size_t createIndexLookup(const Frame& sourceMask, const CV::PixelBoundingBox& sourceBoundingBox, const uint8_t maskValue, Frame& indexLookup);
81 
82  /**
83  * Creates a sparse matrix representing the relations between neighbored source mask pixels.
84  * @param indexLookup Index lookup frame
85  * @param sourceBoundingBox The source bounding box, must be valid
86  * @param targetLeft Left border of insert region in the target frame, with range [0, targetWidth)
87  * @param targetTop Top border of insert region in the target frame, with range [0, targetHeight)
88  * @param targetWidth Width of target frame in pixels
89  * @param targetHeight Height of target frame in pixels
90  * @param matrixA Receives the sparse matrix representing the relations between neighbored source mask pixels
91  */
92  static void createMaskNeighborRelationsMatrix(const Frame& indexLookup, const CV::PixelBoundingBox& sourceBoundingBox, const unsigned int targetLeft, const unsigned int targetTop, const unsigned int targetWidth, const unsigned int targetHeight, SparseMatrixF& matrixA);
93 
94  /**
95  * Creates the vector representing the sum of border pixels and source frame laplacian for each pixel within the source mask.
96  * @param indexLookup Index lookup frame for masked source pixels
97  * @param source The source frame, pixel format must be 8 bit per color channel
98  * @param target The target frame into which the source frame is blended, Pixel format must be identical to the source frame
99  * @param sourceBoundingBox The source bounding box, must be valid
100  * @param targetLeft Left border of insert region in the target frame
101  * @param targetTop Top border of insert region in the target frame
102  * @param channelIndex The index of the color channel, with range [0, target.channels()]
103  * @param vectorB Receives the vector representing the sum of border pixels and source frame laplacian for each pixel within the source mask.
104  */
105  static void createSummedBorderLaplacianVector(const Frame& indexLookup, const Frame& source, const Frame& target, const CV::PixelBoundingBox& sourceBoundingBox, const unsigned int targetLeft, const unsigned int targetTop, const unsigned int channelIndex, MatrixF& vectorB);
106 
107  /**
108  * Inserts the solved matrix into the output frame for the specified channel number.
109  * @param indexLookup Index lookup frame for masked source pixels
110  * @param vectorX Result vector containing the calculated Poisson blended values
111  * @param sourceBoundingBox The source bounding box, must be valid
112  * @param targetLeft Left border of insert region in the target frame
113  * @param targetTop Top border of insert region in the target frame
114  * @param channelIndex The index of the color channel, with range [0, target.channels()]
115  * @param target The target frame into which the source frame is blended, Pixel format must be identical to the source frame
116  */
117  static void insertResultDataToChannel(const Frame& indexLookup, const MatrixF& vectorX, const CV::PixelBoundingBox& sourceBoundingBox, const unsigned int targetLeft, const unsigned int targetTop, const unsigned int channelIndex, Frame& target);
118 };
119 
120 }
121 
122 }
123 
124 }
125 
126 #endif // META_OCEAN_CV_ADVANCED_POISSON_BLENDING_H
Provides image blending based on Poisson Blending, a seamless image composition algorithm from Perez,...
Definition: PoissonBlending.h:36
static size_t createIndexLookup(const Frame &sourceMask, const CV::PixelBoundingBox &sourceBoundingBox, const uint8_t maskValue, Frame &indexLookup)
Creates an index lookup frame for masked source pixels.
static void poissonBlendingSubset(const Frame *indexLookup, const Frame *source, Frame *target, const SparseMatrixF *matrixA, const CV::PixelBoundingBox *sourceBoundingBox, const unsigned int targetLeft, const unsigned int targetTop, const unsigned int firstChannel, const unsigned int channelCount)
Performs separate Poisson blending calculations for a range of color channels.
static void createMaskNeighborRelationsMatrix(const Frame &indexLookup, const CV::PixelBoundingBox &sourceBoundingBox, const unsigned int targetLeft, const unsigned int targetTop, const unsigned int targetWidth, const unsigned int targetHeight, SparseMatrixF &matrixA)
Creates a sparse matrix representing the relations between neighbored source mask pixels.
static void poissonBlending(const Frame &source, const Frame &sourceMask, Frame &target, const int left, const int top, const uint8_t maskValue=0xFFu, Worker *worker=nullptr)
Performs Poisson Blending of the specified source frame into the specified target frame.
static void createSummedBorderLaplacianVector(const Frame &indexLookup, const Frame &source, const Frame &target, const CV::PixelBoundingBox &sourceBoundingBox, const unsigned int targetLeft, const unsigned int targetTop, const unsigned int channelIndex, MatrixF &vectorB)
Creates the vector representing the sum of border pixels and source frame laplacian for each pixel wi...
static void insertResultDataToChannel(const Frame &indexLookup, const MatrixF &vectorX, const CV::PixelBoundingBox &sourceBoundingBox, const unsigned int targetLeft, const unsigned int targetTop, const unsigned int channelIndex, Frame &target)
Inserts the solved matrix into the output frame for the specified channel number.
This class implements Ocean's image class.
Definition: Frame.h:1792
This class implements a matrix with arbitrary size.
Definition: Matrix.h:63
This class implements a sparse matrix using a float type for its elements that is specified by T.
Definition: SparseMatrix.h:61
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15