Engauge Digitizer  2
ColorFilterHistogram.cpp
1 #include "ColorFilter.h"
2 #include "ColorFilterHistogram.h"
3 #include "EngaugeAssert.h"
4 #include <QImage>
5 
7 {
8 }
9 
11  ColorFilterMode colorFilterMode,
12  const QColor &pixel,
13  const QRgb &rgbBackground) const
14 {
15  // Instead of mapping from s=0 through 1 to bin=0 through HISTOGRAM_BINS-1, we
16  // map it to bin=1 through HISTOGRAM_BINS-2 so first and last bin are zero. The
17  // result is a peak at the start or end is complete and easier to read
18  double s = filter.pixelToZeroToOneOrMinusOne (colorFilterMode,
19  pixel,
20  rgbBackground);
21  ENGAUGE_ASSERT (s <= 1.0);
22 
23  int bin = -1;
24 
25  if (s >= 0) {
26 
27  bin = FIRST_NON_EMPTY_BIN_AT_START () + s * (LAST_NON_EMPTY_BIN_AT_END () - FIRST_NON_EMPTY_BIN_AT_START ());
28 
29  }
30 
31  return bin;
32 }
33 
35  double histogramBins [],
36  ColorFilterMode colorFilterMode,
37  const QImage &image,
38  int &maxBinCount) const
39 {
40  // Initialize histogram bins
41  int bin;
42  for (bin = 0; bin < HISTOGRAM_BINS (); bin++) {
43  histogramBins [bin] = 0;
44  }
45 
46  QRgb rgbBackground = filter.marginColor(&image);
47 
48  // Populate histogram bins
49  maxBinCount = 0;
50  for (int x = 0; x < image.width(); x++) {
51  for (int y = 0; y < image.height(); y++) {
52 
53  QColor pixel (image.pixel (x, y));
54  int bin = binFromPixel (filter,
55  colorFilterMode,
56  pixel,
57  rgbBackground);
58  if (bin >= 0) {
59 
60  ENGAUGE_ASSERT ((FIRST_NON_EMPTY_BIN_AT_START () <= bin) &&
61  (LAST_NON_EMPTY_BIN_AT_END () >= bin));
62  ++(histogramBins [bin]);
63 
64  if (histogramBins [bin] > maxBinCount) {
65  maxBinCount = histogramBins [bin];
66  }
67  }
68  }
69  }
70 }
71 
73  ColorFilterMode colorFilterMode,
74  int bin)
75 {
76  // Just do everything in binFromPixel backwards
77  double s = (double) (bin - FIRST_NON_EMPTY_BIN_AT_START ()) / (double) (LAST_NON_EMPTY_BIN_AT_END () - FIRST_NON_EMPTY_BIN_AT_START ());
78  s = qMin (qMax (s, 0.0), 1.0);
79 
80  return filter.zeroToOneToValue (colorFilterMode,
81  s);
82 }
void generate(const ColorFilter &filter, double histogramBins[], ColorFilterMode colorFilterMode, const QImage &image, int &maxBinCount) const
Generate the histogram.
double pixelToZeroToOneOrMinusOne(ColorFilterMode colorFilterMode, const QColor &pixel, QRgb rgbBackground) const
Return pixel converted according to the current filter parameter, normalized to zero to one...
Class for filtering image to remove unimportant information.
Definition: ColorFilter.h:12
int zeroToOneToValue(ColorFilterMode colorFilterMode, double s) const
Inverse of pixelToZeroToOneOrMinusOne.
QRgb marginColor(const QImage *image) const
Identify the margin color of the image, which is defined as the most common color in the four margins...
Definition: ColorFilter.cpp:52
int binFromPixel(const ColorFilter &filter, ColorFilterMode colorFilterMode, const QColor &pixel, const QRgb &rgbBackground) const
Compute histogram bin number from pixel according to filter.
int valueFromBin(const ColorFilter &filter, ColorFilterMode colorFilterMode, int bin)
Inverse of binFromPixel.
ColorFilterHistogram()
Single constructor.
static int HISTOGRAM_BINS()
Number of histogram bins.