-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgorithmSinglePixelSwapHsv.cpp
71 lines (59 loc) · 2.52 KB
/
AlgorithmSinglePixelSwapHsv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "AlgorithmSinglePixelSwapHsv.h"
AlgorithmSinglePixelSwapHsv::AlgorithmSinglePixelSwapHsv(QObject* parent)
: AlgorithmBase(parent)
{
}
QString AlgorithmSinglePixelSwapHsv::GetName()
{
return "Pixel H, S & V error";
}
QString AlgorithmSinglePixelSwapHsv::GetDescription()
{
return "<p>Randomly chooses two pixels, and swaps them if the total colour "
"error would be less.</p>"
"<p>First, the colour of each pixel is converted to the HSV "
"colourspace, then the pixels are compared against the target image"
"to calculate the error, i.e. the difference in HSV values between "
"the rgb image and the target image.</p>"
"<p>The error calculation can take advantage of the HSV colour "
"representation and a quirk of human vision, we are more perceptive "
"at luminosity (light/dark) differences than hue (colour) "
"differences. Therefore when calculating error, luminosity error is "
"considered more important than colour error</p>";
}
void AlgorithmSinglePixelSwapHsv::Iterate()
{
// Do a block of repeats in one go for efficiency
const int repeats = 100000;
for (int i = 0; i < repeats; ++i) {
QPoint aLoc{ RandomNumber(0, 4096 - 1), RandomNumber(0, 4096 - 1) };
QPoint bLoc{ RandomNumber(0, 4096 - 1), RandomNumber(0, 4096 - 1) };
QColor sourceA = allRgb_.pixelColor(aLoc);
QColor sourceB = allRgb_.pixelColor(bLoc);
QColor targetA = target_.pixelColor(aLoc);
QColor targetB = target_.pixelColor(bLoc);
float aDifference = GetHsvError(sourceA, targetA);
float bDifference = GetHsvError(sourceB, targetB);
float aSwappedDifference = GetHsvError(sourceA, targetB);
float bSwappedDifference = GetHsvError(sourceB, targetA);
if ((aDifference + bDifference) > (aSwappedDifference + bSwappedDifference)) {
allRgb_.setPixel(aLoc, sourceB.rgb());
allRgb_.setPixel(bLoc, sourceA.rgb());
++improvements_;
}
}
iterations_ += repeats;
emit onIterationsChanged(iterations_);
emit onImprovementsChanged(improvements_);
}
float AlgorithmSinglePixelSwapHsv::GetHsvError(const QColor& a, const QColor& b) const
{
float ah, as, av;
float bh, bs, bv;
a.getHsvF(&ah, &as, &av);
b.getHsvF(&bh, &bs, &bv);
float hError = std::abs(ah - bh) * 0.9;
float sError = std::abs(as - bs) * 1.0;
float vError = std::abs(av - bv) * 1.1;
return hError + sError + vError;
}