Engauge Digitizer 2
Loading...
Searching...
No Matches
ColorFilterHistogram.cpp
Go to the documentation of this file.
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"
9#include "EngaugeAssert.h"
10#include <QImage>
11#include <qmath.h>
12
16
18 ColorFilterMode colorFilterMode,
19 const QColor &pixel,
20 const QRgb &rgbBackground) const
21{
22 // Instead of mapping from s=0 through 1 to bin=0 through HISTOGRAM_BINS-1, we
23 // map it to bin=1 through HISTOGRAM_BINS-2 so first and last bin are zero. The
24 // result is a peak at the start or end is complete and easier to read
25 double s = filter.pixelToZeroToOneOrMinusOne (colorFilterMode,
26 pixel,
27 rgbBackground);
28 ENGAUGE_ASSERT (s <= 1.0);
29
30 int bin = -1;
31
32 if (s >= 0) {
33
34 bin = qFloor (FIRST_NON_EMPTY_BIN_AT_START () + s * (LAST_NON_EMPTY_BIN_AT_END () - FIRST_NON_EMPTY_BIN_AT_START ()));
35
36 }
37
38 return bin;
39}
40
42 double histogramBins [],
43 ColorFilterMode colorFilterMode,
44 const QImage &image,
45 int &maxBinCount) const
46{
47 // Initialize histogram bins
48 int bin;
49 for (bin = 0; bin < HISTOGRAM_BINS (); bin++) {
50 histogramBins [bin] = 0;
51 }
52
53 QRgb rgbBackground = filter.marginColor(&image);
54
55 // Populate histogram bins
56 maxBinCount = 0;
57 for (int x = 0; x < image.width(); x++) {
58 for (int y = 0; y < image.height(); y++) {
59
60 QColor pixel (image.pixel (x, y));
61 int bin = binFromPixel (filter,
62 colorFilterMode,
63 pixel,
64 rgbBackground);
65 if (bin >= 0) {
66
67 ENGAUGE_ASSERT ((FIRST_NON_EMPTY_BIN_AT_START () <= bin) &&
68 (LAST_NON_EMPTY_BIN_AT_END () >= bin));
69 ++(histogramBins [bin]);
70
71 if (histogramBins [bin] > maxBinCount) {
72 maxBinCount = qFloor (histogramBins [bin]);
73 }
74 }
75 }
76 }
77}
78
80 ColorFilterMode colorFilterMode,
81 int bin)
82{
83 // Just do everything in binFromPixel backwards
84 double s = double (bin - FIRST_NON_EMPTY_BIN_AT_START ()) / double (LAST_NON_EMPTY_BIN_AT_END () - FIRST_NON_EMPTY_BIN_AT_START ());
85 s = qMin (qMax (s, 0.0), 1.0);
86
87 return filter.zeroToOneToValue (colorFilterMode,
88 s);
89}
ColorFilterMode
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT.
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:21
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...
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.