KSquare Utilities
GrayScaleImagesFVProducer.cpp
Go to the documentation of this file.
1 /* CameraAcquisition.cpp -- Base class for the Acquisition threads.
2  * Copyright (C) 2011-2013 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in LarcosCounterUnManaged.txt
4  */
5 #include "FirstIncludes.h"
6 
7 #include <errno.h>
8 #include <istream>
9 #include <iostream>
10 #include <queue>
11 #include <map>
12 #include <vector>
13 #if defined(WIN32)
14 #include <windows.h>
15 #endif
16 #include "MemoryDebug.h"
17 using namespace std;
18 
19 #include "Blob.h"
20 #include "ConvexHull.h"
21 #include "KKBaseTypes.h"
22 #include "GlobalGoalKeeper.h"
23 #include "Raster.h"
24 #include "RunLog.h"
25 using namespace KKB;
26 
27 
28 #include "FactoryFVProducer.h"
30 #include "FileDesc.h"
31 using namespace KKMLL;
32 
34 
35 #include "FeatureFileIOC45.h"
36 
37 
38 
39 kkint16 GrayScaleImagesFVProducer::maxNumOfFeatures = 56;
40 
41 kkint16 GrayScaleImagesFVProducer::SizeIndex = 0;
42 kkint16 GrayScaleImagesFVProducer::Moment1Index = 1;
43 kkint16 GrayScaleImagesFVProducer::Moment2Index = 2;
44 kkint16 GrayScaleImagesFVProducer::Moment3Index = 3;
45 kkint16 GrayScaleImagesFVProducer::Moment4Index = 4;
46 kkint16 GrayScaleImagesFVProducer::Moment5Index = 5;
47 kkint16 GrayScaleImagesFVProducer::Moment6Index = 6;
48 kkint16 GrayScaleImagesFVProducer::Moment7Index = 7;
49 kkint16 GrayScaleImagesFVProducer::Moment8Index = 8;
50 
51 kkint16 GrayScaleImagesFVProducer::EdgeSizeIndex = 9;
52 kkint16 GrayScaleImagesFVProducer::EdgeMoment1Index = 10;
53 kkint16 GrayScaleImagesFVProducer::EdgeMoment2Index = 11;
54 kkint16 GrayScaleImagesFVProducer::EdgeMoment3Index = 12;
55 kkint16 GrayScaleImagesFVProducer::EdgeMoment4Index = 13;
56 kkint16 GrayScaleImagesFVProducer::EdgeMoment5Index = 14;
57 kkint16 GrayScaleImagesFVProducer::EdgeMoment6Index = 15;
58 kkint16 GrayScaleImagesFVProducer::EdgeMoment7Index = 16;
59 kkint16 GrayScaleImagesFVProducer::EdgeMoment8Index = 17;
60 
61 kkint16 GrayScaleImagesFVProducer::TransparancyConvexHullIndex = 18;
62 kkint16 GrayScaleImagesFVProducer::TransparancyPixelCountIndex = 19;
63 kkint16 GrayScaleImagesFVProducer::TransparancyOpen3Index = 20;
64 kkint16 GrayScaleImagesFVProducer::TransparancyOpen5Index = 21;
65 kkint16 GrayScaleImagesFVProducer::TransparancyOpen7Index = 22;
66 kkint16 GrayScaleImagesFVProducer::TransparancyOpen9Index = 23;
67 kkint16 GrayScaleImagesFVProducer::TransparancyClose3Index = 24;
68 kkint16 GrayScaleImagesFVProducer::TransparancyClose5Index = 25;
69 kkint16 GrayScaleImagesFVProducer::TransparancyClose7Index = 26;
70 
71 kkint16 GrayScaleImagesFVProducer::ConvexAreaIndex = 27;
72 kkint16 GrayScaleImagesFVProducer::TransparancySizeIndex = 28;
73 kkint16 GrayScaleImagesFVProducer::TransparancyWtdIndex = 29;
74 kkint16 GrayScaleImagesFVProducer::WeighedMoment0Index = 30;
75 kkint16 GrayScaleImagesFVProducer::WeighedMoment1Index = 31;
76 kkint16 GrayScaleImagesFVProducer::WeighedMoment2Index = 32;
77 kkint16 GrayScaleImagesFVProducer::WeighedMoment3Index = 33;
78 kkint16 GrayScaleImagesFVProducer::WeighedMoment4Index = 34;
79 kkint16 GrayScaleImagesFVProducer::WeighedMoment5Index = 35;
80 kkint16 GrayScaleImagesFVProducer::WeighedMoment6Index = 36;
81 kkint16 GrayScaleImagesFVProducer::WeighedMoment7Index = 37;
82 kkint16 GrayScaleImagesFVProducer::WeighedMoment8Index = 38;
83 
84 kkint16 GrayScaleImagesFVProducer::IntensityHist1Index = 39;
85 kkint16 GrayScaleImagesFVProducer::IntensityHist2Index = 40;
86 kkint16 GrayScaleImagesFVProducer::IntensityHist3Index = 41;
87 kkint16 GrayScaleImagesFVProducer::IntensityHist4Index = 42;
88 kkint16 GrayScaleImagesFVProducer::IntensityHist5Index = 43;
89 kkint16 GrayScaleImagesFVProducer::IntensityHist6Index = 44;
90 kkint16 GrayScaleImagesFVProducer::IntensityHist7Index = 45;
91 
92 kkint16 GrayScaleImagesFVProducer::DarkSpotCount0 = 46;
93 kkint16 GrayScaleImagesFVProducer::DarkSpotCount1 = 47;
94 kkint16 GrayScaleImagesFVProducer::DarkSpotCount2 = 48;
95 kkint16 GrayScaleImagesFVProducer::DarkSpotCount3 = 49;
96 kkint16 GrayScaleImagesFVProducer::DarkSpotCount4 = 50;
97 kkint16 GrayScaleImagesFVProducer::DarkSpotCount5 = 51;
98 kkint16 GrayScaleImagesFVProducer::DarkSpotCount6 = 52;
99 kkint16 GrayScaleImagesFVProducer::DarkSpotCount7 = 53;
100 kkint16 GrayScaleImagesFVProducer::DarkSpotCount8 = 54;
101 kkint16 GrayScaleImagesFVProducer::DarkSpotCount9 = 55;
102 
103 
104 const KKStr GrayScaleImagesFVProducer::featureNames[] =
105 {
106  "Size", // 0
107  "Moment1", // 1
108  "Moment2", // 2
109  "Moment3", // 3
110  "Moment4", // 4
111  "Moment5", // 5
112  "Moment6", // 6
113  "Moment7", // 7
114  "Moment8", // 8
115 
116  "EdgeSize", // 9
117  "EdgeMoment1", // 10
118  "EdgeMoment2", // 11
119  "EdgeMoment3", // 12
120  "EdgeMoment4", // 13
121  "EdgeMoment5", // 14
122  "EdgeMoment6", // 15
123  "EdgeMoment7", // 16
124  "EdgeMoment8", // 17
125 
126  "TransparancyConvexHull", // 18
127  "TransparancyPixelCount", // 19
128 
129  "TransparancyOpen3", // 20 Granulometric features
130  "TransparancyOpen5", // 21
131  "TransparancyOpen7", // 22
132  "TransparancyOpen9", // 23
133  "TransparancyClose3", // 24
134  "TransparancyClose5", // 25
135  "TransparancyClose7", // 26
136 
137  "ConvexArea", // 27
138  "TransparancySize", // 28
139  "TransparancyWtd", // 29
140 
141  "WeighedMoment0", // 30
142  "WeighedMoment1", // 31
143  "WeighedMoment2", // 32
144  "WeighedMoment3", // 33
145  "WeighedMoment4", // 34
146  "WeighedMoment5", // 35
147  "WeighedMoment6", // 36
148  "WeighedMoment7", // 37
149  "WeighedMoment8", // 38
150 
151  "IntensityHist1", // 39
152  "IntensityHist2", // 40
153  "IntensityHist3", // 41
154  "IntensityHist4", // 42
155  "IntensityHist5", // 43
156  "IntensityHist6", // 44
157  "IntensityHist7", // 45
158  "DarkSpotCount0", // 46
159  "DarkSpotCount1", // 47
160  "DarkSpotCount2", // 48
161  "DarkSpotCount3", // 49
162  "DarkSpotCount4", // 50
163  "DarkSpotCount5", // 51
164  "DarkSpotCount6", // 52
165  "DarkSpotCount7", // 53
166  "DarkSpotCount8", // 54
167  "DarkSpotCount9" // 55
168 };
169 
170 
171 
172 
173 GrayScaleImagesFVProducer::GrayScaleImagesFVProducer (FactoryFVProducerPtr factory):
174  FeatureVectorProducer ("GrayScaleImages",
175  factory
176  ),
177  totPixsForMorphOps (4000000),
178  workRaster1Area (NULL),
179  workRaster2Area (NULL),
180  workRaster3Area (NULL),
181  workRaster1Rows (NULL),
182  workRaster2Rows (NULL),
183  workRaster3Rows (NULL)
184 {
185  workRaster1Area = new uchar[totPixsForMorphOps];
186  workRaster2Area = new uchar[totPixsForMorphOps];
187  workRaster3Area = new uchar[totPixsForMorphOps];
188 }
189 
190 
191 
192 
194 {
195  delete workRaster1Area; workRaster1Area = NULL;
196  delete workRaster2Area; workRaster2Area = NULL;
197  delete workRaster3Area; workRaster3Area = NULL;
198  delete workRaster1Rows; workRaster1Rows = NULL;
199  delete workRaster2Rows; workRaster2Rows = NULL;
200  delete workRaster3Rows; workRaster3Rows = NULL;
201 }
202 
203 
204 
205 void GrayScaleImagesFVProducer::ReductionByMultiple (kkint32 multiple,
206  const Raster& srcRaster,
207  Raster& destRaster
208  )
209 
210 {
211  kkint32 srcHeight = srcRaster.Height ();
212  kkint32 srcWidth = srcRaster.Width ();
213 
214  kkint32 destHeight = (srcHeight + multiple - 1) / multiple;
215  kkint32 destWidth = (srcWidth + multiple - 1) / multiple;
216 
217  kkint32 srcRow = 0;
218  kkint32 srcCol = 0;
219  kkint32 destRow = 0;
220  kkint32 destCol = 0;
221 
222  uchar** srcMatrix = srcRaster.Green ();
223  uchar** destMatrix = destRaster.Green ();
224 
225  kkint32 heightOffset = (srcHeight % multiple) / 2;
226  kkint32 widthOffset = (srcWidth % multiple) / 2;
227 
228  srcRow = -heightOffset;
229 
230  kkint32 r, c;
231 
232  for (destRow = 0; destRow < destHeight; ++destRow)
233  {
234  srcCol = -widthOffset;
235  for (destCol = 0; destCol < destWidth; ++destCol)
236  {
237  kkint32 total = 0;
238 
239  kkint32 srcRowStart = Max (0, srcRow);
240  kkint32 srcRowEnd = Min (srcRow + multiple, srcHeight);
241  kkint32 srcColStart = Max (0, srcCol);
242  kkint32 srcColEnd = Min (srcCol + multiple, srcWidth);
243 
244  kkint32 count = 0;
245 
246  for (r = srcRowStart; r <srcRowEnd; ++r)
247  {
248  for (c = srcColStart; c <srcColEnd; ++c)
249  {
250  total += srcMatrix[r][c];
251  ++count;
252  }
253  }
254  destMatrix[destRow][destCol] = (uchar)(total / count);
255  srcCol += multiple;
256  }
257  srcRow += multiple;
258  }
259 } /* ReductionByMultiple */
260 
261 
262 
263 
264 void GrayScaleImagesFVProducer::SaveIntermediateImage (const Raster& raster,
265  const KKStr& desc,
266  RasterListPtr intermediateImages
267  )
268 {
269  if (!intermediateImages)
270  return;
271 
272  RasterPtr newRaster = NULL;
273 
274  kkint32 largestDim = Max (raster.Height (), raster.Width ());
275  if (largestDim < 300)
276  {
277  newRaster = new Raster (raster);
278  }
279  else
280  {
281  kkint32 reductionMultiple = 2;
282  while ((largestDim / reductionMultiple) > 300)
283  reductionMultiple++;
284  newRaster = raster.ReduceByEvenMultiple (reductionMultiple);
285  }
286 
287  newRaster->FileName (desc);
288  intermediateImages->PushOnBack (newRaster);
289 } /* SaveIntermediateImage */
290 
291 
292 
293 void GrayScaleImagesFVProducer::BinarizeImageByThreshold (uchar lower,
294  uchar upper,
295  const Raster& src,
296  Raster& dest
297  )
298 {
299  uchar const * srcData = src.GreenArea ();
300  uchar * destData = dest.GreenArea ();
301 
302  kkint32 totPixels = src.TotPixels ();
303 
304  memset (destData, 0, totPixels);
305 
306  kkint32 x = 0;
307  for (x = 0; x < totPixels; ++x)
308  {
309  if ((srcData[x] >= lower) && (srcData[x] < upper))
310  destData[x] = 255;
311  }
312 } /* BinarizeImageByThreshold */
313 
314 
315 
316 FeatureVectorPtr GrayScaleImagesFVProducer::ComputeFeatureVector (const Raster& srcImage,
317  const MLClassPtr knownClass,
318  RasterListPtr intermediateImages,
319  float priorReductionFactor,
320  RunLog& runLog
321  )
322 
323 {
324  FeatureVectorPtr fv = new FeatureVector (maxNumOfFeatures);
325  fv->MLClass (knownClass);
326  float* featureData = fv->FeatureDataAlter ();
327 
329 
330  kkint32 areaBeforeReduction = 0;
331  float weighedSizeBeforeReduction = 0.0f;
332 
333  kkint32 row = 0;
334  kkint32 col = 0;
335 
336  kkuint32 intensityHistBuckets[8];
337  srcImage.CalcAreaAndIntensityFeatures (areaBeforeReduction,
338  weighedSizeBeforeReduction,
339  intensityHistBuckets
340  );
341 
342  kkint32 srcHeight = srcImage.Height ();
343  kkint32 srcWidth = srcImage.Width ();
344 
345  kkint32 reducedHeight = srcHeight;
346  kkint32 reducedWidth = srcWidth;
347 
348  kkint32 reducedSquareArea = reducedHeight * reducedWidth;
349 
350  kkint32 reductionMultiple = 1;
351 
352  while (reducedSquareArea > totPixsForMorphOps)
353  {
354  ++reductionMultiple;
355  reducedHeight = (srcHeight + reductionMultiple - 1) / reductionMultiple;
356  reducedWidth = (srcWidth + reductionMultiple - 1) / reductionMultiple;
357  reducedSquareArea = reducedHeight * reducedWidth;
358  }
359 
360  kkint32 reductionMultipleSquared = reductionMultiple * reductionMultiple;
361 
362  float totalReductionMultiple = priorReductionFactor * reductionMultiple;
363  float totalReductionMultipleSquared = totalReductionMultiple * totalReductionMultiple;
364 
365  delete workRaster1Rows; workRaster1Rows = new uchar*[reducedHeight];
366  delete workRaster2Rows; workRaster2Rows = new uchar*[reducedHeight];
367  delete workRaster3Rows; workRaster3Rows = new uchar*[reducedHeight];
368 
369  uchar* wp1 = workRaster1Area;
370  uchar* wp2 = workRaster2Area;
371  uchar* wp3 = workRaster3Area;
372 
373  for (row = 0; row < reducedHeight; ++row)
374  {
375  workRaster1Rows[row] = wp1;
376  workRaster2Rows[row] = wp2;
377  workRaster3Rows[row] = wp3;
378  wp1 += reducedWidth;
379  wp2 += reducedWidth;
380  wp3 += reducedWidth;
381  }
382 
383  Raster workRaster1 (reducedHeight, reducedWidth, workRaster1Area, workRaster1Rows);
384  Raster workRaster2 (reducedHeight, reducedWidth, workRaster2Area, workRaster2Rows);
385  Raster workRaster3 (reducedHeight, reducedWidth, workRaster3Area, workRaster3Rows);
386 
387  Raster const * initRaster = NULL;
388  RasterPtr wr1 = NULL;
389  RasterPtr wr2 = NULL;
390 
391  if (reductionMultiple > 1)
392  {
393  try
394  {
395  ReductionByMultiple (reductionMultiple, srcImage, workRaster1);
396  }
397  catch (...)
398  {
399  runLog.Level (-1) << endl << "GrayScaleImagesFVProducer::ComputeFeatureVector ***ERROR*** Exception calling 'ReductionByMultiple'." << endl << endl;
400  return NULL;
401  }
402  initRaster = &workRaster1;
403  wr1 = &workRaster2;
404  wr2 = &workRaster3;
405  }
406  else
407  {
408  initRaster = &srcImage;
409  wr1 = &workRaster1;
410  wr2 = &workRaster2;
411  }
412 
413  if (areaBeforeReduction < 20)
414  {
415  for (kkint32 tp = 0; tp < maxNumOfFeatures; tp++)
416  featureData[tp] = 9999999;
417  return fv;
418  }
419 
420  float convexf = 0.0;
421  float centralMoments[9];
422  float centralMomentsWeighted[9];
423 
424  kkint32 pixelCountReduced = 0;
425  float pixelCountWeightedReduced = 0.0f;
426 
427  initRaster->ComputeCentralMoments (pixelCountReduced, pixelCountWeightedReduced, centralMoments, centralMomentsWeighted);
428 
429 
430  float edgeMomentf[9];
431 
432  initRaster->Dilation (wr1);
433  wr1->Dilation (wr2);
434  wr2->FillHole (wr1);
435 
436  wr1->Erosion (wr2);
437  wr2->Edge (wr1);
438  wr1->CentralMoments (edgeMomentf);
439  if (intermediateImages)
440  {
441  kkint32 numEdgePixelsFound = (kkint32)(edgeMomentf[0]);
442  SaveIntermediateImage (*wr2, "Edge_Image_" + StrFormatInt (numEdgePixelsFound, "ZZZZ0"), intermediateImages);
443  }
444 
445  kkint32 area = (kkint32)(centralMoments[0] + 0.5f); // Moment-0 is the same as the number of foreground pixels in example.
446  {
447  ConvexHullPtr ch = new ConvexHull ();
448  ch->Filter (*initRaster, wr1);
449  convexf = (float)ch->ConvexArea ();
450 
451  if (intermediateImages)
452  {
453  KKStr convexImageFileName = "ConvexHull_" +
454  StrFormatInt ((kkint32)convexf, "ZZZZZ0");
455  SaveIntermediateImage (*wr1, convexImageFileName, intermediateImages);
456  }
457 
458  //delete convexImage;
459  //convexImage = NULL;
460  delete ch;
461  ch = NULL;
462  }
463 
464  initRaster->Erosion (wr1);
465  wr1->Dilation (wr2);
466  float areaOpen3 = (float)(wr2->ForegroundPixelCount());
467  if (intermediateImages)
468  SaveIntermediateImage (*wr2, "Opening3_" + StrFormatInt ((kkint32)areaOpen3, "ZZZZZZ0"), intermediateImages);
469 
470 
473  float areaOpen5 = (float)(wr2->ForegroundPixelCount ());
474  if (intermediateImages)
475  SaveIntermediateImage (*wr2, "Opening5_" + StrFormatInt ((kkint32)areaOpen5, "ZZZZZZ0"), intermediateImages);
476 
479  float areaOpen7 = (float)(wr2->ForegroundPixelCount ());
480  if (intermediateImages)
481  SaveIntermediateImage (*wr2, "Opening7_" + StrFormatInt ((kkint32)areaOpen7, "ZZZZZZ0"), intermediateImages);
482 
485  float areaOpen9 = (float)(wr2->ForegroundPixelCount ());
486  if (intermediateImages)
487  SaveIntermediateImage (*wr2, "Opening9_" + StrFormatInt ((kkint32)areaOpen9, "ZZZZZZ0"), intermediateImages);
488 
489  initRaster->Dilation (wr1);
490  wr1->Erosion (wr2);
491  float areaClose3 = (float)(wr2->ForegroundPixelCount ());
492  if (intermediateImages)
493  SaveIntermediateImage (*wr2, "Close3_" + StrFormatInt ((kkint32)areaClose3, "ZZZZZZ0"), intermediateImages);
494 
495  wr2->FillHole (wr1);
496  float tranf = (float)(wr2->ForegroundPixelCount ());
497  if (intermediateImages)
498  SaveIntermediateImage (*wr2, "FillHole_" + StrFormatInt ((kkint32)tranf, "ZZZZZZ0"), intermediateImages);
499 
502  float areaClose5 = (float)(wr2->ForegroundPixelCount ());
503  if (intermediateImages)
504  SaveIntermediateImage (*wr2, "Close5_" + StrFormatInt ((kkint32)areaClose5, "ZZZZZZ0"), intermediateImages);
505 
508  float areaClose7 = float (wr2->ForegroundPixelCount ());
509  if (intermediateImages)
510  SaveIntermediateImage (*wr2, "Close7_" + StrFormatInt ((kkint32)areaClose7, "ZZZZZZ0"), intermediateImages);
511 
512  {
513  featureData[SizeIndex] = float (areaBeforeReduction * priorReductionFactor);
514  featureData[Moment1Index] = float (centralMoments[1]);
515  featureData[Moment2Index] = float (centralMoments[2]);
516  featureData[Moment3Index] = float (centralMoments[3]);
517  featureData[Moment4Index] = float (centralMoments[4]);
518  featureData[Moment5Index] = float (centralMoments[5]);
519  featureData[Moment6Index] = float (centralMoments[6]);
520  featureData[Moment7Index] = float (centralMoments[7]);
521  featureData[Moment8Index] = float (centralMoments[8]);
522 
523  featureData[WeighedMoment0Index] = centralMomentsWeighted[0] * totalReductionMultiple;
524  featureData[WeighedMoment1Index] = centralMomentsWeighted[1];
525  featureData[WeighedMoment2Index] = centralMomentsWeighted[2];
526  featureData[WeighedMoment3Index] = centralMomentsWeighted[3];
527  featureData[WeighedMoment4Index] = centralMomentsWeighted[4];
528  featureData[WeighedMoment5Index] = centralMomentsWeighted[5];
529  featureData[WeighedMoment6Index] = centralMomentsWeighted[6];
530  featureData[WeighedMoment7Index] = centralMomentsWeighted[7];
531  featureData[WeighedMoment8Index] = centralMomentsWeighted[8];
532 
533  featureData[EdgeSizeIndex] = (float)edgeMomentf[0] * totalReductionMultiple;
534  featureData[EdgeMoment1Index] = (float)edgeMomentf[1];
535  featureData[EdgeMoment2Index] = (float)edgeMomentf[2];
536  featureData[EdgeMoment3Index] = (float)edgeMomentf[3];
537  featureData[EdgeMoment4Index] = (float)edgeMomentf[4];
538  featureData[EdgeMoment5Index] = (float)edgeMomentf[5];
539  featureData[EdgeMoment6Index] = (float)edgeMomentf[6];
540  featureData[EdgeMoment7Index] = (float)edgeMomentf[7];
541  featureData[EdgeMoment8Index] = (float)edgeMomentf[8];
542 
543  // Need to adjust for any reduction in Image Size
544  //featureData[SizeIndex] = float (areaBeforeReduction);
545  //featureData[EdgeSizeIndex] = float (edgeMomentf[0] * (float)(reductionMultipleSquared));
546  //featureData[WeighedMoment0Index] = weighedSizeBeforeReduction;
547  }
548 
549 
550  if ((area > convexf) && (convexf > 0))
551  featureData[TransparancyConvexHullIndex] = 1.0;
552  else
553  featureData[TransparancyConvexHullIndex] = (float)area / (float)convexf;
554 
555  featureData[TransparancyPixelCountIndex] = (float)area / (float)tranf;
556  featureData[TransparancyOpen3Index] = (float)(area - areaOpen3) / (float)area;
557  featureData[TransparancyOpen5Index] = (float)(area - areaOpen5) / (float)area;
558  featureData[TransparancyOpen7Index] = (float)(area - areaOpen7) / (float)area;
559  featureData[TransparancyOpen9Index] = (float)(area - areaOpen9) / (float)area;
560  featureData[TransparancyClose3Index] = (float)(area - areaClose3) / (float)area;
561  featureData[TransparancyClose5Index] = (float)(area - areaClose5) / (float)area;
562  featureData[TransparancyClose7Index] = (float)(area - areaClose7) / (float)area;
563 
564  {
565  // This part has to be done after 'CalcOrientationAndEigerRatio' is called. That is where the example centroid is calculated.
566  //fv->CentroidCol (initRaster->CentroidCol () * reductionMultiple);
567  //fv->CentroidRow (initRaster->CentroidRow () * reductioqnMultiple);
568  }
569 
570  featureData[ConvexAreaIndex] = convexf * totalReductionMultipleSquared;
571  featureData[TransparancySizeIndex] = (float)(centralMoments[0] / convexf);
572  featureData[TransparancyWtdIndex] = (float)(centralMomentsWeighted[0] / convexf);
573 
574  float areaD = float (areaBeforeReduction);
575 
576  featureData[IntensityHist1Index] = ((float)intensityHistBuckets[1] / areaD);
577  featureData[IntensityHist2Index] = ((float)intensityHistBuckets[2] / areaD);
578  featureData[IntensityHist3Index] = ((float)intensityHistBuckets[3] / areaD);
579  featureData[IntensityHist4Index] = ((float)intensityHistBuckets[4] / areaD);
580  featureData[IntensityHist5Index] = ((float)intensityHistBuckets[5] / areaD);
581  featureData[IntensityHist6Index] = ((float)intensityHistBuckets[6] / areaD);
582  featureData[IntensityHist7Index] = ((float)intensityHistBuckets[7] / areaD);
583 
584 
585  {
586  RasterPtr darkSpots = NULL;
587 
588  BinarizeImageByThreshold (200, 255, *initRaster, *wr1);
589 
592 
593  KKB::BlobListPtr blobs = wr1->ExtractBlobs (3);
594 
595  int darkSpotFreq[10];
596  int x;
597  for (x = 0; x < 10; ++x)
598  darkSpotFreq[x] = 0;
599 
600  KKB::BlobList::iterator idx;
601  for (idx = blobs->begin (); idx != blobs->end (); ++idx)
602  {
603  KKB::BlobPtr b = idx->second;
604 
605  double l = ::log ((double)(b->PixelCount ()));
606 
607  double logOfThree = ::log (3.0);
608 
609  int index = (int)floor (l / logOfThree);
610  index = Min (Max (0, index), 9);
611  ++(darkSpotFreq[index]);
612  }
613 
614  featureData[DarkSpotCount0] = (float)darkSpotFreq[0];
615  featureData[DarkSpotCount1] = (float)darkSpotFreq[1];
616  featureData[DarkSpotCount2] = (float)darkSpotFreq[2];
617  featureData[DarkSpotCount3] = (float)darkSpotFreq[3];
618  featureData[DarkSpotCount4] = (float)darkSpotFreq[4];
619  featureData[DarkSpotCount5] = (float)darkSpotFreq[5];
620  featureData[DarkSpotCount6] = (float)darkSpotFreq[6];
621  featureData[DarkSpotCount7] = (float)darkSpotFreq[7];
622  featureData[DarkSpotCount8] = (float)darkSpotFreq[8];
623  featureData[DarkSpotCount9] = (float)darkSpotFreq[9];
624 
625  delete blobs;
626  blobs = NULL;
627  }
628 
629  if (intermediateImages)
630  {
631  RasterPtr thinnedImage = initRaster->ThinContour ();
632  SaveIntermediateImage (*thinnedImage, "_Thinned", intermediateImages);
633  delete thinnedImage;
634  thinnedImage = NULL;
635  }
636 
637  featureData[0] = (float)areaBeforeReduction; // In case the example was reduced.
638  fv->OrigSize ((float)areaBeforeReduction);
639 
640  return fv;
641 } /* ComputeFeatureVector */
642 
643 
644 
645 
647 {
648  return &(typeid (FeatureVector));
649 }
650 
651 
653 {
654  return &(typeid (FeatureVectorList));
655 }
656 
657 
658 
660 {
661  return DefineFileDescStatic ();
662 } /* DefineFileDesc */
663 
664 
665 
666 FileDescPtr GrayScaleImagesFVProducer::existingFileDesc = NULL;
667 
669 {
670  if (existingFileDesc)
671  return existingFileDesc;
672 
674  if (!existingFileDesc)
675  {
676  bool alreadyExists = false;
677  FileDescPtr tempFileDesc = new KKMLL::FileDesc ();
678  for (kkint32 fieldNum = 0; fieldNum < maxNumOfFeatures; ++fieldNum)
679  {
680  tempFileDesc->AddAAttribute (featureNames[fieldNum], AttributeType::Numeric, alreadyExists);
681  }
683 
684  existingFileDesc = FileDesc::GetExistingFileDesc (tempFileDesc);
685  tempFileDesc = NULL;
686  }
688 
689  // Lets make sure that one was already created by opening up a data file.
690  return existingFileDesc;
691 }
692 
693 
694 
695 
696 FeatureVectorListPtr GrayScaleImagesFVProducer::ManufacturFeatureVectorList (bool owner,
697  RunLog& runLog
698  )
699  const
700 {
701  return new FeatureVectorList (FileDesc (), owner);
702 }
703 
704 
705 
706 
707 
708 
709 GrayScaleImagesFVProducerFactory::GrayScaleImagesFVProducerFactory ():
710  FactoryFVProducer("GrayScaleImagesFVProducerFactory", "FeatureVector", "General Purpose Grayscale Image Feature Computer")
711 {
712 }
713 
714 
715 
717 {
718 }
719 
720 
721 
723 {
725 }
726 
727 
729 {
730  return &typeid (FeatureVector);
731 }
732 
733 
734 
736 {
737  return &typeid (FeatureVectorList);
738 }
739 
740 
741 
743 {
745 }
746 
747 
748 
750 {
751  return new GrayScaleImagesFVProducer (this);
752 } /* ManufactureInstance */
753 
754 
755 
756 
758  RunLog& runLog
759  )
760  const
761 {
763 }
764 
765 
766 
768 
769 
770 
771 
773 {
775 
776  if (factory == NULL)
777  {
778  factory = new GrayScaleImagesFVProducerFactory ();
780  }
782 
783  return factory;
784 }
__int16 kkint16
16 bit signed integer.
Definition: KKBaseTypes.h:85
void Erosion(RasterPtr dest) const
Place into destination a eroded version of this instances image.
Definition: Raster.cpp:2411
virtual ~GrayScaleImagesFVProducerFactory()
A Factory can never be deleted until the application terminates; the atexit method will perform the d...
void CentralMoments(float features[9]) const
returns in &#39;features&#39; the 8 central moments as defined by Hu plus eccentricity in the eight bucket...
Definition: Raster.cpp:4100
Provides a detailed description of the attributes of a dataset.
Definition: FileDesc.h:72
static FileDescPtr GetExistingFileDesc(FileDescPtr fileDesc)
Returns a pointer to an existing instance of &#39;fileDesc&#39; if it exists, otherwise will use one being pa...
Definition: FileDesc.cpp:555
static FeatureFileIOC45Ptr Driver()
__int32 kkint32
Definition: KKBaseTypes.h:88
virtual const type_info * FeatureVectorTypeId() const
Returns the &#39;type_info&#39; of the Feature Vector that this instance of &#39;FeatureComputer&#39; creates...
void AddAAttribute(const KKB::KKStr &_name, KKMLL::AttributeType _type, bool &alreadyExists)
Definition: FileDesc.cpp:169
virtual GrayScaleImagesFVProducerPtr ManufactureInstance(RunLog &runLog)
FactoryFVProducer(const KKStr &_name, const KKStr &_fvClassName, const KKStr &_description)
FeatureVector(kkint32 _numOfFeatures)
BlobListPtr ExtractBlobs(kkint32 dist)
Will extract a list of connected components from this instance.
Definition: Raster.cpp:3200
Supports the reading and writing of feature data from C45 formated feature files. ...
A class that is used by to represent a single image in memory.
Definition: Raster.h:108
Blob * BlobPtr
Definition: Blob.h:70
GrayScaleImagesFVProducer(FactoryFVProducerPtr factory)
ConvexHull * ConvexHullPtr
Definition: ConvexHull.h:123
FileDescConstPtr FileDesc() const
Returns back a "FileDesc" instance that describes the features that this instance of &#39;FeatureVectorPr...
static void RegisterFactory(FactoryFVProducerPtr factory, RunLog *runLog)
This is where you register newly instantiated instances of &#39;FactoryFVProducer&#39; derived classes...
virtual FeatureVectorListPtr ManufacturFeatureVectorList(bool owner, RunLog &runLog) const
Manufactures a instance of a derived &#39;FeatureVectorList&#39; class that is appropriate for containing ins...
BlobList * BlobListPtr
Definition: Blob.h:130
void Version(kkint16 _version)
Definition: FileDesc.h:126
#define _GrayScaleImagesFVProducer_VersionNum_
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
virtual FeatureVectorListPtr ManufacturFeatureVectorList(bool owner, RunLog &runLog) const
Manufactures a instance of a &#39;LarcosFeatureVectorList&#39; class that will own its contents.
FeatureVectorList(FileDescPtr _fileDesc, bool _owner)
Will create a new empty list of FeatureVector&#39;s.
void Edge(RasterPtr dest)
Definition: Raster.cpp:3052
kkint32 Height() const
Definition: Raster.h:319
Container class for FeatureVector derived objects.
void FillHole(RasterPtr mask)
Fills holes in the image using the &#39;mask&#39; raster as a work area.
Definition: Raster.cpp:2107
KKTHread * KKTHreadPtr
RasterList * RasterListPtr
Definition: Raster.h:75
RasterPtr Filter(const Raster &src, RasterPtr dest)
Definition: ConvexHull.cpp:128
KKStr operator+(const char *left, const KKStr &right)
Definition: KKStr.cpp:3976
virtual const type_info * FeatureVectorListTypeId() const
Returns the &#39;type_info&#39; of the FeatureVectorList that this instance of &#39;FactoryFVProducer&#39; will creat...
void CalcAreaAndIntensityFeatures(kkint32 &area, float &weightedSize, kkuint32 intensityHistBuckets[8]) const
Calculates both Intensity Histograms, one not including internal background pixels and one with plus ...
Definition: Raster.cpp:4018
virtual const type_info * FeatureVectorTypeId() const
Returns the &#39;type_info&#39; of the FeatureVector that this instance of &#39;FactoryFVProducer&#39; works with...
void Dilation(RasterPtr dest) const
Definition: Raster.cpp:1804
RasterPtr ReduceByEvenMultiple(kkint32 multiple) const
Definition: Raster.cpp:7361
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
kkint32 ForegroundPixelCount() const
Definition: Raster.h:317
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
void Dilation(RasterPtr dest, MaskTypes mask) const
Definition: Raster.cpp:1905
kkint32 ConvexArea()
Definition: ConvexHull.cpp:189
AttributeType
Definition: Attribute.h:36
void Erosion(RasterPtr dest, MaskTypes mask) const
Definition: Raster.cpp:2514
void MLClass(MLClassPtr _mlClass)
Assign a class to this example.
Definition: FeatureVector.h:74
kkint32 TotPixels() const
Definition: Raster.h:323
KKStr StrFormatInt(kkint32 val, const char *mask)
Definition: KKStr.cpp:5004
GrayScaleImagesFVProducer * GrayScaleImagesFVProducerPtr
uchar * GreenArea() const
Definition: Raster.h:330
virtual FeatureFileIOPtr DefaultFeatureFileIO() const
void Version(kkint16 _version)
Definition: FeatureVector.h:80
virtual FeatureVectorPtr ComputeFeatureVector(const Raster &srcImage, const MLClassPtr knownClass, RasterListPtr intermediateImages, float priorReductionFactor, RunLog &runLog)
Compute a FeatureVector for the supplied &#39;image&#39;.
kkint32 Width() const
Definition: Raster.h:324
void ComputeCentralMoments(kkint32 &foregroundPixelCount, float &weightedPixelCount, float centralMoments[9], float centralMomentsWeighted[9]) const
Computes central moments; one set where each pixel is treated as 1 or 0(Foreground/Background) and th...
Definition: Raster.cpp:4418
FeatureVectorProducer(const KKStr &_name, FactoryFVProducerPtr _factory)
void OrigSize(float _origSize)
The value of Feature[0] before normalization.
Definition: FeatureVector.h:77
Operator that will create a Convex Hull of a supplied image.
Definition: ConvexHull.h:42
Raster(kkint32 _height, kkint32 _width, uchar *_Data, uchar **_Rows)
Construct a raster object that will utilize a image already in memory.
Definition: Raster.cpp:689
Base class for all Morphological operations.
Definition: MorphOp.h:44
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)
virtual FileDescPtr DefineFileDesc() const
Raster(const Raster &_raster)
Copy Constructor.
Definition: Raster.cpp:460
Responsible for creating a FeatureFectorProducer instance.
static GrayScaleImagesFVProducerFactory * Factory(RunLog *runLog)
Returns instance of "GrayScaleImagesFVProducerFactory" that is registered with "FactoryFVProducer::Re...
void FileName(const KKStr &_fileName)
Definition: Raster.h:346
kkint32 PixelCount()
Number of pixels that are part of this blob.
Definition: Blob.h:56
RasterPtr ThinContour() const
Definition: Raster.cpp:8187
Represents a Feature Vector of a single example, labeled or unlabeled.
Definition: FeatureVector.h:59
uchar ** Green() const
Definition: Raster.h:327
Maintains one instance of a GoalKeeper object that can be used anywhere in the application.
float * FeatureDataAlter()
Same as &#39;FeatureData() except you can modify the data.
A abstract class that is meant to compute a FeatureVector from a source image.
virtual const type_info * FeatureVectorListTypeId() const
Returns the &#39;type_info&#39; of the FeatureVectorList derived class that can contain instances in &#39;Feature...