Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnMaskedPatchMatch.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnMaskedPatchMatch.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 // c++ standard headers
14 #include <cstdlib>
15 #include <cstdio>
16 
17 // eigen matrix library headers
18 #include "Eigen/Core"
19 
20 // opencv library headers
21 #include "cv.h"
22 
23 // darwin library headers
24 #include "drwnBase.h"
25 #include "drwnIO.h"
26 #include "drwnVision.h"
27 
28 using namespace std;
29 
30 // drwnBasicPatchMatch -------------------------------------------------------
48 
49 cv::Mat drwnBasicPatchMatch(const cv::Mat& imgA, const cv::Mat& imgB,
50  const cv::Size& patchRadius, cv::Mat& nnfA, unsigned maxIterations = 2);
51 
52 // drwnSelfPatchMatch --------------------------------------------------------
55 
56 cv::Mat drwnSelfPatchMatch(const cv::Mat& imgA, const cv::Size& patchRadius,
57  cv::Mat& nnfA, double illegalOverlap = 0.0, unsigned maxIterations = 2);
58 
59 // drwnNNFRepaint ------------------------------------------------------------
69 
70 cv::Mat drwnNNFRepaint(const cv::Mat& img, const cv::Mat& nnf);
71 
72 // drwnMaskedPatchMatch ------------------------------------------------------
110 
112  public:
113  static bool TRY_IDENTITY_INIT;
114  static int DISTANCE_MEASURE;
115  static float HEIGHT_PENALTY;
116 
117  protected:
118  cv::Mat _imgA;
119  cv::Mat _imgB;
120  cv::Mat _maskA;
121  cv::Mat _maskB;
122  cv::Mat _invmaskA;
123  cv::Mat _invmaskB;
124 
125  cv::Mat _overlapA;
126  cv::Mat _validB;
127 
128  cv::Size _patchRadius;
129  cv::Mat _nnfA;
130  cv::Mat _costsA;
131 
133  cv::Mat _lastChanged;
134 
135  public:
137  drwnMaskedPatchMatch(const cv::Mat& imgA, const cv::Mat& imgB, unsigned patchRadius);
139  drwnMaskedPatchMatch(const cv::Mat& imgA, const cv::Mat& imgB, const cv::Size& patchRadius);
141  drwnMaskedPatchMatch(const cv::Mat& imgA, const cv::Mat& imgB, const cv::Mat& maskA,
142  const cv::Mat& maskB, unsigned patchRadius);
144  drwnMaskedPatchMatch(const cv::Mat& imgA, const cv::Mat& imgB, const cv::Mat& maskA,
145  const cv::Mat& maskB, const cv::Size& patchRadius);
147  virtual ~drwnMaskedPatchMatch() { /* do nothing */ }
148 
150  const cv::Mat& getSourceImage() const { return _imgA; }
152  const cv::Mat& getTargetImage() const { return _imgB; }
154  const cv::Mat& getSourceMask() const { return _maskA; }
156  const cv::Mat& getTargetMask() const { return _maskB; }
158  cv::Size getPatchSize() const { return cv::Size(2 * _patchRadius.width + 1, 2 * _patchRadius.height + 1); }
160  cv::Size getFieldSize() const { return _nnfA.size(); }
163  std::pair<cv::Rect, cv::Rect> getMatchingPatches(const cv::Point& ptA) const;
164 
166  void initialize() { initialize(_patchRadius); }
168  void initialize(const cv::Size& patchRadius);
170  void initialize(const cv::Mat& nnf);
171 
174  const cv::Mat& search(unsigned maxIterations = 1) {
175  return search(cv::Rect(0, 0, _nnfA.cols, _nnfA.rows), maxIterations);
176  }
180  const cv::Mat& search(cv::Rect roiToUpdate, unsigned maxIterations = 1);
181 
183  const cv::Mat& nnf() const { return _nnfA; }
185  const cv::Mat& costs() const { return _costsA; }
187  double energy() const { return cv::norm(_costsA, cv::NORM_L1, _maskA); }
188 
191  void modifySourceImage(const cv::Rect& roi, const cv::Mat& img, double alpha = 0.0);
194  void modifySourceImage(const cv::Rect& roiA, const cv::Rect& roiB, double alpha = 0.0);
197  void modifyTargetImage(const cv::Rect& roi, const cv::Mat& img, double alpha = 0.0);
200  void modifyTargetImage(const cv::Rect& roiA, const cv::Rect& roiB, double alpha = 0.0);
204  void expandTargetMask(unsigned radius = 1);
205 
207 
209  cv::Mat visualize() const;
210 
211  protected:
213  bool update(const cv::Point& ptA, const cv::Point& ptB);
215  float score(const cv::Point& ptA, const cv::Point& ptB) const;
216 
218  cv::Rect affectedRegion(const cv::Rect& modified) const {
219  const int y1 = std::max(modified.y - _patchRadius.height, _patchRadius.height);
220  const int x1 = std::max(modified.x - _patchRadius.width, _patchRadius.width);
221  const int y2 = std::min(modified.y + modified.height + _patchRadius.height, _nnfA.rows - _patchRadius.height);
222  const int x2 = std::min(modified.x + modified.width + _patchRadius.width, _nnfA.cols - _patchRadius.width);
223  return cv::Rect(x1, y1, x2 - x1, y2 - y1);
224  }
225 
227  void rescore() { rescore(cv::Rect(0, 0, _imgA.rows, _imgA.cols)); }
230  void rescore(const cv::Rect& roi);
231 
233  void cacheValidPixels();
237  void updateValidPixels(const cv::Rect& roi);
238 };
cv::Mat _lastChanged
last iteration that given patch was changed
Definition: drwnMaskedPatchMatch.h:133
int _iterationCount
total number of iterations since initialization
Definition: drwnMaskedPatchMatch.h:132
Implements the basic PatchMatch algorithm of Barnes et al., SIGGRAPH 2009 on masked images...
Definition: drwnMaskedPatchMatch.h:111
double energy() const
return the current match energy
Definition: drwnMaskedPatchMatch.h:187
cv::Mat _invmaskA
CV_8UC1 inverse of _maskA.
Definition: drwnMaskedPatchMatch.h:122
static int DISTANCE_MEASURE
norm type for comparing patches (cv::NORM_L1 or cv::NORM_L2)
Definition: drwnMaskedPatchMatch.h:114
cv::Size _patchRadius
size of patch is 2 * _patchRadius + 1
Definition: drwnMaskedPatchMatch.h:128
const cv::Mat & nnf() const
return the current nearest neighbour field
Definition: drwnMaskedPatchMatch.h:183
cv::Size getPatchSize() const
return the patch size
Definition: drwnMaskedPatchMatch.h:158
cv::Mat _maskB
CV_8UC1 mask for valid target pixels.
Definition: drwnMaskedPatchMatch.h:121
const cv::Mat & getSourceMask() const
return the source mask
Definition: drwnMaskedPatchMatch.h:154
cv::Mat _nnfA
CV_16S2 nearest neighbour field (size _imgA)
Definition: drwnMaskedPatchMatch.h:129
cv::Mat _overlapA
pixels in _imgA whose patch overlaps with _invmaskA
Definition: drwnMaskedPatchMatch.h:125
void rescore()
update scores on whole image
Definition: drwnMaskedPatchMatch.h:227
cv::Size getFieldSize() const
return the size of the nearest neighbour field
Definition: drwnMaskedPatchMatch.h:160
const cv::Mat & costs() const
return (matching) costs (CV_32FC1) associated with the current nearest neighbour field ...
Definition: drwnMaskedPatchMatch.h:185
const cv::Mat & getSourceImage() const
return the source image
Definition: drwnMaskedPatchMatch.h:150
virtual ~drwnMaskedPatchMatch()
destructor
Definition: drwnMaskedPatchMatch.h:147
const cv::Mat & getTargetImage() const
return the target image
Definition: drwnMaskedPatchMatch.h:152
cv::Mat _imgB
CV_8U multi-channel image (target)
Definition: drwnMaskedPatchMatch.h:119
const cv::Mat & getTargetMask() const
return the target mask
Definition: drwnMaskedPatchMatch.h:156
static bool TRY_IDENTITY_INIT
attempt identity match during initialization (default: true)
Definition: drwnMaskedPatchMatch.h:113
void initialize()
initialize the matches with default patch size
Definition: drwnMaskedPatchMatch.h:166
cv::Mat _imgA
CV_8U multi-channel image (source)
Definition: drwnMaskedPatchMatch.h:118
cv::Mat _invmaskB
CV_8UC1 inverse of _maskB.
Definition: drwnMaskedPatchMatch.h:123
cv::Mat _costsA
CV_32FC1 field of match costs (same size as _nnfA)
Definition: drwnMaskedPatchMatch.h:130
cv::Rect affectedRegion(const cv::Rect &modified) const
calculate nnf pixels affected by a mask or image modification
Definition: drwnMaskedPatchMatch.h:218
cv::Mat _maskA
CV_8UC1 mask for valid source pixels.
Definition: drwnMaskedPatchMatch.h:120
cv::Mat _validB
pixels in _imgB whose patch doesn't overlap with _maskB
Definition: drwnMaskedPatchMatch.h:126
static float HEIGHT_PENALTY
bias term added for row/height difference
Definition: drwnMaskedPatchMatch.h:115
const cv::Mat & search(unsigned maxIterations=1)
Search for better matches and return updated nearest neighbour field. The nearest neighbour field map...
Definition: drwnMaskedPatchMatch.h:174