Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnGrabCutInstance.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: drwnGrabCutInstance.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 ** Kevin Guo <Kevin.Guo@nicta.com.au>
11 **
12 *****************************************************************************/
13 
14 #pragma once
15 #include "Eigen/Core"
16 
17 #include "cv.h"
18 
19 #include "drwnBase.h"
20 #include "drwnIO.h"
21 #include "drwnML.h"
22 
23 #include "drwnColourHistogram.h"
24 #include "drwnPixelNeighbourContrasts.h"
25 
26 using namespace std;
27 using namespace Eigen;
28 
29 // drwnGrabCutInstance ------------------------------------------------------
45 
47 public:
48  static const unsigned char MASK_FG = 0xff;
49  static const unsigned char MASK_BG = 0x00;
50  static const unsigned char MASK_C_FG = 0x80;
51  static const unsigned char MASK_C_BG = 0x40;
52  static const unsigned char MASK_C_BOTH = 0xc0;
53  static const unsigned char MASK_C_NONE = 0x20;
54 
55  static bool bVisualize;
56  static int maxIterations;
57 
58 public:
59  // meta parameters
60  string name;
61 
62 protected:
63  // instance definition
64  cv::Mat _img;
65  cv::Mat _trueMask;
66  cv::Mat _mask;
68 
69  // cached data
70  cv::Mat _unary;
72 
73  // model parameters
74  double _unaryWeight;
75  double _pottsWeight;
76  double _pairwiseWeight;
77 
78 public:
84  virtual ~drwnGrabCutInstance();
85 
87  int width() const { return _img.cols; }
89  int height() const { return _img.rows; }
91  int size() const { return _img.cols * _img.rows; }
93  int numUnknown() const { return _numUnknown; }
94 
95  // caller does not need to free returned objects
97  const cv::Mat& image() const { return _img; }
99  const cv::Mat& trueSegmentation() const { return _trueMask; }
101  const cv::Mat& segmentationMask() const { return _mask; }
102 
104  cv::Mat knownForeground() const;
106  cv::Mat knownBackground() const;
108  cv::Mat unknownPixels() const;
110  cv::Mat foregroundColourMask() const;
112  cv::Mat backgroundColourMask() const;
113 
115  bool isUnknownPixel(int x, int y, const cv::Mat& mask) const {
116  const unsigned char p = mask.at<unsigned char>(y, x);
117  return ((p != MASK_FG) && (p != MASK_BG));
118  }
120  bool isUnknownPixel(int x, int y) const {
121  return isUnknownPixel(x, y, _mask);
122  }
123 
126  void initialize(const cv::Mat& img, const cv::Rect& rect, const char *colorModelFile = NULL);
128  void initialize(const cv::Mat& img, const cv::Mat& inferMask, const char *colorModelFile = NULL);
130  void initialize(const cv::Mat& img, const cv::Rect& rect, const cv::Mat& trueMask,
131  const char *colorModelFile = NULL);
133  void initialize(const cv::Mat& img, const cv::Mat& inferMask, const cv::Mat& trueMask,
134  const char *colorModelFile = NULL);
135 
137  virtual void loadColourModels(const char *filename) = 0;
139  virtual void saveColourModels(const char *filename) const = 0;
140 
142  void setBaseModelWeights(double u, double p, double c);
143 
145  const cv::Mat& unaryPotentials() const { return _unary; }
146 
148  const drwnPixelNeighbourContrasts *pairwisePotentials() { return _pairwise; }
149 
151  double unaryEnergy(const cv::Mat& seg) const;
152 
154  double pottsEnergy(const cv::Mat& seg) const;
155 
157  double pairwiseEnergy(const cv::Mat& seg) const;
158 
160  virtual double energy(const cv::Mat& seg) const;
161 
163  double foregroundRatio(const cv::Mat& seg) const;
164 
166  inline double backgroundRatio(const cv::Mat& seg) const {
167  return 1.0 - foregroundRatio(seg);
168  }
169 
171  double loss(const cv::Mat& seg) const;
172 
174  virtual cv::Mat inference();
176  virtual cv::Mat lossAugmentedInference();
177 
179  virtual cv::Mat visualize(const cv::Mat& seg) const;
180 
181 protected:
183  void free();
184 
186  inline vector<double> pixelColour(int y, int x) const;
187 
189  virtual void learnColourModel(const cv::Mat& mask, bool bForeground) = 0;
190 
192  virtual void updateUnaryPotentials() = 0;
193 
195  virtual cv::Mat graphCut(const cv::Mat& unary) const;
196 };
197 
198 // drwnGrabCutInstanceGMM ---------------------------------------------------
199 
201  public:
202  static size_t maxSamples;
203  static int numMixtures;
204 
205  protected:
208 
209  public:
213  virtual ~drwnGrabCutInstanceGMM();
214 
216  void loadColourModels(const char *filename);
218  void saveColourModels(const char *filename) const;
219 
220 protected:
222  void learnColourModel(const cv::Mat& mask, bool bForeground);
223 
225  void updateUnaryPotentials();
226 };
227 
228 // drwnGrabCutInstanceHistogram ---------------------------------------------
229 
231  public:
232  static double pseudoCounts;
233  static unsigned channelBits;
234 
235  protected:
238 
239  public:
243  virtual ~drwnGrabCutInstanceHistogram();
244 
246  void loadColourModels(const char *filename);
248  void saveColourModels(const char *filename) const;
249 
250 protected:
252  void learnColourModel(const cv::Mat& mask, bool bForeground);
253 
255  void updateUnaryPotentials();
256 };
Definition: drwnGrabCutInstance.h:200
const cv::Mat & unaryPotentials() const
get unary potentials
Definition: drwnGrabCutInstance.h:145
double _pairwiseWeight
weight for contrast-sensitive pairwise term
Definition: drwnGrabCutInstance.h:76
int numUnknown() const
number of unknown pixels in the inference mask
Definition: drwnGrabCutInstance.h:93
string name
instance name (if available)
Definition: drwnGrabCutInstance.h:60
static size_t maxSamples
maximum samples to use for colour models
Definition: drwnGrabCutInstance.h:202
int size() const
number of pixels in the image
Definition: drwnGrabCutInstance.h:91
double _pottsWeight
weight for potts smoothness term
Definition: drwnGrabCutInstance.h:75
Implements a multi-variant Gaussian mixture model.
Definition: drwnGaussianMixture.h:57
Definition: drwnGrabCutInstance.h:230
cv::Mat _unary
unary potentials, (y_i == background)
Definition: drwnGrabCutInstance.h:70
Convenience class for holding pixel contrast weights.
Definition: drwnPixelNeighbourContrasts.h:30
static int numMixtures
number of mixture components in colour models
Definition: drwnGrabCutInstance.h:203
double _unaryWeight
weight for unary term in grabCut model
Definition: drwnGrabCutInstance.h:74
drwnGaussianMixture _fgColourModel
foreground colour model
Definition: drwnGrabCutInstance.h:206
const cv::Mat & trueSegmentation() const
return the mask for the true segmentation (if known)
Definition: drwnGrabCutInstance.h:99
int height() const
height of the image
Definition: drwnGrabCutInstance.h:89
Implements the grabCut algorithm of Rother et al., SIGGRAPH 2004 for figure/ground segmentation...
Definition: drwnGrabCutInstance.h:46
int _numUnknown
number of unknown pixels in _mask
Definition: drwnGrabCutInstance.h:67
bool isUnknownPixel(int x, int y) const
returns true if the pixel at (x, y) is not foreground or background in the initialized inference mask...
Definition: drwnGrabCutInstance.h:120
drwnColourHistogram _bgColourModel
background colour model
Definition: drwnGrabCutInstance.h:237
const cv::Mat & image() const
return the image
Definition: drwnGrabCutInstance.h:97
static unsigned channelBits
number of bits per RGB colour channel
Definition: drwnGrabCutInstance.h:233
cv::Mat _trueMask
ground-truth segmentation (trimap)
Definition: drwnGrabCutInstance.h:65
cv::Mat _img
the image
Definition: drwnGrabCutInstance.h:64
int width() const
width of the image
Definition: drwnGrabCutInstance.h:87
double backgroundRatio(const cv::Mat &seg) const
compute the percentage of unknown pixels labeled as background
Definition: drwnGrabCutInstance.h:166
bool isUnknownPixel(int x, int y, const cv::Mat &mask) const
returns true if the pixel at (x, y) is not foreground or background in mask
Definition: drwnGrabCutInstance.h:115
drwnGaussianMixture _bgColourModel
background colour model
Definition: drwnGrabCutInstance.h:207
cv::Mat _mask
segmentation mask (quadmap)
Definition: drwnGrabCutInstance.h:66
drwnColourHistogram _fgColourModel
foreground colour model
Definition: drwnGrabCutInstance.h:236
static double pseudoCounts
pseudocounts in colour histogram model
Definition: drwnGrabCutInstance.h:232
Specialized histogram for quantized 3-channel colour values (e.g., RGB).
Definition: drwnColourHistogram.h:42
static int maxIterations
maximum number of inference iterations
Definition: drwnGrabCutInstance.h:56
static bool bVisualize
visualize output
Definition: drwnGrabCutInstance.h:55
drwnPixelNeighbourContrasts * _pairwise
pairwise potentials
Definition: drwnGrabCutInstance.h:71
const drwnPixelNeighbourContrasts * pairwisePotentials()
get pairwise potentials
Definition: drwnGrabCutInstance.h:148
const cv::Mat & segmentationMask() const
return the mask for the inferred segmentation
Definition: drwnGrabCutInstance.h:101