KSquare Utilities
SegmentorOTSU.h
Go to the documentation of this file.
1 /* Raster.h -- Class that one raster image.
2  * Copyright (C) 1994-2014 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #if !defined(_SEGMENTOROTSU_)
6 #define _SEGMENTOROTSU_
7 
8 #include "KKBaseTypes.h"
9 #include "Matrix.h"
10 #include "KKStr.h"
11 #include "Raster.h"
12 #include "RunLog.h"
13 
14 
15 namespace KKB
16 {
17  /**
18  *@class SegmentorOTSU
19  *@author Kurt Kramer
20  *@details
21  *@code
22  **************************************************************************************
23  ** SegmentorOTSU *
24  ** *
25  ** Will segment an image using an implementation of OTSU. Based on MatLab code I *
26  ** found on the Internet. http://www.biomecardio.com/matlab/otsu.html *
27  ** by Otsu N, *
28  ** <a href="matlab:web('http://dx.doi.org/doi:10.1109/TSMC.1979.4310076')"> *
29  ** A Threshold Selection Method from Gray-Level Histograms</a>, *
30  ** IEEE Trans. Syst. Man Cybern. 9:62-66;1979 *
31  **************************************************************************************
32  *@endcode
33  *@sa raster
34  */
35 
37  {
38  public:
39  SegmentorOTSU (RunLog& _log);
40  ~SegmentorOTSU ();
41 
42  /**
43  *@brief Segments image into 'numClasses'.
44  *@param[in] srcImage Image to segment. If it is a color image will be
45  * converted to Gray-Scale using 'CreateGrayScaleKLTOnMaskedArea'
46  *@param[in] numClasses Number of classes to segment image into. Current only '2' and '3' are supported.
47  *@param[out] sep
48  *@return Labeled gray-scale image where pixels will be labels into their respective class; between '1' and 'numClasses'.
49  */
50  RasterPtr SegmentImage (RasterPtr srcImage,
51  kkint32 numClasses,
52  double& sep
53  );
54 
55 
56  /**
57  *@brief Segments image into 'numClasses' taking into account only pixels
58  * indicated by 'mask' image.
59  *@param[in] srcImage Image to segment. If it is a color image will be
60  * converted to Gray-Scale using 'CreateGrayScaleKLTOnMaskedArea'
61  *@param[in] mask Indicates which pixels to consider when thresholding image. Pixels
62  * that are not part of mask will be assigned label '0'.
63  *@param[in] numClasses Number of classes to segment image into. Current only '2' and '3' are supported.
64  *@param[out] sep
65  *@return Labeled gray-scale image where pixels will be label into their respective class; between '1' and 'numClasses'.
66  */
67  RasterPtr SegmentMaskedImage (RasterPtr srcImage,
68  RasterPtr mask,
69  kkint32 numClasses,
70  double& sep
71  );
72 
73 
74  /**
75  *@brief Will compute the average RGB values of the region indicated by the segmented image.
76  *@param[in] origImage The original image that 'SegmentImage' was performed on.
77  *@param[in] segmentedImage The segmented image that was returned by either 'SegmentImage' or 'SegmentMaskedImage'.
78  *@param[in] segmentedClass The class in 'segmentedImage' that we want to compute average RGB for.
79  */
80  PixelValue ClassAverageRGB (const RasterPtr origImage,
81  const RasterPtr segmentedImage,
82  uchar segmentedClass
83  );
84 
85 
86  /**
87  *@brief Determines which class in the segmented image is closet in RGB color space to the specified target color.
88  *@param[in] origImage The original image that 'SegmentImage' was performed on.
89  *@param[in] segmentedImage The segmented image that was returned by either 'SegmentImage' or 'SegmentMaskedImage'.
90  *@param[in] targetColor RGB Color that we want the chosen class to close to.
91  *@returns The class that has the average RGB value closest to targetColor.
92  */
93  uchar GetClassClosestToTargetColor (const RasterPtr origImage,
94  const RasterPtr segmentedImage,
95  const PixelValue& targetColor
96  );
97 
98  uchar Threshold1 () const {return threshold1;}
99  uchar Threshold2 () const {return threshold2;}
100 
101 
102  private:
103  void LabelRaster (RasterPtr result,
104  uchar pixelValue,
105  uchar label,
106  RasterPtr srcImage
107  );
108 
109  void LabelRaster (RasterPtr result,
110  RasterPtr mask,
111  uchar pixelValue,
112  uchar label,
113  RasterPtr srcImage
114  );
115 
116  double sig_func (VectorDouble k,
117  kkint32 nbins,
118  const VectorDouble& P,
119  kkint32 numClasses
120  );
121 
122  template<typename T>
123  vector<T> BDV (T start,
124  T inc,
125  T end
126  );
127 
128 
129  VectorDouble Add (const VectorDouble& left,
130  double right
131  );
132 
133 
134  template<typename T>
135  vector<T> CumSum (const vector<T>& v);
136 
137 
138  VectorDouble DotMult (const VectorDouble& left,
139  const VectorDouble& right
140  );
141 
142  Matrix DotMult (const Matrix& left,
143  const Matrix& right
144  );
145 
146 
147  VectorDouble DotDiv (const VectorDouble& left,
148  const VectorDouble& right
149  );
150 
151  Matrix DotDiv (const Matrix& left,
152  const Matrix& right
153  );
154 
155  template<typename T>
156  vector<T> FlipLeftRight (const vector<T>& v);
157 
158  VectorDouble LinSpace (double start,
159  double end,
160  kkint32 numPoints
161  );
162 
163  void MakeNanWhenLesOrEqualZero (Matrix& m);
164 
165  void NdGrid (const VectorDouble& x,
166  const VectorDouble& y,
167  Matrix& xm,
168  Matrix& ym
169  );
170 
171 
172  VectorDouble Power (const VectorDouble& left,
173  double right
174  );
175 
176  Matrix Power (const Matrix& left,
177  double right
178  );
179 
180  VectorDouble Round (const VectorDouble& v);
181 
182  VectorDouble Subt (const VectorDouble& left,
183  const VectorDouble& right
184  );
185 
186  VectorDouble Subt (const VectorDouble& left,
187  double right
188  );
189 
190  VectorDouble Subt (double left,
191  const VectorDouble& right
192  );
193 
194  template<typename T>
195  vector<T> SubSet (const vector<T>& P,
196  kkint32 start,
197  kkint32 end
198  );
199 
200  template<typename T>
201  T Sum (const vector<T>& v);
202 
203  template<typename T>
204  T SumSubSet (const vector<T>& P,
205  kkint32 start,
206  kkint32 end
207  );
208 
209  template<typename T>
210  void ZeroOutNaN (vector<T>& v);
211 
212  void ZeroOutNaN (Matrix& m);
213 
214 
215  //VectorDouble PWD
216 
217  uchar threshold1;
218  uchar threshold2;
219  double NaN;
220  RunLog& log;
221  }; /* SegmentorOTSU */
222 } /* KKB */
223 
224 
225 
226 #endif
__int32 kkint32
Definition: KKBaseTypes.h:88
uchar Threshold1() const
Definition: SegmentorOTSU.h:98
PixelValue ClassAverageRGB(const RasterPtr origImage, const RasterPtr segmentedImage, uchar segmentedClass)
Will compute the average RGB values of the region indicated by the segmented image.
Supports two dimensional matrices.
Definition: Matrix.h:46
SegmentorOTSU(RunLog &_log)
uchar GetClassClosestToTargetColor(const RasterPtr origImage, const RasterPtr segmentedImage, const PixelValue &targetColor)
Determines which class in the segmented image is closet in RGB color space to the specified target co...
KKTHread * KKTHreadPtr
uchar Threshold2() const
Definition: SegmentorOTSU.h:99
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
RasterPtr SegmentImage(RasterPtr srcImage, kkint32 numClasses, double &sep)
Segments image into &#39;numClasses&#39;.
Used for logging messages.
Definition: RunLog.h:49
Used by the Raster Class to represent the contents of one pixel.
Definition: PixelValue.h:22
std::vector< double > VectorDouble
Vector of doubles.
Definition: KKBaseTypes.h:148
RasterPtr SegmentMaskedImage(RasterPtr srcImage, RasterPtr mask, kkint32 numClasses, double &sep)
Segments image into &#39;numClasses&#39; taking into account only pixels indicated by &#39;mask&#39; image...