23 #include "drwnColourHistogram.h"
24 #include "drwnPixelNeighbourContrasts.h"
27 using namespace Eigen;
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;
87 int width()
const {
return _img.cols; }
89 int height()
const {
return _img.rows; }
91 int size()
const {
return _img.cols * _img.rows; }
97 const cv::Mat&
image()
const {
return _img; }
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;
116 const unsigned char p = mask.at<
unsigned char>(y, x);
117 return ((p != MASK_FG) && (p != MASK_BG));
121 return isUnknownPixel(x, y, _mask);
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);
137 virtual void loadColourModels(
const char *filename) = 0;
139 virtual void saveColourModels(
const char *filename)
const = 0;
142 void setBaseModelWeights(
double u,
double p,
double c);
151 double unaryEnergy(
const cv::Mat& seg)
const;
154 double pottsEnergy(
const cv::Mat& seg)
const;
157 double pairwiseEnergy(
const cv::Mat& seg)
const;
160 virtual double energy(
const cv::Mat& seg)
const;
163 double foregroundRatio(
const cv::Mat& seg)
const;
167 return 1.0 - foregroundRatio(seg);
171 double loss(
const cv::Mat& seg)
const;
174 virtual cv::Mat inference();
176 virtual cv::Mat lossAugmentedInference();
179 virtual cv::Mat visualize(
const cv::Mat& seg)
const;
186 inline vector<double> pixelColour(
int y,
int x)
const;
189 virtual void learnColourModel(
const cv::Mat& mask,
bool bForeground) = 0;
192 virtual void updateUnaryPotentials() = 0;
195 virtual cv::Mat graphCut(
const cv::Mat& unary)
const;
216 void loadColourModels(
const char *filename);
218 void saveColourModels(
const char *filename)
const;
222 void learnColourModel(
const cv::Mat& mask,
bool bForeground);
225 void updateUnaryPotentials();
246 void loadColourModels(
const char *filename);
248 void saveColourModels(
const char *filename)
const;
252 void learnColourModel(
const cv::Mat& mask,
bool bForeground);
255 void updateUnaryPotentials();
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