Skip to content

Commit

Permalink
Pass image_size/anchor_size to extractor...
Browse files Browse the repository at this point in the history
Caveat: we're currently exposing image_size/anchor_size in the config
header. This is pretty wacky, and possibly a bad idea. But it also might
get the job done for now...?

(we'll see)
  • Loading branch information
sz3 committed Jun 1, 2023
1 parent 7766cfd commit 3616c36
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/exe/cimbar_extract/cimbar_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using std::string;

int main(int argc, char** argv)
{
cxxopts::Options options("cimbar scan", "Scan/extract cimbar code from a source image.");
cxxopts::Options options("cimbar_extract", "Scan/extract cimbar code from a source image.");

options.add_options()
("i,in", "Encoded png/jpg/etc", cxxopts::value<std::string>())
Expand Down
10 changes: 0 additions & 10 deletions src/lib/cimb_translator/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ unsigned Config::ecc_block_size()
return 216;
}

int Config::image_size()
{
return 988;
}

unsigned Config::anchor_size()
{
return 30;
}

unsigned Config::cell_size()
{
return 5;
Expand Down
11 changes: 9 additions & 2 deletions src/lib/cimb_translator/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ namespace cimbar
unsigned ecc_bytes();
unsigned ecc_block_size();

int image_size();
unsigned anchor_size();
constexpr int image_size()
{
return 988;
}

constexpr unsigned anchor_size()
{
return 30;
}

unsigned cell_size();
unsigned cell_spacing();
Expand Down
6 changes: 3 additions & 3 deletions src/lib/encoder/test/reed_solomon_streamTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace {
return ex;
}

string exampleEncodedBlock()
string exampleEncodedBlock155()
{
string ex = exampleDecodedBlock() + "\xa4t\x02\x03r\xc3\xad\xf2`\xc5\xb6\x9e&xs";
return ex;
Expand All @@ -36,15 +36,15 @@ TEST_CASE( "reed_solomon_streamTest/testEncodeOnce", "[unit]" )
reed_solomon_stream<stringstream> rss(ins, 15, 155);

assertEquals( 155, rss.readsome() );
assertEquals( exampleEncodedBlock(), string(rss.buffer(), 155) );
assertEquals( exampleEncodedBlock155(), string(rss.buffer(), 155) );
}

TEST_CASE( "reed_solomon_streamTest/testDecodeOnce", "[unit]" )
{
stringstream outs;
reed_solomon_stream<stringstream> rss(outs, 15, 155);

string encoded = exampleEncodedBlock();
string encoded = exampleEncodedBlock155();
rss.write(encoded.data(), encoded.size());

string actual = outs.str();
Expand Down
11 changes: 6 additions & 5 deletions src/lib/extractor/Deskewer.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/* This code is subject to the terms of the Mozilla Public License, v.2.0. http://mozilla.org/MPL/2.0/. */
#include "Deskewer.h"
#include "cimb_translator/Config.h"

#include <iostream>

Deskewer::Deskewer(unsigned total_size, unsigned anchor_size)
: _totalSize(total_size)
, _anchorSize(anchor_size)
Deskewer::Deskewer(unsigned image_size, unsigned anchor_size)
: _imageSize(image_size? image_size : cimbar::Config::image_size())
, _anchorSize(anchor_size? anchor_size : cimbar::Config::anchor_size())
{
}

int Deskewer::total_size() const
unsigned Deskewer::image_size() const
{
return _totalSize;
return _imageSize;
}

cv::Mat Deskewer::deskew(std::string img, const Corners& corners)
Expand Down
14 changes: 7 additions & 7 deletions src/lib/extractor/Deskewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
class Deskewer
{
public:
Deskewer(unsigned total_size=1024, unsigned anchor_size=30);
int total_size() const;
Deskewer(unsigned image_size=0, unsigned anchor_size=0);
unsigned image_size() const;

template <typename MAT>
MAT deskew(const MAT& img, const Corners& corners);
Expand All @@ -21,7 +21,7 @@ class Deskewer
bool save(const cv::Mat& img, std::string path);

protected:
int _totalSize;
int _imageSize;
int _anchorSize;
};

Expand All @@ -30,11 +30,11 @@ inline MAT Deskewer::deskew(const MAT& img, const Corners& corners)
{
std::vector<cv::Point2f> outputPoints;
outputPoints.push_back(cv::Point2f(_anchorSize, _anchorSize));
outputPoints.push_back(cv::Point2f(_totalSize - _anchorSize, _anchorSize));
outputPoints.push_back(cv::Point2f(_anchorSize, _totalSize - _anchorSize));
outputPoints.push_back(cv::Point2f(_totalSize - _anchorSize, _totalSize - _anchorSize));
outputPoints.push_back(cv::Point2f(_imageSize - _anchorSize, _anchorSize));
outputPoints.push_back(cv::Point2f(_anchorSize, _imageSize - _anchorSize));
outputPoints.push_back(cv::Point2f(_imageSize - _anchorSize, _imageSize - _anchorSize));

MAT output(_totalSize, _totalSize, img.type());
MAT output(_imageSize, _imageSize, img.type());
cv::Mat transform = cv::getPerspectiveTransform(corners.all(), outputPoints);

cv::warpPerspective(img, output, transform, output.size(), cv::INTER_LINEAR);
Expand Down
13 changes: 8 additions & 5 deletions src/lib/extractor/Extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

#include "Deskewer.h"
#include "Scanner.h"
#include "cimb_translator/Config.h"
#include <vector>
using std::string;

Extractor::Extractor()
Extractor::Extractor(unsigned image_size, unsigned anchor_size)
: _imageSize(image_size? image_size : cimbar::Config::image_size())
, _anchorSize(anchor_size? anchor_size : cimbar::Config::anchor_size())
{
}

Expand All @@ -18,10 +21,10 @@ int Extractor::extract(const cv::Mat& img, cv::Mat& out)
return FAILURE;

Corners corners(points);
Deskewer de;
Deskewer de(_imageSize, _anchorSize);
out = de.deskew(img, corners);

if ( !corners.is_granular_scale(de.total_size()) )
if ( !corners.is_granular_scale(de.image_size()) )
return NEEDS_SHARPEN;
return SUCCESS;
}
Expand All @@ -34,10 +37,10 @@ int Extractor::extract(const cv::UMat& img, cv::UMat& out)
return FAILURE;

Corners corners(points);
Deskewer de;
Deskewer de(_imageSize, _anchorSize);
out = de.deskew(img, corners);

if ( !corners.is_granular_scale(de.total_size()) )
if ( !corners.is_granular_scale(de.image_size()) )
return NEEDS_SHARPEN;
return SUCCESS;
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib/extractor/Extractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ class Extractor
static constexpr int NEEDS_SHARPEN = 2;

public:
Extractor();
Extractor(unsigned image_size=0, unsigned anchor_size=0);

int extract(const cv::Mat& img, cv::Mat& out);
int extract(const cv::UMat& img, cv::UMat& out);
int extract(std::string read_path, cv::Mat& out);
int extract(std::string read_path, std::string write_path);

protected:
unsigned _imageSize;
unsigned _anchorSize;
};
2 changes: 1 addition & 1 deletion src/lib/extractor/test/DeskewerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
TEST_CASE( "DeskewerTest/testSimple", "[unit]" )
{
Corners corners({312, 519}, {323, 2586}, {2405, 461}, {2425, 2594});
Deskewer de;
Deskewer de(1024, 30);

cv::Mat actual = de.deskew(TestCimbar::getSample("6bit/4_30_f0_big.jpg"), corners);
assertEquals(cv::Size(1024, 1024), actual.size());
Expand Down
6 changes: 3 additions & 3 deletions src/lib/extractor/test/ExtractorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TEST_CASE( "ExtractorTest/testExtract", "[unit]" )
MakeTempDirectory tempdir;

std::string imgPath = tempdir.path() / "ex.jpg";
Extractor ext;
Extractor ext(1024, 30);
ext.extract(TestCimbar::getSample("6bit/4_30_f0_big.jpg"), imgPath);

cv::Mat out = cv::imread(imgPath);
Expand All @@ -27,7 +27,7 @@ TEST_CASE( "ExtractorTest/testExtractMid", "[unit]" )
MakeTempDirectory tempdir;

std::string imgPath = tempdir.path() / "ex.jpg";
Extractor ext;
Extractor ext(1024, 30);
ext.extract(TestCimbar::getSample("6bit/4_30_f2_734.jpg"), imgPath);

cv::Mat out = cv::imread(imgPath);
Expand All @@ -40,7 +40,7 @@ TEST_CASE( "ExtractorTest/testExtractUpscale", "[unit]" )
MakeTempDirectory tempdir;

std::string imgPath = tempdir.path() / "exup.jpg";
Extractor ext;
Extractor ext(1024, 30);
ext.extract(TestCimbar::getSample("6bit/4_30_f0_627.jpg"), imgPath);

cv::Mat out = cv::imread(imgPath);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/extractor/test/UndistortTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ TEST_CASE( "UndistortTest/testUndistortAndExtract", "[unit]" )
Undistort<SimpleCameraCalibration> und;
assertTrue( und.undistort(img, out) );

Extractor ex;
Extractor ex(1024, 30);
assertTrue( ex.extract(out, out) );

assertEquals( 0x18f26faca7766794, image_hash::average_hash(out) );
Expand Down

0 comments on commit 3616c36

Please sign in to comment.