16 #if defined(_WIN32)||defined(WIN32)||defined(__WIN32__)||defined(__VISUALC__)
26 #include "drwnVision.h"
34 typedef enum { DRWN_PM_DIRTY,
37 } drwnPatchMatchStatus;
44 typedef unsigned char drwnPatchMatchTransform;
46 #define DRWN_PM_TRANSFORM_NONE 0x00
47 #define DRWN_PM_TRANSFORM_HFLIP 0x01
48 #define DRWN_PM_TRANSFORM_VFLIP 0x02
51 inline drwnPatchMatchTransform drwnComposeTransforms(
const drwnPatchMatchTransform& f,
52 const drwnPatchMatchTransform& g) {
77 imgIndx(idx), imgScale(sc), xPosition(x), yPosition(y) { };
79 imgIndx(idx), imgScale(sc), xPosition(p.x), yPosition(p.y) { };
84 if (imgIndx < node.
imgIndx)
return true;
85 if (imgIndx > node.
imgIndx)
return false;
86 if (imgScale < node.
imgScale)
return true;
87 if (imgScale > node.
imgScale)
return false;
88 if (yPosition < node.
yPosition)
return true;
89 if (yPosition > node.
yPosition)
return false;
95 if (imgIndx != node.
imgIndx)
return false;
96 if (imgScale != node.
imgScale)
return false;
97 if (yPosition != node.
yPosition)
return false;
98 if (xPosition != node.
xPosition)
return false;
120 xform(DRWN_PM_TRANSFORM_NONE), status(DRWN_PM_DIRTY) { };
123 const drwnPatchMatchTransform& patchXform) : matchScore(score), targetNode(node),
124 xform(patchXform), status(DRWN_PM_DIRTY) { };
127 size_t numBytesOnDisk()
const;
129 bool read(istream& is);
131 bool write(ostream& os)
const;
158 typedef vector<drwnPatchMatchEdge> drwnPatchMatchEdgeList;
172 vector<drwnPatchMatchEdgeList> _matches;
181 DRWN_ASSERT((width > 0) && (height > 0));
182 _matches.resize(width * height);
190 uint16_t
width()
const {
return _width; }
192 uint16_t
height()
const {
return _height; }
194 size_t size()
const {
return _width * _height; }
200 inline uint16_t
x(
int indx)
const {
return indx % _width; }
202 inline uint16_t
y(
int indx)
const {
return indx / _width; }
204 inline cv::Point
index2pixel(
int indx)
const {
return cv::Point(indx % _width, indx / _width); }
206 inline int pixel2index(uint16_t x, uint16_t y)
const {
return y * _width + x; }
214 return update(pixel2index(x, y), match);
218 drwnPatchMatchEdgeList&
operator()(uint16_t x, uint16_t y) {
return _matches[y * _width + x]; }
220 const drwnPatchMatchEdgeList&
operator()(uint16_t x, uint16_t y)
const {
return _matches[y * _width + x]; }
222 drwnPatchMatchEdgeList&
operator[](
int indx) {
return _matches[indx]; }
224 const drwnPatchMatchEdgeList&
operator[](
int indx)
const {
return _matches[indx]; }
260 bActive(true), eqvClass(-1), _name(
""), _width(0), _height(0) {
266 _name(name), _width(width), _height(height) {
267 DRWN_ASSERT((width > 0) && (height > 0));
268 constructPyramidLevels();
276 const string&
name()
const {
return _name; }
278 uint16_t
width()
const {
return _width; }
280 uint16_t
height()
const {
return _height; }
282 size_t levels()
const {
return _levels.size(); }
288 size_t numBytesOnDisk()
const;
289 bool write(ostream& os)
const;
290 bool read(istream& is);
293 inline cv::Point
mapPixel(
const cv::Point& p,
int srcLevel,
int dstLevel)
const {
294 return cv::Point(p.x * _levels[dstLevel].width() / _levels[srcLevel].width(),
295 p.y * _levels[dstLevel].height() / _levels[srcLevel].height());
309 void constructPyramidLevels();
327 static unsigned int K;
346 bool write(
const char *filestem)
const;
348 bool read(
const char *filestem);
351 size_t size()
const {
return _images.size(); }
359 int findImage(
const string& baseName)
const;
361 void appendImage(
const string& baseName);
363 void appendImage(
const string& baseName,
const cv::Size& imgSize);
365 void appendImages(
const vector<string>& baseNames);
369 int removeImage(
unsigned imgIndx);
385 pair<double, double> energy()
const;
391 return imageDirectory.empty() ? baseName + imageExtension :
392 imageDirectory + DRWN_DIRSEP + baseName + imageExtension;
419 #ifdef DRWN_DEBUG_STATISTICS
420 static unsigned _dbPatchesScored;
436 virtual void initialize(
unsigned imgIndx);
445 virtual void update(
unsigned imgIndx);
452 void cacheImageFeatures();
454 void cacheImageFeatures(
unsigned imgIndx);
456 virtual void cacheImageFeatures(
unsigned imgIndx,
const cv::Mat& baseImg);
460 int appendCIELabFeatures(
const cv::Mat& img, cv::Mat& features,
int nChannel = 0)
const;
461 int appendVerticalFeatures(
const cv::Mat& img, cv::Mat& features,
int nChannel = 0)
const;
462 int appendEdgeFeatures(
const cv::Mat& img, cv::Mat& features,
int nChannel = 0)
const;
463 int appendTextonFilterFeatures(
const cv::Mat& img, cv::Mat& features,
int nChannel = 0)
const;
479 virtual bool enrichment();
489 const drwnPatchMatchTransform& xform,
float maxValue = DRWN_FLT_MAX)
const;
492 cv::Point mapPatch(
const cv::Point& p,
int imgIndx,
int srcScale,
int dstScale)
const;
510 virtual cv::Mat retarget(
unsigned imgIndx)
const;
513 void updateImageLabels(
unsigned imgIndx,
const cv::Mat& labels);
517 void cacheImageLabels();
519 virtual void cacheImageLabels(
unsigned imgIndx);
525 namespace drwnPatchMatchVis {
528 int imgIndx,
const cv::Point& p);
532 int imgIndx,
float maxScore = 0.0,
unsigned kthBest = 0);
uint16_t height() const
image height
Definition: drwnPatchMatch.h:192
vector< vector< cv::Mat > > _features
image features indexed by (image, pyramid level)
Definition: drwnPatchMatch.h:425
unsigned int patchHeight() const
patch height used for constructing this graph
Definition: drwnPatchMatch.h:356
uint16_t width() const
image width
Definition: drwnPatchMatch.h:278
cv::Point index2pixel(int indx) const
convert from index to pixel coordinates
Definition: drwnPatchMatch.h:204
float matchScore
score of this match
Definition: drwnPatchMatch.h:111
cv::Mat visualizeMatches(const drwnPatchMatchGraph &graph, int imgIndx, const cv::Point &p)
visualize matches for a given pixel
Definition: drwnPatchMatch.cpp:1711
uint16_t yPosition
y location of the patch in the target pyramid level
Definition: drwnPatchMatch.h:71
static unsigned int K
default number of matches per pixel
Definition: drwnPatchMatch.h:327
bool operator<(const drwnPatchMatchEdge &e) const
default comparison for sorting
Definition: drwnPatchMatch.h:134
drwnPatchMatchEdgeList & edges(const drwnPatchMatchNode &node)
returns (a reference to) the list of outgoing edges for a given node
Definition: drwnPatchMatch.h:372
unsigned int _patchWidth
patch width (at base scale in this graph)
Definition: drwnPatchMatch.h:333
static bool DO_LOCAL_SEARCH
run neighbourhood search on dirty pixels
Definition: drwnPatchMatch.h:412
uint16_t _width
width of the image (may differ from first pyramid level)
Definition: drwnPatchMatch.h:251
string imageFilename(int indx) const
full image filename of a given image
Definition: drwnPatchMatch.h:388
static bool ALLOW_MULTIPLE
allow multiple matches within a single image
Definition: drwnPatchMatch.h:165
drwnPatchMatchEdgeList & operator()(uint16_t x, uint16_t y)
reference edges at location (x, y)
Definition: drwnPatchMatch.h:218
static drwnPatchMatchTransform ALLOWED_TRANSFORMATIONS
allowable patch transformations
Definition: drwnPatchMatch.h:415
Class for repainting an image from matches within the PatchMatchGraph.
Definition: drwnPatchMatch.h:500
size_t levels() const
image pyramid levels
Definition: drwnPatchMatch.h:282
static unsigned MIN_SIZE
minimum image size (if smaller than size at MAX_LEVELS)
Definition: drwnPatchMatch.h:235
vector< drwnPatchMatchImageRecord > _levels
pyramid levels holding image matches
Definition: drwnPatchMatch.h:255
drwnPatchMatchStatus status
status flag
Definition: drwnPatchMatch.h:115
string imageDirectory
directory to prepend to instance basename
Definition: drwnPatchMatch.h:329
const drwnPatchMatchImagePyramid & operator[](unsigned indx) const
reference image pyramid level indx
Definition: drwnPatchMatch.h:398
size_t size() const
number of images in the graph
Definition: drwnPatchMatch.h:351
drwnPatchMatchEdgeList & operator[](int indx)
reference edges at index indx
Definition: drwnPatchMatch.h:222
uint16_t imgIndx
index of image (in drwnPatchMatchGraph) for this match
Definition: drwnPatchMatch.h:68
uint16_t height() const
image height
Definition: drwnPatchMatch.h:280
Records matches for one level in an image pyramid.
Definition: drwnPatchMatch.h:163
uint16_t _height
height of the image at this pyrmaid level
Definition: drwnPatchMatch.h:169
size_t size() const
number of pixels in image (width * height)
Definition: drwnPatchMatch.h:194
uint16_t x(int indx) const
convert from index to x pixel coordinate
Definition: drwnPatchMatch.h:200
Record of patch matches for mutliple levels each image.
Definition: drwnPatchMatch.h:231
static unsigned int PATCH_WIDTH
default patch width (at base scale)
Definition: drwnPatchMatch.h:325
drwnPatchMatchImageRecord()
default constructor
Definition: drwnPatchMatch.h:176
uint16_t xPosition
x location of the patch in the target pyramid level
Definition: drwnPatchMatch.h:70
unsigned int _patchHeight
patch height (at base scale in this graph)
Definition: drwnPatchMatch.h:334
drwnPatchMatchImageRecord(uint16_t width, uint16_t height)
constructor
Definition: drwnPatchMatch.h:180
drwnPatchMatchTransform xform
transformation applied to the patch
Definition: drwnPatchMatch.h:113
static unsigned MAX_LEVELS
maximum level in the image pyramid
Definition: drwnPatchMatch.h:233
drwnPatchMatchGraph & _graph
the graph that is being learned
Definition: drwnPatchMatch.h:424
string imageFilename(const string &baseName) const
full image filename from an image basename
Definition: drwnPatchMatch.h:390
static unsigned int PATCH_HEIGHT
default patch height (at base scale)
Definition: drwnPatchMatch.h:326
Learns a PatchMatchGraph by iteratively performing search moves over the space of matches...
Definition: drwnPatchMatch.h:407
static double PYR_SCALE
pyramid downsampling rate
Definition: drwnPatchMatch.h:236
uint16_t width() const
image width
Definition: drwnPatchMatch.h:190
const drwnPatchMatchEdgeList & operator()(uint16_t x, uint16_t y) const
reference edges at location (x, y)
Definition: drwnPatchMatch.h:220
Represents an edge in the drwnPatchMatchGraph.
Definition: drwnPatchMatch.h:109
static double SEARCH_DECAY_RATE
decay rate during search in range [0, 1)
Definition: drwnPatchMatch.h:409
static int FORWARD_ENRICHMENT_K
forward enrichment search depth
Definition: drwnPatchMatch.h:410
drwnPatchMatchNode targetNode
index of image, pyramid level and location of patch
Definition: drwnPatchMatch.h:112
uint16_t _width
width of the image at this pyramid level
Definition: drwnPatchMatch.h:168
uint16_t y(int indx) const
convert from index to x pixel coordinate
Definition: drwnPatchMatch.h:202
~drwnPatchMatchImageRecord()
destructor
Definition: drwnPatchMatch.h:185
std::string toString(const T &v)
Templated function to make conversion from simple data types like int and double to strings easy for ...
Definition: drwnStrUtils.h:134
Represents a node in the drwnPatchMatchGraph.
Definition: drwnPatchMatch.h:66
cv::Mat visualizeMatchTransforms(const drwnPatchMatchGraph &graph, int imgIndx)
visualize best match transformations for a given image
Definition: drwnPatchMatch.cpp:1785
const drwnPatchMatchEdgeList & edges(const drwnPatchMatchNode &node) const
returns (a constant reference to) the list of outgoing edges for a given node
Definition: drwnPatchMatch.h:378
const drwnPatchMatchGraph & _graph
the graph that is being learned
Definition: drwnPatchMatch.h:502
Each image maintains a W-by-H-by-K array of match records referencing the (approximate) best K matche...
Definition: drwnPatchMatch.h:323
static double TOP_VAR_PATCHES
initialize all patches below this with only one match
Definition: drwnPatchMatch.h:417
bool update(uint16_t x, uint16_t y, const drwnPatchMatchEdge &match)
updates edge list for pixel (x,y) with a candidate match and returns true if match is better than exi...
Definition: drwnPatchMatch.h:213
const string & name() const
image name
Definition: drwnPatchMatch.h:276
const drwnPatchMatchImageRecord & operator[](uint8_t indx) const
reference pyramid level indx
Definition: drwnPatchMatch.h:305
string _name
image identifier
Definition: drwnPatchMatch.h:250
drwnPatchMatchImageRecord & operator[](uint8_t indx)
reference pyramid level indx
Definition: drwnPatchMatch.h:303
int eqvClass
Equivalence class for this image. If negative (which is the default) the image is considered to be in...
Definition: drwnPatchMatch.h:247
bool bActive
Include this image during update (initialize, propagate, local, search, and enrichment). If false the image can still be matched to but it's own matches are not updated.
Definition: drwnPatchMatch.h:242
Interface class for drwnPersistentStorage.
Definition: drwnPersistentStorage.h:25
bool operator==(const drwnPatchMatchNode &node) const
equality operator
Definition: drwnPatchMatch.h:94
uint16_t _height
height of the image (may differ from first pyramid level)
Definition: drwnPatchMatch.h:252
bool operator<(const drwnPatchMatchNode &node) const
default comparison for sorting
Definition: drwnPatchMatch.h:83
drwnPatchMatchEdge(float score, const drwnPatchMatchNode &node, const drwnPatchMatchTransform &patchXform)
constructor
Definition: drwnPatchMatch.h:122
int pixel2index(uint16_t x, uint16_t y) const
convert from pixel coordinates to index
Definition: drwnPatchMatch.h:206
static unsigned MAX_SIZE
maximum image size (if larger will be resized in first level)
Definition: drwnPatchMatch.h:234
drwnPatchMatchImagePyramid(const string &name, uint16_t width, uint16_t height)
constructor an image record with a given name (and size)
Definition: drwnPatchMatch.h:264
string imageExtension
extension to append to instance basename
Definition: drwnPatchMatch.h:330
vector< cv::Mat > _labels
the labels that will be used for retargetting
Definition: drwnPatchMatch.h:503
drwnPatchMatchImagePyramid & operator[](unsigned indx)
reference image pyramid indx
Definition: drwnPatchMatch.h:396
static bool DO_INVERSE_ENRICHMENT
perform inverse enrichment
Definition: drwnPatchMatch.h:411
drwnPatchMatchEdge()
default constructor
Definition: drwnPatchMatch.h:119
vector< drwnPatchMatchImagePyramid * > _images
image pyramid matches
Definition: drwnPatchMatch.h:335
const drwnPatchMatchEdgeList & operator[](int indx) const
reference edges at index indx
Definition: drwnPatchMatch.h:224
uint8_t imgScale
level in the image pyramid for the match
Definition: drwnPatchMatch.h:69
cv::Mat visualizeMatchQuality(const drwnPatchMatchGraph &graph, int imgIndx, float maxScore=0.0, unsigned kthBest=0)
visualize match quality for a given image
Definition: drwnPatchMatch.cpp:1736
cv::Mat visualizeMatchTargets(const drwnPatchMatchGraph &graph, int imgIndx)
visualize best match target image for a given image
Definition: drwnPatchMatch.cpp:1819
~drwnPatchMatchImagePyramid()
destructor
Definition: drwnPatchMatch.h:271
unsigned int patchWidth() const
patch width used for constructing this graph
Definition: drwnPatchMatch.h:354
static bool DO_EXHAUSTIVE
run exhustive search on random pixel
Definition: drwnPatchMatch.h:413
drwnPatchMatchImagePyramid()
default constructor
Definition: drwnPatchMatch.h:259
cv::Point mapPixel(const cv::Point &p, int srcLevel, int dstLevel) const
map a pixel from one pyramid level to another
Definition: drwnPatchMatch.h:293