Engauge Digitizer 2
ColorFilterHistogram.cpp
1/******************************************************************************************************
2 * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3 * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4 * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5 ******************************************************************************************************/
6
7#include "ColorFilter.h"
8#include "ColorFilterHistogram.h"
9#include "EngaugeAssert.h"
10#include <QImage>
11
13{
14}
15
17 ColorFilterMode colorFilterMode,
18 const QColor &pixel,
19 const QRgb &rgbBackground) const
20{
21 // Instead of mapping from s=0 through 1 to bin=0 through HISTOGRAM_BINS-1, we
22 // map it to bin=1 through HISTOGRAM_BINS-2 so first and last bin are zero. The
23 // result is a peak at the start or end is complete and easier to read
24 double s = filter.pixelToZeroToOneOrMinusOne (colorFilterMode,
25 pixel,
26 rgbBackground);
27 ENGAUGE_ASSERT (s <= 1.0);
28
29 int bin = -1;
30
31 if (s >= 0) {
32
33 bin = FIRST_NON_EMPTY_BIN_AT_START () + s * (LAST_NON_EMPTY_BIN_AT_END () - FIRST_NON_EMPTY_BIN_AT_START ());
34
35 }
36
37 return bin;
38}
39
41 double histogramBins [],
42 ColorFilterMode colorFilterMode,
43 const QImage &image,
44 int &maxBinCount) const
45{
46 // Initialize histogram bins
47 int bin;
48 for (bin = 0; bin < HISTOGRAM_BINS (); bin++) {
49 histogramBins [bin] = 0;
50 }
51
52 QRgb rgbBackground = filter.marginColor(&image);
53
54 // Populate histogram bins
55 maxBinCount = 0;
56 for (int x = 0; x < image.width(); x++) {
57 for (int y = 0; y < image.height(); y++) {
58
59 QColor pixel (image.pixel (x, y));
60 int bin = binFromPixel (filter,
61 colorFilterMode,
62 pixel,
63 rgbBackground);
64 if (bin >= 0) {
65
66 ENGAUGE_ASSERT ((FIRST_NON_EMPTY_BIN_AT_START () <= bin) &&
67 (LAST_NON_EMPTY_BIN_AT_END () >= bin));
68 ++(histogramBins [bin]);
69
70 if (histogramBins [bin] > maxBinCount) {
71 maxBinCount = histogramBins [bin];
72 }
73 }
74 }
75 }
76}
77
79 ColorFilterMode colorFilterMode,
80 int bin)
81{
82 // Just do everything in binFromPixel backwards
83 double s = (double) (bin - FIRST_NON_EMPTY_BIN_AT_START ()) / (double) (LAST_NON_EMPTY_BIN_AT_END () - FIRST_NON_EMPTY_BIN_AT_START ());
84 s = qMin (qMax (s, 0.0), 1.0);
85
86 return filter.zeroToOneToValue (colorFilterMode,
87 s);
88}
void generate(const ColorFilter &filter, double histogramBins[], ColorFilterMode colorFilterMode, const QImage &image, int &maxBinCount) const
Generate the histogram.
ColorFilterHistogram()
Single constructor.
int valueFromBin(const ColorFilter &filter, ColorFilterMode colorFilterMode, int bin)
Inverse of binFromPixel.
static int HISTOGRAM_BINS()
Number of histogram bins.
int binFromPixel(const ColorFilter &filter, ColorFilterMode colorFilterMode, const QColor &pixel, const QRgb &rgbBackground) const
Compute histogram bin number from pixel according to filter.
Class for filtering image to remove unimportant information.
Definition: ColorFilter.h:19
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:58
int zeroToOneToValue(ColorFilterMode colorFilterMode, double s) const
Inverse of pixelToZeroToOneOrMinusOne.
double pixelToZeroToOneOrMinusOne(ColorFilterMode colorFilterMode, const QColor &pixel, QRgb rgbBackground) const
Return pixel converted according to the current filter parameter, normalized to zero to one.