Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
drwnMaskedPatchMatch Class Reference

Implements the basic PatchMatch algorithm of Barnes et al., SIGGRAPH 2009 on masked images. More...

Public Member Functions

 drwnMaskedPatchMatch (const cv::Mat &imgA, const cv::Mat &imgB, unsigned patchRadius)
 construct without masks and square patches
 
 drwnMaskedPatchMatch (const cv::Mat &imgA, const cv::Mat &imgB, const cv::Size &patchRadius)
 construct without masks and arbitrary patches
 
 drwnMaskedPatchMatch (const cv::Mat &imgA, const cv::Mat &imgB, const cv::Mat &maskA, const cv::Mat &maskB, unsigned patchRadius)
 costruct with matching masks (empty for no mask) and square patches
 
 drwnMaskedPatchMatch (const cv::Mat &imgA, const cv::Mat &imgB, const cv::Mat &maskA, const cv::Mat &maskB, const cv::Size &patchRadius)
 costruct with matching masks (empty for no mask) and arbitrary patches
 
virtual ~drwnMaskedPatchMatch ()
 destructor
 
const cv::Mat & getSourceImage () const
 return the source image
 
const cv::Mat & getTargetImage () const
 return the target image
 
const cv::Mat & getSourceMask () const
 return the source mask
 
const cv::Mat & getTargetMask () const
 return the target mask
 
cv::Size getPatchSize () const
 return the patch size
 
cv::Size getFieldSize () const
 return the size of the nearest neighbour field
 
std::pair< cv::Rect, cv::Rect > getMatchingPatches (const cv::Point &ptA) const
 Return the best matching pair of patches (source and destination) for source patch centred at ptA. Points close to the boundary have their size adjusted.
 
void initialize ()
 initialize the matches with default patch size
 
void initialize (const cv::Size &patchRadius)
 initialize the matches given patchRadius More...
 
void initialize (const cv::Mat &nnf)
 initialize the matches given nearest neighbour field (patch size is unchanged)
 
const cv::Mat & search (unsigned maxIterations=1)
 Search for better matches and return updated nearest neighbour field. The nearest neighbour field maps is indexed by the centre pixel of the patch.
 
const cv::Mat & search (cv::Rect roiToUpdate, unsigned maxIterations=1)
 Search for better matches on subregion of the image and return updated nearest neighbour field. Only regions whose centre pixel appear in roiToUpdate will be changed.
 
const cv::Mat & nnf () const
 return the current nearest neighbour field
 
const cv::Mat & costs () const
 return (matching) costs (CV_32FC1) associated with the current nearest neighbour field
 
double energy () const
 return the current match energy
 
void modifySourceImage (const cv::Rect &roi, const cv::Mat &img, double alpha=0.0)
 Copy over a region of the source image and unmask the region copied into. Alpha-blends with the masked area.
 
void modifySourceImage (const cv::Rect &roiA, const cv::Rect &roiB, double alpha=0.0)
 Copy over a region of the source image from the target image and unmask the region copied into. Alpha-blends with the masked area.
 
void modifyTargetImage (const cv::Rect &roi, const cv::Mat &img, double alpha=0.0)
 Copy over a region of the target image (_imgB) and unmask the region copied into. Alpha-blends with the masked area.
 
void modifyTargetImage (const cv::Rect &roiA, const cv::Rect &roiB, double alpha=0.0)
 Copy over a region of the target image (_imgB) from itself and unmask the region copied into. Alpha-blends with the masked area.
 
void expandTargetMask (unsigned radius=1)
 Expand the target mask (_maskB) by dilating pixels around the boundary with the unmasked region. The expansion kernel is a rectangle of size 2 * radius + 1.
 
cv::Mat visualize () const
 visualize the nearest neighbour field More...
 

Static Public Attributes

static bool TRY_IDENTITY_INIT = true
 attempt identity match during initialization (default: true)
 
static int DISTANCE_MEASURE = cv::NORM_L1
 norm type for comparing patches (cv::NORM_L1 or cv::NORM_L2)
 
static float HEIGHT_PENALTY = 32.0f
 bias term added for row/height difference
 

Protected Member Functions

bool update (const cv::Point &ptA, const cv::Point &ptB)
 attempt and update a match candidate
 
float score (const cv::Point &ptA, const cv::Point &ptB) const
 scores a match
 
cv::Rect affectedRegion (const cv::Rect &modified) const
 calculate nnf pixels affected by a mask or image modification
 
void rescore ()
 update scores on whole image
 
void rescore (const cv::Rect &roi)
 Update scores on given region. All affected scores are updated, that is, any score that was calculated from a patch that overlaps with roi.
 
void cacheValidPixels ()
 compute valid pixels
 
void updateValidPixels (const cv::Rect &roi)
 Update the validity of pixels that occur because of a change to the source mask on a given region (i.e., any pixel whose corresponding patch overlaps with the region).
 

Protected Attributes

cv::Mat _imgA
 CV_8U multi-channel image (source)
 
cv::Mat _imgB
 CV_8U multi-channel image (target)
 
cv::Mat _maskA
 CV_8UC1 mask for valid source pixels.
 
cv::Mat _maskB
 CV_8UC1 mask for valid target pixels.
 
cv::Mat _invmaskA
 CV_8UC1 inverse of _maskA.
 
cv::Mat _invmaskB
 CV_8UC1 inverse of _maskB.
 
cv::Mat _overlapA
 pixels in _imgA whose patch overlaps with _invmaskA
 
cv::Mat _validB
 pixels in _imgB whose patch doesn't overlap with _maskB
 
cv::Size _patchRadius
 size of patch is 2 * _patchRadius + 1
 
cv::Mat _nnfA
 CV_16S2 nearest neighbour field (size _imgA)
 
cv::Mat _costsA
 CV_32FC1 field of match costs (same size as _nnfA)
 
int _iterationCount
 total number of iterations since initialization
 
cv::Mat _lastChanged
 last iteration that given patch was changed
 

Detailed Description

Implements the basic PatchMatch algorithm of Barnes et al., SIGGRAPH 2009 on masked images.

The source and target images can have different sizes but must both be CV_8U and have the same number of channels (features). The source image/feautures and mask can be adjusted between calls to search(). Masked regions in the source image are ignored. Matching to masked regions in the target image has infinite cost.

Each pixel in the returned nearest neighbour field represents the offset to the optimal match for a patch of size 2 * patchRadius + 1 and centred on that pixel. Use the getMatchingPatches() to get matching regions with size adjusted for pixels within patchRadius pixel of the boundary.

cv::Mat imgA;
cv::Mat imgB;
cv::Size patchRadius;
...
drwnMaskedPatchMatch pm(imgA, imgB, patchRadius);
pm.search(10);
Vec2s p = pm.nnf().at<Vec2s>(y, x);
cv::Rect rA(x - patchRadius.width, y - patchRadius.height, 2 * patchRadius.width + 1, 2 * patchRadius.height + 1);
cv::Rect rB(p[0] - patchRadius.width, p[1] - patchRadius.height, 2 * patchRadius.width + 1, 2 * patchRadius.height + 1);
cout << rA << " in imageA matches " << rB << " in imageB\n";

Alternatively you can use the getMatchingPatches() function, as in:

pair<cv::Rect, cv::Rect> r = pm.getMatchingPatches(cv::Point(x, y));
cout << r.first << " in imageA matches " << r.second << " in imageB\n";
See Also
drwnInPaint

Member Function Documentation

void drwnMaskedPatchMatch::initialize ( const cv::Size &  patchRadius)

initialize the matches given patchRadius

only attempt valid B

cv::Mat drwnMaskedPatchMatch::visualize ( ) const

visualize the nearest neighbour field

Todo:
Add modifySourceImage and modifyTargetImage functions based on a mask

The documentation for this class was generated from the following files: