Skip to content

Commit

Permalink
Fix long-standing bug in Cell::mean_rgb_continuous()
Browse files Browse the repository at this point in the history
-- relevant now that things aren't square.
  • Loading branch information
sz3 committed Jul 24, 2024
1 parent 9febbad commit 651676e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/lib/cimb_translator/Cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ class Cell
const uchar* p = _img.ptr<uchar>(0) + (index * channels);

int increment = 1 + skip;
int toNextCol = channels * (_img.rows - _rows);
int toNextRow = channels * (_img.cols - _cols);
if (skip)
toNextCol += channels * _img.rows;
toNextRow += channels * _img.cols;

for (int i = 0; i < _cols; i+=increment)
for (int i = 0; i < _rows; i+=increment)
{
for (int j = 0; j < _rows; ++j, ++count)
for (int j = 0; j < _cols; ++j, ++count)
{
red += p[0];
green += p[1];
blue += p[2];
p += channels;
}
p += toNextCol;
p += toNextRow;
}

if (!count)
Expand Down
39 changes: 35 additions & 4 deletions src/lib/cimb_translator/test/CellTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,52 @@ TEST_CASE( "CellTest/testRgbCellOffsets.Asymmetric.Contiguous", "[unit]" )
{
cv::Mat img = TestCimbar::loadSample("6bit/4color_ecc30_fountain_0.png");

cv::Rect crop(125, 8, 4, 6);
cv::Rect crop(126, 9, 6, 6);
cv::Mat cell = img(crop);
cv::Scalar expectedColor = cv::mean(cell);

auto [r, g, b] = Cell(img, 125, 8, 4, 6).mean_rgb();
auto [r, g, b] = Cell(img, 126, 9, 6, 6).mean_rgb();

DYNAMIC_SECTION( "r" )
{
assertEquals( 191, (int)r );
assertAlmostEquals( expectedColor[0], (int)r );
assertEquals( 198, (int)r );
}
DYNAMIC_SECTION( "g" )
{
assertEquals( 191, (int)g );
assertAlmostEquals( expectedColor[1], (int)g );
assertEquals( 198, (int)g );
}
DYNAMIC_SECTION( "b" )
{
assertAlmostEquals( expectedColor[2], (int)b );
assertEquals( 0, (int)b );
}
}

TEST_CASE( "CellTest/testRgbCellOffsets.WideCanvas", "[unit]" )
{
cv::Mat img = TestCimbar::loadSample("bm/ecc35.png");

cv::Rect crop(127, 10, 6, 6);
cv::Mat cell = img(crop);
cv::Scalar expectedColor = cv::mean(cell);

auto [r, g, b] = Cell(img, 127, 10, 6, 6).mean_rgb();

DYNAMIC_SECTION( "r" )
{
assertAlmostEquals( expectedColor[0], (int)r );
assertEquals( 148, (int)r );
}
DYNAMIC_SECTION( "g" )
{
assertAlmostEquals( expectedColor[1], (int)g );
assertEquals( 0, (int)g );
}
DYNAMIC_SECTION( "b" )
{
assertAlmostEquals( expectedColor[2], (int)b );
assertEquals( 148, (int)b );
}
}

0 comments on commit 651676e

Please sign in to comment.