KSquare Utilities
GrayScaleImagesFV.h
Go to the documentation of this file.
1 #ifndef _LASRCOSFEATUREVECTOR_
2 #define _LASRCOSFEATUREVECTOR_
3 
4 /**
5  *@class KKMLL::GrayScaleImagesFV
6  *@brief Specialized version of KKMLL::FeatureVector that will be used
7  *to represent the features of a Shrimp.
8  *@author Kurt Kramer
9  *@details
10  * Used for the representation of a Single Plankton Image. You create an instance of this object for
11  * each single image you need to keep track of. There is a specialized version of KKMLL::FeatureFileIO
12  * calculated KKMLL::FeatureFileIOKK that is used to write and read feature Data files. What makes this
13  * class of KKMLL::FeatureVector special are the additional fields that are Plankton specific such as
14  * centroidCol, centroidRow, latitude, longitude, numOfEdgePixels, centroid within SIPPEER file
15  * sfCentroidCol, sfCentroidRow and version.<p>
16  *
17  * The version number field is supposed to indicate which feature calculation routines were used. This
18  * way if there are changes the way features are calculated it can be detected during runtime if the features
19  * are up to date they need to be recomputed.
20 */
21 
22 
23 #include "BMPImage.h"
24 #include "KKQueue.h"
25 #include "Raster.h"
26 #include "RunLog.h"
27 #include "KKStr.h"
28 using namespace KKB;
29 
30 
31 //#include "FeatureNumList.h"
32 #include "FactoryFVProducer.h"
33 #include "FeatureVector.h"
34 #include "FileDesc.h"
35 #include "MLClass.h"
36 using namespace KKMLL;
37 
38 
39 namespace KKMLL
40 {
41  #ifndef _FEATURENUMLIST_
42  class FeatureNumList;
44  #endif
45 
46  #ifndef _MLCLASS_
47  class MLClass;
48  typedef MLClass* MLClassPtr;
49  class MLClassList;
50  typedef MLClassList* MLClassListPtr;
51  #endif
52 
53 
55  {
56  public:
58  typedef KKB::uchar uchar;
59 
60  GrayScaleImagesFV (kkint32 _numOfFeatures);
61 
63 
64 
65 
66  /**
67  *@brief Smart copy constructor that will detect the underlying type of the source instance.
68  *@details
69  *@code
70  ** This constructor will detect what the underlying type of 'featureVector' is.
71  ** If (underlying type is a 'GrayScaleImagesFV' object) then
72  ** | Information that is particular to a 'GrayScaleImagesFV' object will be extracted
73  ** | from the 'FeatureVector' object.
74  ** else
75  ** | Info that is particular to a 'GrayScaleImagesFV' object will be set to default
76  ** | values.
77  *@endcode
78  */
79  GrayScaleImagesFV (const FeatureVector& featureVector);
80 
81  virtual ~GrayScaleImagesFV ();
82 
83  virtual GrayScaleImagesFVPtr Duplicate () const;
84 
85  // Access Methods.
86  void CentroidCol (float _centroidCol) {centroidCol = _centroidCol;}
87  void CentroidRow (float _centroidRow) {centroidRow = _centroidRow;}
88  void NumOfEdgePixels (kkint32 _numOfEdgePixels) {numOfEdgePixels = _numOfEdgePixels;}
89 
90 
91  float CentroidCol () const {return centroidCol;} // Centroid with respect to image
92  float CentroidRow () const {return centroidRow;} // "" "" "" "" ""
93  kkint32 NumOfEdgePixels () const {return numOfEdgePixels;}
94 
95 
96  private:
97  static RasterListPtr calcImages;
98 
99  float centroidCol; /**< centroid Column with just respect to image. */
100  float centroidRow; /**< centroid Row with just respect to image. */
101  kkint32 numOfEdgePixels;
102 
103  }; /* GrayScaleImagesFV */
104 
105 
106  typedef GrayScaleImagesFV::GrayScaleImagesFVPtr GrayScaleImagesFVPtr;
107 
108 #define _GrayScaleImagesFV_Defined_
109 
110 
111 
113  {
114  public:
116 
117  GrayScaleImagesFVList (FileDescPtr _fileDesc,
118  bool _owner
119  );
120 
121  private:
122  /**
123  *@brief Will create a duplicate List of examples, in the same order. If the source
124  * 'examples' owns its entries, then new duplicate entries will be created, and will
125  * own them otherwise will only get pointers to existing instances in 'examples'.
126  */
127  GrayScaleImagesFVList (const GrayScaleImagesFVList& examples);
128 
129 
130  public:
131 
132  /**
133  *@brief Creates a duplicate List of examples, in the same order, and depending on '_owner' will
134  * create new instances or just point to the existing one.
135  *@details
136  *@code
137  * If '_owner' = true
138  * Create new instances of contents and own them.
139  * else if 'owner' = false,
140  * Copy over pointers to existing instances.
141  *@endcode
142  *@param[in] _examples The list of 'GrayScaleImagesFV' that is to be copied.
143  *@param[in] _owner If 'true' new instances of 'GrayScaleImagesFV' instances will be created
144  * and this new list will own them and if 'false' will just point to
145  * the existing instances and not own the.
146  */
148  bool _owner
149  );
150 
151 
152  /**
153  *@brief Creates a duplicate List of examples, in the same order, and depending on '_owner' will
154  * create new instances or just point to the existing one.
155  *@details
156  *@code
157  * If '_owner' = true
158  * Create new instances of contents and own them.
159  * else if 'owner' = false,
160  * Copy over pointers to existing instances.
161  *@endcode
162  * If any of the existing instances do not have an underlying class of GrayScaleImagesFV;
163  * the function will throw an exception.
164  *
165  *@param[in] _examples The list of 'GrayScaleImagesFV' that is to be copied.
166  *@param[in] _owner If 'true' new instances of 'GrayScaleImagesFV' instances will be created
167  * and this new list will own them and if 'false' will just point to
168  * the existing instances and not own the.
169  */
170  GrayScaleImagesFVList (const FeatureVectorList& featureVectorList,
171  bool _owner
172  );
173 
174 
175  /**
176  *@brief Constructor that will extract a list of feature vectors for all the image files in the
177  * specified directory.
178  *@details
179  * Will scan the directory _dirName for any image files. For each image found a new instance of GrayScaleImagesFV
180  * will be created who's features will be derived from the image. These GrayScaleImagesFV' objects will be
181  * assigned the class specified by '_mlClass'. A new data file containing the extracted features will be
182  * saved in fileName.
183  *
184  *@param[in] _fvProducerFactory
185  *@param[in] _mlClass Class to assign to new 'GrayScaleImagesFV' objects.
186  *@param[in] _dirName Directory to scan for examples.
187  *@param[in] _fileName Name of file to contain the extracted feature data. Will be of the Raw format.
188  *@param[in] _log Log file to write messages to.
189  */
190  GrayScaleImagesFVList (FactoryFVProducerPtr _fvProducerFactory,
191  MLClassPtr _mlClass,
192  KKStr _dirName,
193  KKStr _fileName,
194  RunLog& _log
195  );
196 
197 
198 
199  /**
200  *@brief constructor that will create a list of examples from _examples that are assigned one of the
201  * classes listed in _mlClasses.
202  *@details
203  * Will Create a list of examples that are a subset of the ones in _examples. The subset will
204  * consist of the examples who's mlClass is one of the ones in mlClasses. We will not own
205  * any the contents only point to the ones already in _examples.
206  *@param[in] _mlClasses List of classes that we are interested in.
207  *@param[in] _examples Source examples that we want to scan.
208  */
209  GrayScaleImagesFVList (MLClassList& _mlClasses,
210  GrayScaleImagesFVList& _examples
211  );
212 
213 
214  /**
215  @brief
216  @details
217  This constructor is meant to create a list of 'GrayScaleImagesFV' objects from the FeatureVector
218  objects contained in featureVectorList.
219  @code
220  If 'featureVectorList' owns its contents (that is 'featureVectorList.Owner () == true' then
221  | We will create new Instances of 'GrayScaleImagesFV' objects that we will own.
222  | The underlying class of the 'FeatureVector' objects will be converted to a
223  | 'GrayScaleImagesFV' class.
224  else
225  | all the 'FeatureVector' objects in 'featureVectorList' must have an underlying class of
226  | 'GrayScaleImagesFV'. If one or more do not then the program will halt with a message to
227  | the log.
228  @endcode
229  */
230  GrayScaleImagesFVList (const FeatureVectorList& featureVectorList);
231 
232 
233 
234 
235  virtual ~GrayScaleImagesFVList ();
236 
237 
238  void AddQueue (GrayScaleImagesFVList& imagesToAdd);
239 
240  GrayScaleImagesFVPtr BackOfQueue ();
241 
242  GrayScaleImagesFVPtr BinarySearchByName (const KKStr& _imageFileName) const;
243 
244  virtual
245  GrayScaleImagesFVListPtr Duplicate (bool _owner) const;
246 
247  virtual
249 
251 
253  kkint32 _maxToExtract = -1,
254  float _minSize = -1.0f
255  ) const;
256 
257 
258  GrayScaleImagesFVPtr IdxToPtr (kkint32 idx) const;
259 
260  GrayScaleImagesFVPtr LookUpByImageFileName (const KKStr& _imageFileName) const;
261 
262  GrayScaleImagesFVPtr LookUpByRootName (const KKStr& _rootName);
263 
264  virtual
266 
267 
268  /**
269  *@brief Using list of ImageFileNames in a file('fileName') create a new GrayScaleImagesFVList instance
270  * with examples in order based off contents of file. If error occurs will return NULL.
271  */
273  RunLog& log
274  );
275 
276  GrayScaleImagesFVPtr PopFromBack ();
277 
278  void RecalcFeatureValuesFromImagesInDirTree (FactoryFVProducerPtr fvProducerFactory,
279  const KKStr& rootDir,
280  bool& successful,
281  RunLog& log
282  );
283 
285  RunLog& log
286  );
287 
288 
290  kkint32 maxImagesPerClass,
291  kkint32 numOfFolds,
292  RunLog& log
293  );
294 
295 
297  {
298  private:
299  FeatureVectorList::const_iterator idx;
300 
301  public:
303  idx ()
304  {
305  }
306 
307 
308  const_iterator (const const_iterator& ivConst_Iterator):
310  {
311  }
312 
313 
316  {
317  }
318 
319 
321  idx (fvIterator)
322  {
323  }
324 
325 
326  const GrayScaleImagesFVPtr operator*()
327  {
328  return (const GrayScaleImagesFVPtr)*idx;
329  }
330 
331 
333  {
334  idx = right.idx;
335  return *this;
336  }
337 
338 
339 
341  {
342  idx = right;
343  return *this;
344  }
345 
346 
348  {
349  idx = right;
350  return *this;
351  }
352 
353 
354  bool operator!= (const const_iterator& right) const
355  {
356  return idx != right.idx;
357  }
358 
359 
360  bool operator!= (const FeatureVectorList::iterator& right) const
361  {
363  }
364 
365 
367  {
368  return idx != right;
369  }
370 
371 
372  bool operator== (const const_iterator& right) const
373  {
374  return idx == right.idx;
375  }
376 
377 
378  bool operator== (const FeatureVectorList::iterator& right) const
379  {
380  return (idx == right);
381  }
382 
383 
385  {
386  idx++;
387  return *this;
388  }
389  }; /* const_iterator */
390 
391 
392 
393 
394 
395  class iterator
396  {
397  private:
398  FeatureVectorList::iterator idx;
399 
400  public:
402  idx ()
403  {
404  }
405 
406  iterator (const iterator& idx):
407  idx (idx.idx)
408  {
409  }
410 
411 
413  idx (idx)
414  {
415  }
416 
417 
418  GrayScaleImagesFVPtr operator*()
419  {
420  return (GrayScaleImagesFVPtr)*idx;
421  }
422 
423  iterator& operator= (const iterator& right)
424  {
425  idx = right.idx;
426  return *this;
427  }
428 
429  bool operator!= (const iterator& right) const
430  {
431  return idx != right.idx;
432  }
433 
434  bool operator== (const iterator& right) const
435  {
436  return idx == right.idx;
437  }
438 
439  iterator& operator++ (int x)
440  {
441  idx++;
442  return *this;
443  }
444  };
445 
446  private:
447  void FeatureExtraction (FactoryFVProducerPtr _fvProducerFactory,
448  KKStr _dirName,
449  KKStr _fileName,
450  MLClassPtr _mlClass,
451  RunLog& _log
452  );
453 
454 
455 
456  }; /* GrayScaleImagesFVList */
457 
458 
459  typedef GrayScaleImagesFVList::GrayScaleImagesFVListPtr GrayScaleImagesFVListPtr;
460 
461 #define _GrayScaleImagesFVList_Defined_
462 
463 
464 } /* namespace LarcosCounterUnManaged */
465 
466 #endif
GrayScaleImagesFV(const GrayScaleImagesFV &_fv)
GrayScaleImagesFVList(FileDescPtr _fileDesc, bool _owner)
MLClass * MLClassPtr
Definition: MLClass.h:46
GrayScaleImagesFVListPtr StratifyAmoungstClasses(kkint32 numOfFolds, RunLog &log)
const_iterator(const const_iterator &ivConst_Iterator)
iterator & operator=(const iterator &right)
GrayScaleImagesFVListPtr ExtractDuplicatesByRootImageFileName()
__int32 kkint32
Definition: KKBaseTypes.h:88
GrayScaleImagesFVList(const FeatureVectorList &featureVectorList, bool _owner)
Creates a duplicate List of examples, in the same order, and depending on &#39;_owner&#39; will create new in...
GrayScaleImagesFVPtr PopFromBack()
void AddQueue(GrayScaleImagesFVList &imagesToAdd)
Keeps track of selected features.
GrayScaleImagesFVPtr LookUpByImageFileName(const KKStr &_imageFileName) const
GrayScaleImagesFVList(MLClassList &_mlClasses, GrayScaleImagesFVList &_examples)
constructor that will create a list of examples from _examples that are assigned one of the classes l...
GrayScaleImagesFVPtr LookUpByRootName(const KKStr &_rootName)
GrayScaleImagesFVList(const GrayScaleImagesFVList &_examples, bool _owner)
Creates a duplicate List of examples, in the same order, and depending on &#39;_owner&#39; will create new in...
GrayScaleImagesFV(const FeatureVector &featureVector)
Smart copy constructor that will detect the underlying type of the source instance.
GrayScaleImagesFV * GrayScaleImagesFVPtr
virtual GrayScaleImagesFVListPtr ManufactureEmptyList(bool _owner) const
Creates an instance of a Empty FeatureVectorList.
bool operator==(const const_iterator &right) const
virtual GrayScaleImagesFVListPtr DuplicateListAndContents() const
Creates a duplicate of list and also duplicates it contents.
Container class for FeatureVector derived objects.
KKTHread * KKTHreadPtr
GrayScaleImagesFVList(FactoryFVProducerPtr _fvProducerFactory, MLClassPtr _mlClass, KKStr _dirName, KKStr _fileName, RunLog &_log)
Constructor that will extract a list of feature vectors for all the image files in the specified dire...
RasterList * RasterListPtr
Definition: Raster.h:75
bool operator==(const FeatureVectorList::iterator &right) const
GrayScaleImagesFVPtr BinarySearchByName(const KKStr &_imageFileName) const
Specialized version of KKMLL::FeatureVector that will be used to represent the features of a Shrimp...
FeatureNumList * FeatureNumListPtr
bool operator!=(const const_iterator &right) const
void CentroidCol(float _centroidCol)
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
GrayScaleImagesFVPtr BackOfQueue()
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
void CentroidRow(float _centroidRow)
const_iterator & operator=(const const_iterator &right)
void NumOfEdgePixels(kkint32 _numOfEdgePixels)
GrayScaleImagesFVListPtr OrderUsingNamesFromAFile(const KKStr &fileName, RunLog &log)
Using list of ImageFileNames in a file(&#39;fileName&#39;) create a new GrayScaleImagesFVList instance with e...
GrayScaleImagesFV(kkint32 _numOfFeatures)
GrayScaleImagesFVListPtr ExtractExamplesForAGivenClass(MLClassPtr _mlClass, kkint32 _maxToExtract=-1, float _minSize=-1.0f) const
Used for logging messages.
Definition: RunLog.h:49
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
MLClassList * MLClassListPtr
Definition: MLClass.h:49
GrayScaleImagesFVList * GrayScaleImagesFVListPtr
virtual GrayScaleImagesFVPtr Duplicate() const
iterator(const FeatureVectorList::iterator &idx)
virtual GrayScaleImagesFVListPtr Duplicate(bool _owner) const
Creates a duplicate of list using the same container.
kkint32 NumOfEdgePixels() const
Maintains a list of MLClass instances.
Definition: MLClass.h:233
Represents a Feature Vector of a single example, labeled or unlabeled.
Definition: FeatureVector.h:59
GrayScaleImagesFVPtr IdxToPtr(kkint32 idx) const
bool operator!=(const iterator &right) const
GrayScaleImagesFVList(const FeatureVectorList &featureVectorList)
bool operator==(const iterator &right) const
GrayScaleImagesFVListPtr StratifyAmoungstClasses(MLClassListPtr mlClasses, kkint32 maxImagesPerClass, kkint32 numOfFolds, RunLog &log)
void RecalcFeatureValuesFromImagesInDirTree(FactoryFVProducerPtr fvProducerFactory, const KKStr &rootDir, bool &successful, RunLog &log)