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

Holds multiple oversegmentations for a given image. More...

Inheritance diagram for drwnSuperpixelContainer:
drwnPersistentRecord

Public Member Functions

 drwnSuperpixelContainer ()
 default constructor
 
 drwnSuperpixelContainer (const drwnSuperpixelContainer &container)
 copy constructor
 
void clear ()
 clears all oversegmentations (and releases memory)
 
bool empty () const
 returns true if their are no superpixels
 
int size () const
 returns the number of superpixels
 
int pixels (unsigned segId) const
 returns the number of pixels within the given superpixel
 
int width () const
 returns the image width
 
int height () const
 returns the image height
 
int channels () const
 returns the maximum number of superpixels to which any pixel can belong
 
size_t memory () const
 returns number of bytes stored (approximately)
 
virtual size_t numBytesOnDisk () const
 number of bytes required to store object on disk or in a character stream (without compression)
 
virtual bool write (ostream &os) const
 write the object to an output stream
 
virtual bool read (istream &is)
 read the object from an input stream
 
void loadSuperpixels (const char *filename)
 load superpixels from file (.png or .txt)
 
void addSuperpixels (cv::Mat superpixels)
 add 32-bit oversegmentation (container does not clone)
 
void copySuperpixels (const cv::Mat &superpixels)
 copy 32-bit oversegmentation (container clones argument)
 
cv::Mat mask (unsigned segId) const
 return 8-bit mask for given superpixel
 
cv::Mat & mask (unsigned segId, cv::Mat &m) const
 return 8-bit mask for given superpixel (use supplied matrix if given)
 
set< unsigned > parents (int x, int y) const
 returns set of superpixel indices to which the given pixel belongs
 
set< unsigned > neighbours (unsigned segId) const
 returns all overlapping or 8-connected superpixels to a given superpixel
 
const cv::Rect & boundingBox (unsigned segId) const
 returns the bounding box for a superpixel
 
cv::Point centroid (unsigned segId) const
 calculates and returns centroid for a superpixel
 
cv::Mat intersection () const
 return 32-bit intersection of superpixels
 
int removeSmallSuperpixels (unsigned minSize=2)
 filters small superpixels and returns number of superpixels removed
 
int removeUnmaskedSuperpixels (const cv::Mat &mask, double areaOverlap=0.5)
 filters superpixels that do not overlap with supplied mask
 
int removeSuperpixels (const std::set< unsigned > &segIds)
 filters superpixels matching the given set of segIds
 
cv::Mat visualize (const cv::Mat &img, bool bColourById=false) const
 visualize superpixels
 
cv::Mat visualize (const cv::Mat &img, const vector< cv::Scalar > &colors, double alpha=1.0) const
 visualize superpixels with specific colours
 
drwnSuperpixelContaineroperator= (const drwnSuperpixelContainer &container)
 assignment operator
 
const cv::Mat & operator[] (unsigned int indx) const
 access the i-th superpixel map as CV_32SC1
 

Protected Attributes

vector< int > _start
 start index for each _map
 
vector< int > _nsegs
 number of segments in each _map
 
vector< cv::Mat > _maps
 superpixel maps (-1 if pixel is not part of a map)
 
vector< int > _pixels
 size of each superpixel
 
vector< cv::Rect > _bboxes
 bounding boxes for each superpixel
 

Detailed Description

Holds multiple oversegmentations for a given image.

The container is populated with segment maps with the same dimensions as the image. A map is an integer array, each entry corresponding to a pixel in the image. Maps can define more than one segment/superpixel. A segment is defined as all pixels (in a map) labeled with the same non-negative integer. Pixels labeled with negative integers are ignored. Maps are renumbered when added to the container. Member functions allow you to retrieve the mask of each segment/superpixel or the (renumbered) maps.

The following code snippet shows how the container can be used to iterate through different superpixels:

// load an image three different oversegmentations
cv::Mat img = cv::imread("image.jpg");
container.loadSuperpixels("oversegmentation_1.txt");
container.loadSuperpixels("oversegmentation_2.txt");
container.loadSuperpixels("oversegmentation_3.txt");
// iterate through the superpixels and display them
cv::Mat segMask;
for (int sedId = 0; segId < container.size(); segId++) {
// get segmentation mask for the superpixel
segMask = container.mask(segId, segMask);
// draw a white and red boundary around the superpixel on a temporary image
cv::Mat canvas = img.clone();
drwnDrawRegionBoundaries(canvas, segMask, CV_RGB(255, 255, 255), 3);
drwnDrawRegionBoundaries(canvas, segMask, CV_RGB(255, 0, 0), 1);
// show the image and superpixel
drwnShowDebuggingImage(canvas, string("superpixel"), true);
}

The class provides a drwnPersistentStorage interface. Superpixel region data is stored run-length encoded in binary format. This is useful for caching superpixel calculations. The following code snippet shows an example of this functionality.

// load an image three different oversegmentations
cv::Mat img = cv::imread("image.jpg");
if (drwnFileExists("cache.bin")) {
// load cached superpixels
ifstream ifs("cache.bin", ios::binary);
container.read(ifs);
ifs.close();
} else {
// compute superpixels
container.addSuperpixels(drwnFastSuperpixels(img, 32));
container.addSuperpixels(drwnFastSuperpixels(img, 16));
// cache to file
ofstream ofs("cache.bin", ios::binary);
container.write(ofs);
ofs.close();
}

The superpixels in stored drwnSuperpixelContainer objects can be accessed in Matlab via the mexLoadSuperpixels and mexSaveSuperpixels application wrappers.


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