30 bucketSize (_bucketSize),
32 equalizedMapTable (NULL),
34 numOfBuckets (_numOfBuckets),
37 wrapArround (_wrapArround)
43 <<
"*** ERROR *** Histogram, Negative numOfBuckets[" << numOfBuckets <<
"." << std::endl
48 range = bucketSize * numOfBuckets;
50 buckets =
new float[numOfBuckets];
51 bucketTotals =
new float[numOfBuckets];
53 for (
kkint32 x = 0; x < numOfBuckets; x++)
55 buckets [x] = (
float)0.0;
56 bucketTotals[x] = (
float)0.0;
68 if (equalizedMapTable)
69 delete equalizedMapTable;
83 for (idx = 0; idx < numOfBuckets; idx++)
85 if (buckets[idx] > maxCount)
87 maxCount = buckets[idx];
102 if (buckets[idx] <= (
float)0.0)
105 return bucketTotals[idx] / buckets[idx];
124 if ((bucket < 0) || (bucket >= numOfBuckets))
127 <<
"*** ERROR *** Histogram::Bucket, Bucket[" << bucket <<
"] is out of range." << std::endl
132 return buckets[bucket];
142 <<
"*** ERROR *** val[" << val <<
"] is out of range [" << minValue <<
"]." << std::endl
148 if (bucket >= numOfBuckets)
149 bucket = numOfBuckets - 1;
153 buckets[bucket] = buckets[bucket] + (
float)1.0;
154 bucketTotals[bucket] += val;
156 totalCount += (
float)1.0;
169 float maxCount = (
float)0.0;
174 uchar borderColor = 100;
175 uchar colorAxis = 150;
176 uchar colorBar = 200;
177 uchar colorBarMax = 255;
178 uchar colorBarPeak = 255;
179 uchar gridColor = 180;
180 uchar hashColor = 70;
181 uchar hashColor2 = 45;
183 kkint32 graphWidth = numOfBuckets * (barWidth + interBarWidth) + 20;
188 bool* vertGridLines =
new bool[numOfBuckets];
189 bool* peakBarLines =
new bool[numOfBuckets];
190 for (x = 0; x < numOfBuckets; x++)
192 vertGridLines[x] =
false;
202 kkint32 numOfBucketsToZero = (
kkint32)(0.5 + fabs (minValue) / bucketSize);
204 if (numOfBucketsToZero < numOfBuckets)
205 middleIdx = numOfBucketsToZero;
209 while (x < numOfBuckets)
211 vertGridLines[x] =
true;
218 vertGridLines[x] =
true;
222 for (bucket = 0; bucket < numOfBuckets; bucket++)
224 if (buckets[bucket] > maxCount)
225 maxCount = buckets[bucket];
228 kkint32 maxColHeight = Min ((kkint32)(maxCount * (barWidth + interBarWidth) + (
float)0.5), (kkint32)512);
229 kkint32 graphHeight = maxColHeight + 20;
232 RasterPtr graph =
new Raster (graphHeight
, graphWidth
);
236 graph
->DrawLine ( 10
, graphWidth - 10
, graphHeight - 10
, graphWidth - 10
, borderColor
);
238 graph
->DrawLine (graphHeight - 10
, graphWidth - 10
, graphHeight - 10
, 10
, borderColor
);
244 row = graphHeight - 10;
247 while (x < maxColHeight)
277 for (bucket = 0; bucket < numOfBuckets; bucket++)
279 for (x = 0; x < (barWidth + interBarWidth); x++)
284 if ((bucket % 10) == 0)
293 if (vertGridLines[bucket] ==
true)
298 colHeight = (
kkint32)ceil ((((
float)maxColHeight * buckets[bucket]) / maxCount));
300 if ((colHeight < 1) && (buckets[bucket] > (
float)0.0))
302 cout <<
"I want to know about this." << std::endl;
305 row = graphHeight - 10;
307 uchar barCodeToUse = colorBar;
308 if (buckets[bucket] >= maxCount)
309 barCodeToUse = colorBarMax;
311 else if (peakBarLines[bucket])
312 barCodeToUse = colorBarPeak;
314 for (
kkint32 x = 0; x < colHeight; x++)
316 for (y = 0; y < barWidth; y++)
324 col += (barWidth + interBarWidth);
327 delete[] peakBarLines;
328 delete[] vertGridLines;
344 float maxCount = (
float)0.0;
349 uchar borderColor = 100;
350 uchar colorAxis = 150;
351 uchar colorBar = 200;
352 uchar colorBarMax = 255;
353 uchar colorBarPeak = 255;
354 uchar gridColor = 180;
355 uchar hashColor = 70;
356 uchar hashColor2 = 45;
358 kkint32 graphWidth = numOfBuckets * (barWidth + interBarWidth) + 20;
363 bool* vertGridLines =
new bool[numOfBuckets];
364 bool* peakBarLines =
new bool[numOfBuckets];
365 for (x = 0; x < numOfBuckets; x++)
367 vertGridLines[x] =
false;
377 kkint32 numOfBucketsToZero = (
kkint32)(0.5 + fabs (minValue) / bucketSize);
379 if (numOfBucketsToZero < numOfBuckets)
380 middleIdx = numOfBucketsToZero;
384 while (x < numOfBuckets)
386 vertGridLines[x] =
true;
393 vertGridLines[x] =
true;
398 for (bucket = 0; bucket < numOfBuckets; bucket++)
400 if (buckets[bucket] > maxCount)
401 maxCount = buckets[bucket];
405 kkint32 maxColHeight = Min ((kkint32)(maxCount * (barWidth + interBarWidth) + (
float)0.5), (kkint32)512);
406 kkint32 graphHeight = maxColHeight + 20;
408 RasterPtr graph =
new Raster (graphHeight
, graphWidth
);
411 graph
->DrawLine ( 10
, graphWidth - 10
, graphHeight - 10
, graphWidth - 10
, borderColor
);
412 graph
->DrawLine (graphHeight - 10
, graphWidth - 10
, graphHeight - 10
, 10
, borderColor
);
416 row = graphHeight - 10;
419 while (x < maxColHeight)
447 for (bucket = 0; bucket < numOfBuckets; bucket++)
449 for (x = 0; x < (barWidth + interBarWidth); x++)
454 if ((bucket % 10) == 0)
463 if (vertGridLines[bucket] ==
true)
468 colHeight = (
kkint32)ceil ((((
float)maxColHeight * buckets[bucket]) / maxCount));
470 if ((colHeight < 1) && (buckets[bucket] > (
float)0.0))
472 cout <<
"I want to know about this." << std::endl;
475 row = graphHeight - 10;
477 uchar barCodeToUse = colorBar;
478 if (buckets[bucket] >= maxCount)
479 barCodeToUse = colorBarMax;
481 else if (peakBarLines[bucket])
482 barCodeToUse = colorBarPeak;
484 for (
kkint32 x = 0; x < colHeight; x++)
486 for (y = 0; y < barWidth; y++)
494 col += (barWidth + interBarWidth);
497 delete[] peakBarLines;
498 delete[] vertGridLines;
531 ofstream o (fileName.Str ());
536 float avgVal = totalVal / totalCount;
538 o <<
"MinValue" <<
"\t" <<
"\t" <<
"BucketSize" <<
"\t" <<
"NumOfBuckets" <<
"\t" <<
"TotalCount" <<
"\t" <<
"Average" << std::endl;
539 o << minValue <<
"\t" <<
"\t" << bucketSize <<
"\t" << numOfBuckets <<
"\t" << totalCount <<
"\t" << avgVal << std::endl;
546 float bucketVal = minValue + bucketSize;
548 o <<
"BucketValue" <<
"\t" 550 <<
"AvgValue" <<
"\t" 555 for (bucketIDX = 0; bucketIDX < numOfBuckets; bucketIDX++)
559 o <<
"\t" << buckets[bucketIDX];
561 if (buckets[bucketIDX] > (
float)0.0)
562 avg = bucketTotals[bucketIDX] / buckets[bucketIDX];
568 percentage = (
float)100.0 * buckets[bucketIDX] / totalCount;
569 o <<
"\t" << percentage <<
"%";
571 bucketVal += bucketSize;
590 float sumOfbuckets = (
float)0.0;
591 float sumOfBucketTotals = (
float)0.0;
602 startBuckOffset = numOfBuckets - (smoothWidth / 2);
603 endBuckOffset = startBuckOffset + smoothWidth - 1 - numOfBuckets;
608 bucket = startBuckOffset;
612 sumOfbuckets += buckets [bucket];
613 sumOfBucketTotals += bucketTotals [bucket];
615 if (bucket == endBuckOffset)
619 if (bucket >= numOfBuckets)
620 bucket = bucket - numOfBuckets;
623 for (bucket = 0; bucket < numOfBuckets; bucket++)
625 hist->buckets [bucket] = sumOfbuckets / (
float)smoothWidth;
626 hist->bucketTotals [bucket] = sumOfBucketTotals / (
float)smoothWidth;
628 sumOfbuckets -= buckets [startBuckOffset];
629 sumOfBucketTotals -= bucketTotals [startBuckOffset];
632 if (startBuckOffset >= numOfBuckets)
633 startBuckOffset = startBuckOffset - numOfBuckets;
636 if (endBuckOffset >= numOfBuckets)
637 endBuckOffset = endBuckOffset - numOfBuckets;
639 sumOfbuckets += buckets [endBuckOffset];
640 sumOfBucketTotals += bucketTotals [endBuckOffset];
648 startBuckOffset = -(smoothWidth / 2);
649 endBuckOffset = startBuckOffset + smoothWidth - 1;
651 kkint32 spreadSize = endBuckOffset + 1;
654 for (bucket = 0; bucket <= endBuckOffset; bucket++)
656 sumOfbuckets += buckets [bucket];
657 sumOfBucketTotals += bucketTotals [bucket];
661 for (bucket = 0; bucket < numOfBuckets; bucket++)
663 hist->buckets [bucket] = sumOfbuckets / (
float)spreadSize;
665 if (sumOfbuckets > (
float)0.0)
667 hist->bucketTotals [bucket] = hist->buckets [bucket] * sumOfBucketTotals / sumOfbuckets;
671 hist->bucketTotals [bucket] = (
float)0.0;
674 if (startBuckOffset >= 0)
676 sumOfbuckets = sumOfbuckets - buckets [startBuckOffset];
677 sumOfBucketTotals = sumOfBucketTotals - bucketTotals [startBuckOffset];
684 if (endBuckOffset < numOfBuckets)
686 sumOfbuckets = sumOfbuckets + buckets [endBuckOffset];
687 sumOfBucketTotals = sumOfBucketTotals + bucketTotals [endBuckOffset];
693 hist->totalCount = totalCount;
694 hist->totalVal = totalVal;
709 kkint32 lastBeforeIDX = bucket;
712 if (buckets[bucket] == (
float)0.0)
715 for (
kkint32 x = 1; x <= tolerance; x++)
723 beforeIDX = beforeIDX + numOfBuckets;
729 afterIDX = afterIDX - numOfBuckets;
734 if (buckets [beforeIDX] > buckets[lastBeforeIDX])
738 if (afterIDX < numOfBuckets)
740 if (buckets [afterIDX] > buckets[lastAfterIDX])
744 lastBeforeIDX = beforeIDX;
745 lastAfterIDX = afterIDX;
760 peaks.erase (peaks.begin (), peaks.end ());
762 for (bucket = 0; bucket < numOfBuckets; bucket++)
764 if (IsBucketAPeak (bucket, threshold))
765 peaks.push_back (bucket);
776 if (peakNum > (kkint32)peaks.size ())
780 return peaks[peakNum];
790 if (peakNum > (kkint32)peaks.size ())
799 for (x = 1; x < (kkint32)sortedPeaks.size (); x++)
801 for (y = x; y > 0; y--)
803 if (buckets[sortedPeaks[y - 1]] < buckets[sortedPeaks[y]])
806 sortedPeaks[y] = sortedPeaks[y - 1];
807 sortedPeaks[y - 1] = t;
812 kkint32 returnIdxNum = sortedPeaks[peakNum];
827 cout <<
"GetPeakAvgByHighestOrder GetPeakByHighestOrder returned[" << x <<
"]" << std::endl;
831 if (x >=
this->numOfBuckets)
834 cout <<
"GetPeakAvgByHighestOrder GetPeakByHighestOrder returned[" << x <<
"]" << std::endl;
839 if (buckets[x] <= (
float)0.0)
842 return (bucketTotals[x] / buckets[x]);
850 float* histSumTable =
new float[numOfBuckets];
853 HistogramPtr newHist =
new Histogram (minValue
, numOfBuckets
, bucketSize
, wrapArround
);
857 newHist->equalizedMapTable =
new kkint32[numOfBuckets];
859 float accumulatedCount = (
float)0.0;
860 for (x = 0; x < numOfBuckets; x++)
862 accumulatedCount += buckets[x];
863 histSumTable[x] = accumulatedCount;
867 for (x = 0; x < numOfBuckets; x++)
869 idx = (
kkint32)((
float)0.5 + (((
float)(histSumTable[x] * (numOfBuckets - 1)) / totalCount)));
870 if (idx >= numOfBuckets)
871 idx = numOfBuckets - 1;
873 newHist->equalizedMapTable[x] = idx;
874 newHist->buckets[idx] += buckets[x];
875 newHist->bucketTotals[idx] += bucketTotals[x];
879 newHist->totalCount = totalCount;
880 newHist->totalVal = totalVal;
883 delete[] histSumTable;
895 if ((minBucket < 0) || (minBucket >= numOfBuckets) ||
896 (maxBucket < 0) || (maxBucket >= numOfBuckets) ||
897 (minBucket > maxBucket)
901 cerr <<
"Histogram::AverageOfMaxBucketInRange *** ERROR *** Invalid Parameters" << std::endl;
902 cerr <<
"NumOfBuckets[" << numOfBuckets <<
"] MinBucket[" << minBucket <<
"] MaxBucket[" << maxBucket <<
"]" << std::endl;
909 kkint32 maxBucketIDX = minBucket;
910 float maxBucketVal = buckets[minBucket];
912 for (b = minBucket; b <= maxBucket; b++)
914 if (buckets[b] > maxBucketVal)
917 maxBucketVal = buckets[b];
921 if (maxBucketVal > (
float)0.0)
922 return bucketTotals[maxBucketIDX] / buckets[maxBucketIDX];
934 if ((firstBucket < 0) || (firstBucket >= numOfBuckets) ||
935 (lastBucket < 0) || (lastBucket >= numOfBuckets) ||
936 (firstBucket > lastBucket)
940 cerr <<
"Histogram::AverageOfMaxBucketInRange *** ERROR *** Invalid Parameters" << std::endl;
941 cerr <<
"NumOfBuckets[" << numOfBuckets <<
"] FirstBucket[" << firstBucket <<
"] LastBucket[" << lastBucket <<
"]" << std::endl;
947 kkint32 minBucketIDX = firstBucket;
948 float minBucketVal = buckets[firstBucket];
950 for (b = firstBucket; b <= lastBucket; b++)
952 if (buckets[b] <= minBucketVal)
955 minBucketVal = buckets[b];
959 if (minBucketVal > 0)
960 return bucketTotals[minBucketIDX] / buckets[minBucketIDX];
971 if ((minBucket < 0) || (minBucket >= numOfBuckets) ||
972 (maxBucket < 0) || (maxBucket >= numOfBuckets) ||
973 (minBucket > maxBucket)
977 cerr <<
"Histogram::AverageOfMaxBucketExcludingRange *** ERROR *** Invalid Parameters" << std::endl;
978 cerr <<
"NumOfBuckets[" << numOfBuckets <<
"] MinBucket[" << minBucket <<
"] MaxBucket[" << maxBucket <<
"]" << std::endl;
985 float maxBucketVal = 0;
987 for (b = 0; b < numOfBuckets; b++)
989 if ((b < minBucket) || (b > maxBucket))
991 if (buckets[b] > maxBucketVal)
994 maxBucketVal = buckets[b];
999 if (maxBucketIDX < 0)
1002 if (maxBucketVal > (
float)0.0)
1003 return bucketTotals[maxBucketIDX] / buckets[maxBucketIDX];
1014 if ((minBucket < 0) || (minBucket >= numOfBuckets) ||
1015 (maxBucket < 0) || (maxBucket >= numOfBuckets) ||
1016 (minBucket > maxBucket)
1020 cerr <<
"Histogram::AreaInRange *** ERROR *** Invalid Parameters" << std::endl;
1021 cerr <<
"NumOfBuckets[" << numOfBuckets <<
"] MinBucket[" << minBucket <<
"] MaxBucket[" << maxBucket <<
"]" << std::endl;
1025 float area = (
float)0.0;
1026 for (
kkint32 b = minBucket; b <= maxBucket; b++)
1031 return (
kkint32)(area + (
float)0.5);
1040 if (totalCount <= (
float)0.0)
1043 return (
float)100.0 * (
float)((
float)
AreaInRange (minBucket
, maxBucket
) / totalCount);
1054 for (x = 1; x < numOfBuckets; x++)
1062 for (x = 1; x < numOfBuckets; x++)
1064 o
<< "\t" << buckets[x];
1079 mean = totalCount / numOfBuckets;
1082 float deltaSquareTotal = 0.0f;
1083 for (
kkint32 idx = 0; idx < numOfBuckets; idx++)
1085 if (buckets[idx] < min) min = buckets[idx];
1086 if (buckets[idx] > max) max = buckets[idx];
1088 float delta = buckets[idx] - mean;
1089 float deltaSquare = delta * delta;
1090 deltaSquareTotal += deltaSquare;
1092 variance = deltaSquareTotal / (
float)numOfBuckets;
void CalculatePeaks(kkint32 threshold)
kkint32 MaxBucketIdx() const
void Save(KKStr fileName) const
void DrawLine(kkint32 bpRow, kkint32 bpCol, kkint32 epRow, kkint32 epCol, uchar pixelVal)
void SetPixelValue(kkint32 row, kkint32 col, uchar pixVal)
float AreaInRangePercent(kkint32 minBucket, kkint32 maxBucket) const
A class that is used by to represent a single image in memory.
float AverageOfMaxBucketExcludingRange(kkint32 minBucket, kkint32 maxBucket) const
float AverageOfMinBucketInRange(kkint32 minBucket, kkint32 maxBucket) const
RasterPtr CreateGraph() const
void PrintTable(ostream &o)
Raster(kkint32 _height, kkint32 _width)
Constructs a blank image with given dimensions; all pixels will be initialized to 0...
RasterPtr CreateGraph(kkint32 barSize) const
kkint32 GetPeakByHighestOrder(kkint32 peakNum)
float AverageOfMaxBucketInRange(kkint32 firstBucket, kkint32 lastBucket) const
void SaveImage(const Raster &image, const KKStr &imageFileName)
kkint32 GetPeakBucket(kkint32 peakNum)
unsigned char uchar
Unsigned character.
float GetPeakAvgByHighestOrder(kkint32 peakNum)
Histogram * Smooth(kkint32 smoothWidth)
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of 'std::string' strings.
std::vector< kkint32 > VectorInt32
Vector of signed 32 bit integers.
Histogram(float _minValue, kkint32 _numOfBuckets, float _bucketSize, bool _wrapArround)
bool IsBucketAPeak(kkint32 bucket, kkint32 tolerance) const
float CountOfMaxBucket() const
std::ostream &__cdecl operator<<(std::ostream &os, const KKStr &str)
void GetStats(float &min, float &max, float &mean, float &variance)
void SaveGraphImage(const KKStr &fileName) const
void SaveGraphImage(const KKStr &fileName, kkint32 barSize) const
float AverageOfMaxBucket() const
void Increment(float val)
float Bucket(kkint32 bucket) const
Used to manage the construction of a Histogram.
kkint32 AreaInRange(kkint32 minBucket, kkint32 maxBucket) const