OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimKMeansFilter.h
Go to the documentation of this file.
1 //**************************************************************************************************
2 //
3 // OSSIM Open Source Geospatial Data Processing Library
4 // See top level LICENSE.txt file for license information
5 //
6 //**************************************************************************************************
7 #ifndef ossimKMeansFilter_HEADER
8 #define ossimKMeansFilter_HEADER
9 
12 #include <vector>
13 
14 class ossimImageData;
16 class ossimHistogram;
17 
18 /***************************************************************************************************
19  *
20  * This class provides a pixel classification capability using the traditional K-means scheme for
21  * partitioning datasets into K distinct clusters according to pixel value. The output tile consists
22  * of the input pixels remapped according to their classification to a digital number representing
23  * the cluster. Null pixels are ignored and left null.
24  *
25  * The caller can set the digital values representing the classification clusters. Otherwise they are
26  * mapped to integers 1, 2, ..., K.
27  *
28  * The output pixel type will be the minimum necessary to hold K clusters + null pixel. Typically
29  * this would be UInt8. Multiple bands are treated separately, so the number of output bands is
30  * the same as the number of input bands.
31  *
32  * This filter requires a histogram for the input source. If none is provided, one is computed.
33  *
34  * A common use for this class is for thresholding an image based on the histogram. This corresponds
35  * to K=2. The threshold point can be left at the default boundary between the two clusters, or
36  * alternatively, can be set to some standard deviation departure from one of the cluster's mean.
37  * See setThreshold() for this specific use case.
38  *
39  * Important Note:
40  * The histogram provided (or computed) should reflect all of the pixels that will be processed
41  * over multiple calls to getTile(). In other words, it doesn't compute the clustering on
42  * a per-tile basis, since that would cause each tile running through to use a different
43  * classification criteria. Therefore, it is important that if a sub-image AOI is being used by the
44  * output sink, the cut-rect filter needs to be inserted on the input-side of this filter so that it
45  * only considers pixels in the AOI. Alternatively, the AOI (in pixel coordinates) can be specified
46  * to this class directly and only those pixels in the AOI will be used in computing a histogram
47  * for use in the clustering.
48  *
49  **************************************************************************************************/
51 {
52 public:
53  enum ThresholdMode { NONE=0, MEAN=1, SIGMA_WEIGHTED=2, VARIANCE_WEIGHTED=3 };
54 
56 
60  ossimKMeansFilter(ossimImageSource* input_source, ossimMultiBandHistogram* histogram=0);
61 
63 
64  virtual ossimRefPtr<ossimImageData> getTile(const ossimIrect& origin, ossim_uint32 resLevel=0);
65 
66  virtual ossimScalarType getOutputScalarType() const;
67 
71  void setInputHistogram(ossimMultiBandHistogram* histo);
72 
78  void setNumClusters(ossim_uint32 K);
79 
86  void setClusterPixelValues(const ossim_uint32* dns, ossim_uint32 K);
87 
99  void setThresholdMode(ThresholdMode mode);
100 
105  const ossimKMeansClustering* getBandClassifier(ossim_uint32 band=0) const;
106 
107  virtual double getMinPixelValue(ossim_uint32 band=0)const;
108  virtual double getMaxPixelValue(ossim_uint32 band=0)const;
109 
110  virtual void initialize();
111 
112  virtual bool saveState(ossimKeywordlist& kwl, const char* prefix=NULL)const;
113  virtual bool loadState(const ossimKeywordlist& kwl, const char* prefix=NULL);
114 
115 protected:
116  bool computeKMeans();
117 
121  void allocate();
122  void clear();
123 
124  std::vector<ossimRefPtr<ossimKMeansClustering> > m_classifiers;
127  std::vector<ossim_uint32> m_pixelValues;
128  std::vector<double> m_minPixelValue;
129  std::vector<double> m_maxPixelValue;
134  std::vector<double> m_thresholds;
135 
136 TYPE_DATA
137 };
138 
139 #endif /* #ifndef ossimKMeansFilter_HEADER */
ossimScalarType m_outputScalarType
Represents serializable keyword/value map.
ossim_uint32 m_numClusters
std::vector< double > m_thresholds
std::vector< ossimRefPtr< ossimKMeansClustering > > m_classifiers
virtual double getMinPixelValue(ossim_uint32 band=0) const
Returns the min pixel of the band.
#define TYPE_DATA
Definition: ossimRtti.h:339
unsigned int ossim_uint32
ossimRefPtr< ossimMultiBandHistogram > m_histogram
Have num_bands entries.
std::vector< double > m_minPixelValue
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of an object from a keyword list.
std::vector< double > m_maxPixelValue
ThresholdMode m_thresholdMode
ossimScalarType
virtual ossimScalarType getOutputScalarType() const
This will be used to query the output pixel type of the tile source.
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.
virtual double getMaxPixelValue(ossim_uint32 band=0) const
Returns the max pixel of the band.
ossimRefPtr< ossimImageData > m_tile
#define OSSIM_DLL
std::vector< ossim_uint32 > m_pixelValues
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)