Skip to content

Commit

Permalink
Use 4 middle cols when decoding 8x8 colors
Browse files Browse the repository at this point in the history
Seems more consistent, without *too* much of a performance hit than the
every-other 3 col optimization.
  • Loading branch information
sz3 committed Nov 27, 2023
1 parent 5b82141 commit fe5415c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/lib/cimb_translator/CimbDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,9 @@ unsigned CimbDecoder::get_best_color(float r, float g, float b) const

float max = std::max({r, g, b, 1.0f});
float min = std::min({r, g, b, BEST_COLOR_FLOOR});
float adjust = 255.0;
if (min >= max)
min = 0;
adjust /= (max - min);
float adjust = 255.0/(max - min);

std::tuple<uchar,uchar,uchar> c = fix_color({r, g, b}, adjust, min);

Expand All @@ -188,8 +187,9 @@ unsigned CimbDecoder::decode_color(const Cell& color_cell) const
// TODO: check/enforce dimensions of color_cell?
// limit dimensions to ignore outer row/col. We want to look at the middle 6x6, or 3x3...
Cell center = color_cell;
center.crop(1, 1, color_cell.cols()-2, color_cell.rows()-2);
auto [r, g, b] = center.mean_rgb(center.cols() > 4? Cell::SKIP : 0);
int hcrop = color_cell.cols() > 6? 2 : 1;
center.crop(hcrop, 1, color_cell.cols()-1-hcrop, color_cell.rows()-2);
auto [r, g, b] = center.mean_rgb(center.cols() > 5? Cell::SKIP : 0);
return get_best_color(r, g, b);
}

Expand Down
13 changes: 7 additions & 6 deletions src/lib/cimb_translator/test/CimbReaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ TEST_CASE( "CimbReaderTest/testSample", "[unit]" )
++count;
}

string expected = "0=0 99=8 600=33 710=28 711=30 821=8 822=22 823=19 934=55 11688=55 11799=25 "
"11900=28 12000=23 12100=0 12200=5 12201=0 12298=32 12299=34 12300=30 12301=32 "
"12398=10 12399=15";
string expected = "0=0 99=8 11680=3 11681=32 11900=28 11901=25 11904=12 11995=2 11996=8 11998=6 "
"11999=54 12001=29 12004=6 12099=2 12195=57 12196=1 12200=5 12201=0 12298=32 "
"12299=34 12300=30 12399=15";
assertEquals( expected, turbo::str::join(res) );

PositionData pos;
Expand Down Expand Up @@ -109,8 +109,9 @@ TEST_CASE( "CimbReaderTest/testSampleMessy", "[unit]" )
++count;
}

string expected = "0=0 99=8 600=33 711=30 11462=2 11573=33 11574=0 11685=52 11686=17 11687=41 "
"11688=55 11797=30 11798=15 11799=25 12200=5 12298=46 12299=34 12300=30 12301=32 12397=38 12398=10 12399=15";
string expected = "0=0 1=28 99=8 100=28 600=33 601=38 711=30 712=57 11238=24 11350=32 11571=0 "
"11573=33 11574=0 11575=1 11685=52 11686=17 11687=41 11688=55 11798=15 11799=25 "
"12300=30 12399=15";
assertEquals( expected, turbo::str::join(res) );

PositionData pos;
Expand Down Expand Up @@ -200,7 +201,7 @@ TEST_CASE( "CimbReaderTest/testCCM.VeryNecessary", "[unit]" )
" 0.023812667, 0.98703396, -0.0048082001;\n"
" 0, 0, 1.0017186]", ss.str());

std::array<unsigned, 6> expectedColors = {0, 0, 0, 1, 2, 3}; // it's wrong, but it's consistent!
std::array<unsigned, 6> expectedColors = {0, 0, 0, 0, 1, 0}; // it's wrong, but it's consistent!
for (unsigned i = 0; i < expectedColors.size(); ++i)
{
PositionData pos;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/encoder/test/DecoderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ TEST_CASE( "DecoderTest/testDecode.Sample", "[unit]" )
assertEquals( 9300, bytesDecoded );

if (CV_VERSION_MAJOR == 4)
assertEquals( "69b608d82083b5d22cd3ebf8785c2d0329a83b0cd084521c418ea97316b54cc6", get_hash(decodedFile) );
assertEquals( "847dc20288c717b6f43b67b15fd2001d950ea5bcfe82de5c5d17bdc047f8631f", get_hash(decodedFile) );
}

0 comments on commit fe5415c

Please sign in to comment.