Engauge Digitizer 2
Loading...
Searching...
No Matches
ColorFilter Class Reference

Class for filtering image to remove unimportant information. More...

#include <ColorFilter.h>

Collaboration diagram for ColorFilter:
Collaboration graph

Public Member Functions

 ColorFilter ()
 Single constructor.
 ~ColorFilter ()
 Destructor deallocates memory.
bool colorCompare (QRgb rgb1, QRgb rgb2) const
 See if the two color values are close enough to be considered to be the same.
void filterImage (const QImage &imageOriginal, QImage &imageFiltered, ColorFilterMode colorFilterMode, double low, double high, QRgb rgbBackground)
 Filter the original image according to the specified filtering parameters.
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.
bool pixelFilteredIsOn (const QImage &image, int x, int y) const
 Return true if specified filtered pixel is on.
double pixelToZeroToOneOrMinusOne (ColorFilterMode colorFilterMode, const QColor &pixel, QRgb rgbBackground) const
 Return pixel converted according to the current filter parameter, normalized to zero to one.
bool pixelUnfilteredIsOn (ColorFilterMode colorFilterMode, const QColor &pixel, QRgb rgbBackground, double low0To1, double high0To1) const
 Return true if specified unfiltered pixel is on.
int zeroToOneToValue (ColorFilterMode colorFilterMode, double s) const
 Inverse of pixelToZeroToOneOrMinusOne.

Detailed Description

Class for filtering image to remove unimportant information.

Definition at line 20 of file ColorFilter.h.

Constructor & Destructor Documentation

◆ ColorFilter()

ColorFilter::ColorFilter ( )

Single constructor.

Definition at line 21 of file ColorFilter.cpp.

22{
23 createStrategies ();
24}

◆ ~ColorFilter()

ColorFilter::~ColorFilter ( )

Destructor deallocates memory.

Definition at line 26 of file ColorFilter.cpp.

27{
28 qDeleteAll (m_strategies);
29}

Member Function Documentation

◆ colorCompare()

bool ColorFilter::colorCompare ( QRgb rgb1,
QRgb rgb2 ) const

See if the two color values are close enough to be considered to be the same.

Definition at line 31 of file ColorFilter.cpp.

33{
34 const long MASK = 0xf0f0f0f0;
35 return (rgb1 & MASK) == (rgb2 & MASK);
36}

◆ filterImage()

void ColorFilter::filterImage ( const QImage & imageOriginal,
QImage & imageFiltered,
ColorFilterMode colorFilterMode,
double low,
double high,
QRgb rgbBackground )

Filter the original image according to the specified filtering parameters.

Definition at line 47 of file ColorFilter.cpp.

53{
54 ENGAUGE_ASSERT (imageOriginal.width () == imageFiltered.width());
55 ENGAUGE_ASSERT (imageOriginal.height() == imageFiltered.height());
56 ENGAUGE_ASSERT (imageFiltered.format () == QImage::Format_RGB32);
57
58 for (int x = 0; x < imageOriginal.width(); x++) {
59 for (int y = 0; y < imageOriginal.height (); y++) {
60
61 QColor pixel = imageOriginal.pixel (x, y);
62 bool isOn = false;
63 if (pixel.rgb() != rgbBackground) {
64
65 isOn = pixelUnfilteredIsOn (colorFilterMode,
66 pixel,
67 rgbBackground,
68 low,
69 high);
70 }
71
72 imageFiltered.setPixel (x, y, (isOn ?
73 QColor (Qt::black).rgb () :
74 QColor (Qt::white).rgb ()));
75 }
76 }
77}
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT.
bool pixelUnfilteredIsOn(ColorFilterMode colorFilterMode, const QColor &pixel, QRgb rgbBackground, double low0To1, double high0To1) const
Return true if specified unfiltered pixel is on.

◆ marginColor()

QRgb ColorFilter::marginColor ( const QImage * image) const

Identify the margin color of the image, which is defined as the most common color in the four margins.

For speed, only pixels in the four borders are examined, with the results from those borders safely representing the most common color of the entire margin areas.

Definition at line 79 of file ColorFilter.cpp.

80{
81 // Add unique colors to colors list
82 ColorList colorCounts;
83 for (int x = 0; x < image->width (); x++) {
84 mergePixelIntoColorCounts (image->pixel (x, 0), colorCounts);
85 mergePixelIntoColorCounts (image->pixel (x, image->height () - 1), colorCounts);
86 }
87 for (int y = 0; y < image->height (); y++) {
88 mergePixelIntoColorCounts (image->pixel (0, y), colorCounts);
89 mergePixelIntoColorCounts (image->pixel (image->width () - 1, y), colorCounts);
90 }
91
92 // Margin color is the most frequent color
93 ColorFilterEntry entryMax;
94 entryMax.count = 0;
95 for (ColorList::const_iterator itr = colorCounts.begin (); itr != colorCounts.end (); itr++) {
96 if ((*itr).count > entryMax.count) {
97 entryMax = *itr;
98 }
99 }
100
101 return entryMax.color.rgb();
102}
QColor color
Unique color entry.
unsigned int count
Number of times this color has appeared.

◆ pixelFilteredIsOn()

bool ColorFilter::pixelFilteredIsOn ( const QImage & image,
int x,
int y ) const

Return true if specified filtered pixel is on.

Definition at line 127 of file ColorFilter.cpp.

130{
131 bool rtn = false;
132
133 if ((0 <= x) &&
134 (0 <= y) &&
135 (x < image.width()) &&
136 (y < image.height())) {
137
138 // Pixel is on if it is closer to black than white in gray scale. This test must be performed
139 // on little endian and big endian systems, with or without alpha bits (which are typically high bits);
140 const int BLACK_WHITE_THRESHOLD = 255 / 2; // Put threshold in middle of range
141 int gray = qGray (pixelRGB (image, x, y));
142 rtn = (gray < BLACK_WHITE_THRESHOLD);
143
144 }
145
146 return rtn;
147}
QRgb pixelRGB(const QImage &image, int x, int y)
Get pixel method for any bit depth.
Definition mmsubs.cpp:209

◆ pixelToZeroToOneOrMinusOne()

double ColorFilter::pixelToZeroToOneOrMinusOne ( ColorFilterMode colorFilterMode,
const QColor & pixel,
QRgb rgbBackground ) const

Return pixel converted according to the current filter parameter, normalized to zero to one.

Special case is -1 for a pixel that cannot be converted, like finding hue value for gray scale pixel

Definition at line 177 of file ColorFilter.cpp.

180{
181 if (m_strategies.contains (colorFilterMode)) {
182
183 // Ignore false positive cmake compiler warning about -Wreturn-stack-address in next line (bug #26396)
184 const ColorFilterStrategyAbstractBase *strategy = m_strategies [colorFilterMode];
185 return strategy->pixelToZeroToOne (pixel,
186 rgbBackground);
187
188 } else {
189
190 LOG4CPP_ERROR_S ((*mainCat)) << "ColorFilter::pixelToZeroToOneOrMinusOne is missing color filter mode";
191 ENGAUGE_ASSERT (false);
192 return 0.0;
193
194 }
195}
log4cpp::Category * mainCat
Definition Logger.cpp:14
virtual double pixelToZeroToOne(const QColor &pixel, QRgb rgbBackground) const =0
Return a normalized value of 0 to 1 given input pixel.
#define LOG4CPP_ERROR_S(logger)
Definition convenience.h:12

◆ pixelUnfilteredIsOn()

bool ColorFilter::pixelUnfilteredIsOn ( ColorFilterMode colorFilterMode,
const QColor & pixel,
QRgb rgbBackground,
double low0To1,
double high0To1 ) const

Return true if specified unfiltered pixel is on.

Definition at line 149 of file ColorFilter.cpp.

154{
155 bool rtn = false;
156
157 double s = pixelToZeroToOneOrMinusOne (colorFilterMode,
158 pixel,
159 rgbBackground);
160 if (s >= 0.0) {
161 if (low0To1 <= high0To1) {
162
163 // Single valid range
164 rtn = (low0To1 <= s) && (s <= high0To1);
165
166 } else {
167
168 // Two ranges
169 rtn = (s <= high0To1) || (low0To1 <= s);
170
171 }
172 }
173
174 return rtn;
175}
double pixelToZeroToOneOrMinusOne(ColorFilterMode colorFilterMode, const QColor &pixel, QRgb rgbBackground) const
Return pixel converted according to the current filter parameter, normalized to zero to one.

◆ zeroToOneToValue()

int ColorFilter::zeroToOneToValue ( ColorFilterMode colorFilterMode,
double s ) const

Inverse of pixelToZeroToOneOrMinusOne.

Definition at line 197 of file ColorFilter.cpp.

199{
200 if (m_strategies.contains (colorFilterMode)) {
201
202 const ColorFilterStrategyAbstractBase *strategy = m_strategies [colorFilterMode];
203 return strategy->zeroToOneToValue (s);
204
205 } else {
206
207 LOG4CPP_ERROR_S ((*mainCat)) << "ColorFilter::zeroToOneToValue is missing color filter mode";
208 ENGAUGE_ASSERT (false);
209 return 0;
210
211 }
212}
virtual int zeroToOneToValue(double s) const =0
Return the low value normalized to 0 to 1.

The documentation for this class was generated from the following files: