Skip to content

Commit

Permalink
Important calibration fix and stronger noise resistance
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Oct 3, 2024
1 parent 3acbc6e commit 4cb189a
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 100 deletions.
16 changes: 8 additions & 8 deletions include/lut-calibrator/CapturedColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,16 @@ class CapturedColor
int sourceRGBdelta = 0;
std::list<byte3> finalRGB;
double3 color;
std::map<double3, int> inputColors;
std::list<std::pair<double3, int>> sortedInputColors;
std::list<std::pair<byte3, int>> inputColors;
std::list<std::pair<byte3, int>> sortedInputYUVColors;
std::list<std::pair<double3, int>> sortedInputYuvColors;
byte3 min, max;
byte3 colorInt;

public:
CapturedColor() = default;

const double& y() const { return color.x; }
const double& u() const { return color.y; }
const double& v() const { return color.z; }
const double3& yuv() const { return color; }

const uint8_t& Y() const { return colorInt.x; }
Expand All @@ -69,13 +68,14 @@ class CapturedColor
bool calculateFinalColor();
bool hasAllSamples();
bool hasAnySample();
std::list<std::pair<double3, int>> getInputColors();
std::list<std::pair<byte3, int>> getInputYUVColors() const;
std::list<std::pair<double3, int>> getInputYuvColors() const;
void addColor(ColorRgb i);
void addColor(const double3& i);
void addColor(const byte3& i);
void setSourceRGB(byte3 _color);
int getSourceError(const int3& _color);
int getSourceError(const int3& _color) const;
int3 getSourceRGB() const;
void setFinalRgb(double3 _color);
void setFinalRGB(byte3 input);
std::list<byte3> getFinalRGB() const;

QString toString();
Expand Down
6 changes: 3 additions & 3 deletions sources/lut-calibrator/BoardUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,15 @@ namespace BoardUtils

for(const auto& currentArray : arrayMark)
{
myfile << currentArray.first;
myfile << "capturedData = " << currentArray.first;
for (int r = 0; r < SCREEN_COLOR_DIMENSION; r++)
{
for (int g = 0; g < SCREEN_COLOR_DIMENSION; g++)
{
myfile << std::endl << "\t";
for (int b = 0; b < SCREEN_COLOR_DIMENSION; b++)
{
auto elems = all[r][g][b].getInputColors();
auto elems = all[r][g][b].getInputYUVColors();
myfile << currentArray.first;

for (const auto& elem : elems)
Expand All @@ -509,7 +509,7 @@ namespace BoardUtils
}
}
}
myfile << std::endl << currentArray.second << std::endl;
myfile << std::endl << currentArray.second << ";" << std::endl;
}

myfile.close();
Expand Down
52 changes: 39 additions & 13 deletions sources/lut-calibrator/CapturedColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,42 @@ bool CapturedColor::calculateFinalColor()
int count = 0;
color = double3{ 0,0,0 };

sortedInputYUVColors.clear();
sortedInputYuvColors.clear();
for (auto iter = inputColors.begin(); iter != inputColors.end(); ++iter)
{
color += ((*iter).first) * ((*iter).second);
count += ((*iter).second);

// sort
bool inserted = false;
for (auto sorted = sortedInputColors.begin(); sorted != sortedInputColors.end(); ++sorted)
for (auto sorted = sortedInputYUVColors.begin(); sorted != sortedInputYUVColors.end(); ++sorted)
if (((*iter).second) > (*sorted).second)
{
sortedInputColors.insert(sorted, std::pair<double3, int>((*iter).first, (*iter).second));
sortedInputYUVColors.insert(sorted, std::pair<byte3, int>((*iter).first, (*iter).second));
inserted = true;
break;
}

if (!inserted)
sortedInputColors.push_back(std::pair<double3, int>((*iter).first, (*iter).second));
sortedInputYUVColors.push_back(std::pair<byte3, int>((*iter).first, (*iter).second));
}

while(sortedInputYUVColors.size() > 3 || (sortedInputYUVColors.size() == 3 && sortedInputYUVColors.back().second <= 6))
sortedInputYUVColors.pop_back();

std::for_each(sortedInputYUVColors.begin(), sortedInputYUVColors.end(), [this](std::pair<byte3, int>& m) {

if (m.first.y >= 127 && m.first.y <= 129 && m.first.z >= 127 && m.first.z <= 129)
{
m.first.y = 128;
m.first.z = 128;
}

sortedInputYuvColors.push_back(std::pair<double3, int>(static_cast<double3>(m.first) / 255.0, m.second));
});


auto workColor = color / count;

colorInt = ColorSpaceMath::to_byte3(workColor);
Expand Down Expand Up @@ -118,10 +135,10 @@ bool CapturedColor::hasAnySample()

void CapturedColor::addColor(ColorRgb i)
{
addColor(double3(i.red, i.green, i.blue));
addColor(byte3{ i.red, i.green, i.blue });
}

void CapturedColor::addColor(const double3& i)
void CapturedColor::addColor(const byte3& i)
{
bool empty = !hasAnySample();

Expand All @@ -138,15 +155,20 @@ void CapturedColor::addColor(const double3& i)
max.y = i.y;
if (empty || max.z < i.z)
max.z = i.z;

auto findIter = std::find_if(inputColors.begin(), inputColors.end(), [&](auto& m) {
return m.first == i;
});

if (inputColors.find(i) == inputColors.end())
if (findIter == inputColors.end())
{
inputColors[i] = 1;
inputColors.push_back(std::pair<byte3, int>(i, 1));
}
else
{
inputColors[i] = inputColors[i] + 1;
(*findIter).second++;
}


totalSamples++;
}
Expand All @@ -168,9 +190,8 @@ int3 CapturedColor::getSourceRGB() const
}


void CapturedColor::setFinalRgb(double3 _color)
void CapturedColor::setFinalRGB(byte3 input)
{
auto input = ColorSpaceMath::to_byte3(_color);
bool found = (std::find(finalRGB.begin(), finalRGB.end(), input) != finalRGB.end());
if (!found)
{
Expand All @@ -183,12 +204,17 @@ std::list<byte3> CapturedColor::getFinalRGB() const
return finalRGB;
}

std::list<std::pair<double3, int>> CapturedColor::getInputColors()
std::list<std::pair<byte3, int>> CapturedColor::getInputYUVColors() const
{
return sortedInputYUVColors;
}

std::list<std::pair<double3, int>> CapturedColor::getInputYuvColors() const
{
return sortedInputColors;
return sortedInputYuvColors;
}

int CapturedColor::getSourceError(const int3& _color)
int CapturedColor::getSourceError(const int3& _color) const
{
if (sourceRGBdelta == 0)
{
Expand Down
Loading

0 comments on commit 4cb189a

Please sign in to comment.