Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnSuperpixelContainer.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: drwnSuperpixelContainer.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <vector>
16 #include <set>
17 
18 #include "cv.h"
19 
20 #include "drwnBase.h"
21 #include "drwnIO.h"
22 
23 using namespace std;
24 
25 // drwnSuperpixelContainer -----------------------------------------------------
90 
92  protected:
93  vector<int> _start;
94  vector<int> _nsegs;
95  vector<cv::Mat> _maps;
96  vector<int> _pixels;
97  vector<cv::Rect> _bboxes;
98 
99  public:
105 
107  void clear();
109  inline bool empty() const { return _maps.empty(); }
111  inline int size() const { return _maps.empty() ? 0 : _start.back() + _nsegs.back(); }
113  inline int pixels(unsigned segId) const { return _pixels[segId]; }
115  inline int width() const { return _maps.empty() ? 0 : _maps[0].cols; }
117  inline int height() const { return _maps.empty() ? 0 : _maps[0].rows; }
119  inline int channels() const { return (int)_maps.size(); }
121  inline size_t memory() const {
122  return _maps.size() * width() * height() * sizeof(int) + 3 * size() * sizeof(int);
123  }
124 
125  // persistent storage interface
126  virtual size_t numBytesOnDisk() const;
127  virtual bool write(ostream& os) const;
128  virtual bool read(istream& is);
129 
131  void loadSuperpixels(const char *filename);
133  void addSuperpixels(cv::Mat superpixels);
135  void copySuperpixels(const cv::Mat& superpixels);
136 
138  cv::Mat mask(unsigned segId) const;
140  cv::Mat& mask(unsigned segId, cv::Mat& m) const;
141 
143  set<unsigned> parents(int x, int y) const;
145  set<unsigned> neighbours(unsigned segId) const;
147  const cv::Rect& boundingBox(unsigned segId) const { return _bboxes[segId]; }
149  cv::Point centroid(unsigned segId) const;
150 
152  cv::Mat intersection() const;
153 
155  int removeSmallSuperpixels(unsigned minSize = 2);
157  int removeUnmaskedSuperpixels(const cv::Mat& mask, double areaOverlap = 0.5);
159  int removeSuperpixels(const std::set<unsigned>& segIds);
160 
162  cv::Mat visualize(const cv::Mat& img, bool bColourById = false) const;
164  cv::Mat visualize(const cv::Mat& img, const vector<cv::Scalar>& colors, double alpha = 1.0) const;
165 
167  drwnSuperpixelContainer& operator=(const drwnSuperpixelContainer& container);
169  const cv::Mat& operator[](unsigned int indx) const { return _maps[indx]; }
170 };
171 
172 
173 
double areaOverlap(const cv::Rect &r, const cv::Rect &s)
area of the intersection between two rectangles
Definition: drwnOpenCVUtils.cpp:200
int pixels(unsigned segId) const
returns the number of pixels within the given superpixel
Definition: drwnSuperpixelContainer.h:113
const cv::Mat & operator[](unsigned int indx) const
access the i-th superpixel map as CV_32SC1
Definition: drwnSuperpixelContainer.h:169
int size() const
returns the number of superpixels
Definition: drwnSuperpixelContainer.h:111
int height() const
returns the image height
Definition: drwnSuperpixelContainer.h:117
bool empty() const
returns true if their are no superpixels
Definition: drwnSuperpixelContainer.h:109
vector< int > _nsegs
number of segments in each _map
Definition: drwnSuperpixelContainer.h:94
vector< int > _pixels
size of each superpixel
Definition: drwnSuperpixelContainer.h:96
Holds multiple oversegmentations for a given image.
Definition: drwnSuperpixelContainer.h:91
vector< cv::Rect > _bboxes
bounding boxes for each superpixel
Definition: drwnSuperpixelContainer.h:97
const cv::Rect & boundingBox(unsigned segId) const
returns the bounding box for a superpixel
Definition: drwnSuperpixelContainer.h:147
vector< cv::Mat > _maps
superpixel maps (-1 if pixel is not part of a map)
Definition: drwnSuperpixelContainer.h:95
int channels() const
returns the maximum number of superpixels to which any pixel can belong
Definition: drwnSuperpixelContainer.h:119
Interface class for drwnPersistentStorage.
Definition: drwnPersistentStorage.h:25
int width() const
returns the image width
Definition: drwnSuperpixelContainer.h:115
size_t memory() const
returns number of bytes stored (approximately)
Definition: drwnSuperpixelContainer.h:121
vector< int > _start
start index for each _map
Definition: drwnSuperpixelContainer.h:93