Skip to content

Commit

Permalink
More precise color capture saving/loading
Browse files Browse the repository at this point in the history
New calibration_captured_yuv.txt format
  • Loading branch information
awawa-dev committed Sep 15, 2024
1 parent b1c3fbb commit 7832376
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
4 changes: 3 additions & 1 deletion include/lut-calibrator/BoardUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace BoardUtils
const int SCREEN_COLOR_STEP = 16;
const int SCREEN_COLOR_DIMENSION = (256 / SCREEN_COLOR_STEP) + 1;

const int IMPORT_SCALE = 1000000;

const int SCREEN_YUV_RANGE_LIMIT = 2;

const int SCREEN_CRC_LINES = 2;
Expand All @@ -59,7 +61,7 @@ namespace BoardUtils
const int SCREEN_LAST_BOARD_INDEX = std::pow(SCREEN_COLOR_DIMENSION, 3) / SCREEN_SAMPLES_PER_BOARD;

int indexToColorAndPos(int index, byte3& color, int2& position);
CapturedColor readBlock(const Image<ColorRgb>& yuvImage, int2 position);
CapturedColor readBlock(const Image<ColorRgb>& yuvImage, int2 position, byte3* _color = nullptr);
void getWhiteBlackColorLevels(const Image<ColorRgb>& yuvImage, CapturedColor& white, CapturedColor& black, int& line);
bool verifyBlackColorPattern(const Image<ColorRgb>& yuvImage, bool isFirstWhite, CapturedColor& black);
bool parseBoard(Logger* _log, const Image<ColorRgb>& yuvImage, int& boardIndex, CapturedColors& allColors);
Expand Down
1 change: 1 addition & 0 deletions include/lut-calibrator/CapturedColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class CapturedColor

bool calculateFinalColor();
void addColor(ColorRgb i);
void addColor(double3 i);
void setSourceRGB(byte3 _color);
int getSourceError(const int3& _color);
int3 getSourceRGB() const;
Expand Down
17 changes: 10 additions & 7 deletions sources/lut-calibrator/BoardUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ namespace BoardUtils
return boardIndex;
}

CapturedColor readBlock(const Image<ColorRgb>& yuvImage, int2 position)
CapturedColor readBlock(const Image<ColorRgb>& yuvImage, int2 position, byte3* _color)
{
const double2 delta(yuvImage.width() / (double)SCREEN_BLOCKS_X, yuvImage.height() / (double)SCREEN_BLOCKS_Y);
CapturedColor color;

if (_color != nullptr)
color.setSourceRGB(*_color);

const double2 positionF{ position };
const double2 startF = positionF * delta;
const double2 endF = ((positionF + double2(1, 1)) * delta) - double2(1, 1);
Expand Down Expand Up @@ -206,7 +209,7 @@ namespace BoardUtils

try
{
auto capturedColor = readBlock(yuvImage, position);
auto capturedColor = readBlock(yuvImage, position, &color);
capturedColor.setSourceRGB(color);
allColors.all[R][G][B] = capturedColor;
}
Expand Down Expand Up @@ -446,7 +449,7 @@ namespace BoardUtils

std::list<std::pair<const char*, const char*>> arrayMark = { {"np.array([", "])"}, {"{", "}"} };

myfile << "// Values of the table represent YUV in range [0 - 255], limited of full" << std::endl;
myfile << "// Values of the table represent YUV in range [0 - 1] * " << std::to_string(IMPORT_SCALE) << ", limited of full" << std::endl;
myfile << "// Each row of the " << std::to_string(SCREEN_COLOR_DIMENSION) <<"^3 table consist of the following RGB coordinates:" << std::endl << "// [ ";

for (int i = 0, j = 0; i < SCREEN_COLOR_DIMENSION; i++, j += SCREEN_COLOR_STEP)
Expand All @@ -469,11 +472,11 @@ namespace BoardUtils
myfile << "\t\t" << currentArray.first << std::endl << "\t\t\t";
for (int b = 0; b < SCREEN_COLOR_DIMENSION; b++)
{
const auto& elem = all[r][g][b];
const auto& elem = all[r][g][b].yuv();
myfile << currentArray.first;
myfile << " " << std::to_string(elem.Y()) << ", ";
myfile << std::to_string(elem.U()) << ", ";
myfile << std::to_string(elem.V()) << " ";
myfile << " " << std::to_string(std::lround(elem.x * IMPORT_SCALE)) << ", ";
myfile << std::to_string(std::lround(elem.y * IMPORT_SCALE)) << ", ";
myfile << std::to_string(std::lround(elem.z * IMPORT_SCALE)) << " ";
myfile << currentArray.second << ((b + 1 < SCREEN_COLOR_DIMENSION) ? "," : "");
}
myfile << std::endl << "\t\t" << currentArray.second << ((g + 1 < SCREEN_COLOR_DIMENSION) ? "," : "") << std::endl;
Expand Down
25 changes: 23 additions & 2 deletions sources/lut-calibrator/CapturedColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ bool CapturedColor::calculateFinalColor()
totalMinYUV = { std::min(totalMinYUV.x, min.x), std::min(totalMinYUV.y, min.y), std::min(totalMinYUV.z, min.z) };
totalMaxYUV = { std::max(totalMaxYUV.x, max.x), std::max(totalMaxYUV.y, max.y), std::max(totalMaxYUV.z, max.z) };

colorInt = byte3(color / count);
if (colorInt.y >= 127 && colorInt.y <= 129 && colorInt.z >= 127 && colorInt.z <= 129)
colorInt = ColorSpaceMath::to_byte3(color / count);
if (sourceRGB.x == sourceRGB.y && sourceRGB.y == sourceRGB.z)
{
colorInt.y = colorInt.z = 128;
color.y = color.z = (count * 128.0);
Expand Down Expand Up @@ -78,6 +78,27 @@ void CapturedColor::addColor(ColorRgb i)
count++;
}

void CapturedColor::addColor(double3 i)
{
color += i;

if (count == 0 || min.x > i.x)
min.x = i.x;
if (count == 0 || min.y > i.y)
min.y = i.y;
if (count == 0 || min.z > i.z)
min.z = i.z;

if (count == 0 || max.x < i.x)
max.x = i.x;
if (count == 0 || max.y < i.y)
max.y = i.y;
if (count == 0 || max.z < i.z)
max.z = i.z;

count++;
}

byte3 CapturedColor::getMaxYUV()
{
return totalMaxYUV;
Expand Down
15 changes: 13 additions & 2 deletions sources/lut-calibrator/LutCalibrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ void LutCalibrator::fineTune()

for (double gammaHLG : gammasHLG)
{
if (gammaHLG == 1.1 && bestResult->gammaHLG == 0)
if (gammaHLG == 1.1 && bestResult->gamma != HDR_GAMMA::HLG)
break;

if (gamma == HDR_GAMMA::HLG)
Expand Down Expand Up @@ -1034,6 +1034,7 @@ void LutCalibrator::capturedPrimariesCorrection(ColorSpaceMath::HDR_GAMMA gamma,

bool LutCalibrator::setTestData()
{
bool oldFormat = false;
std::vector<std::vector<std::vector<std::vector<int>>>> testData;

// asssign your test data from calibration_captured_yuv.txt to testData here
Expand All @@ -1054,7 +1055,17 @@ bool LutCalibrator::setTestData()
int B = std::min(b * SCREEN_COLOR_STEP, 255);
sample.setSourceRGB(byte3(R, G, B));
auto ref = &testData[r][g][b];
sample.addColor(ColorRgb((*ref)[0], (*ref)[1], (*ref)[2]));
if (oldFormat)
{
sample.addColor(ColorRgb((*ref)[0], (*ref)[1], (*ref)[2]));
}
else
{
sample.addColor(double3((*ref)[0] * 255.0 / IMPORT_SCALE,
(*ref)[1] * 255.0 / IMPORT_SCALE,
(*ref)[2] * 255.0 / IMPORT_SCALE));
}

sample.calculateFinalColor();
}
if (_capturedColors->all[0][0][0].Y() > SCREEN_YUV_RANGE_LIMIT || _capturedColors->all[0][0][0].Y() < 255 - SCREEN_YUV_RANGE_LIMIT)
Expand Down

0 comments on commit 7832376

Please sign in to comment.