Skip to content

Commit

Permalink
Merge pull request opencv#24299 from dkurt:qrcode_decode
Browse files Browse the repository at this point in the history
In-house QR codes decoding opencv#24299

### Pull Request Readiness Checklist

QR codes decoding pipeline without 3rdparty dependency (Quirc library). Implemented according to standard https://github.com/yansikeim/QR-Code/blob/master/ISO%20IEC%2018004%202015%20Standard.pdf

**Merge with extra**: opencv/opencv_extra#1124

resolves opencv#24225
resolves opencv#17290
resolves opencv#24318 opencv#24346

Resources:
* https://en.wikiversity.org/wiki/Reed%E2%80%93Solomon_codes_for_coders
* https://en.wikipedia.org/wiki/Berlekamp%E2%80%93Massey_algorithm

```
Geometric mean (ms)

                                   Name of Test                                      quirc   new2      new2   
                                                                                                        vs    
                                                                                                      quirc   
                                                                                                    (x-factor)
decode::Perf_Objdetect_Not_QRCode::("chessboard", 640x480)                           9.151   9.157     1.00   
decode::Perf_Objdetect_Not_QRCode::("chessboard", 1280x720)                         21.609  21.609     1.00   
decode::Perf_Objdetect_Not_QRCode::("chessboard", 1920x1080)                        42.088  41.924     1.00   
decode::Perf_Objdetect_Not_QRCode::("chessboard", 3840x2160)                        169.737 169.050    1.00   
decode::Perf_Objdetect_Not_QRCode::("random", 640x480)                               8.552   8.611     0.99   
decode::Perf_Objdetect_Not_QRCode::("random", 1280x720)                             21.264  21.581     0.99   
decode::Perf_Objdetect_Not_QRCode::("random", 1920x1080)                            42.415  43.468     0.98   
decode::Perf_Objdetect_Not_QRCode::("random", 3840x2160)                            175.003 174.294    1.00   
decode::Perf_Objdetect_Not_QRCode::("zero", 640x480)                                 8.528   8.421     1.01   
decode::Perf_Objdetect_Not_QRCode::("zero", 1280x720)                               21.548  21.209     1.02   
decode::Perf_Objdetect_Not_QRCode::("zero", 1920x1080)                              42.581  42.529     1.00   
decode::Perf_Objdetect_Not_QRCode::("zero", 3840x2160)                              176.231 174.410    1.01   
decode::Perf_Objdetect_QRCode::"kanji.jpg"                                           6.105   6.072     1.01   
decode::Perf_Objdetect_QRCode::"link_github_ocv.jpg"                                 6.069   6.076     1.00   
decode::Perf_Objdetect_QRCode::"link_ocv.jpg"                                        6.143   6.240     0.98   
decode::Perf_Objdetect_QRCode::"link_wiki_cv.jpg"                                    6.369   6.420     0.99   
decode::Perf_Objdetect_QRCode::"russian.jpg"                                         6.558   6.549     1.00   
decode::Perf_Objdetect_QRCode::"version_1_down.jpg"                                  5.634   5.621     1.00   
decode::Perf_Objdetect_QRCode::"version_1_left.jpg"                                  5.560   5.609     0.99   
decode::Perf_Objdetect_QRCode::"version_1_right.jpg"                                 5.539   5.631     0.98   
decode::Perf_Objdetect_QRCode::"version_1_top.jpg"                                   5.622   5.566     1.01   
decode::Perf_Objdetect_QRCode::"version_1_up.jpg"                                    5.569   5.534     1.01   
decode::Perf_Objdetect_QRCode::"version_5_down.jpg"                                  6.514   6.436     1.01   
decode::Perf_Objdetect_QRCode::"version_5_left.jpg"                                  6.668   6.479     1.03   
decode::Perf_Objdetect_QRCode::"version_5_top.jpg"                                   6.481   6.484     1.00   
decode::Perf_Objdetect_QRCode::"version_5_up.jpg"                                    7.011   6.513     1.08   
decodeMulti::Perf_Objdetect_QRCode_Multi::("2_qrcodes.png", "aruco_based")          14.885  15.089     0.99   
decodeMulti::Perf_Objdetect_QRCode_Multi::("2_qrcodes.png", "contours_based")       14.896  14.906     1.00   
decodeMulti::Perf_Objdetect_QRCode_Multi::("3_close_qrcodes.png", "aruco_based")     6.661   6.663     1.00   
decodeMulti::Perf_Objdetect_QRCode_Multi::("3_close_qrcodes.png", "contours_based")  6.614   6.592     1.00   
decodeMulti::Perf_Objdetect_QRCode_Multi::("3_qrcodes.png", "aruco_based")          14.814  14.592     1.02   
decodeMulti::Perf_Objdetect_QRCode_Multi::("3_qrcodes.png", "contours_based")       15.245  15.135     1.01   
decodeMulti::Perf_Objdetect_QRCode_Multi::("4_qrcodes.png", "aruco_based")          10.923  10.881     1.00   
decodeMulti::Perf_Objdetect_QRCode_Multi::("4_qrcodes.png", "contours_based")       10.680  10.128     1.05   
decodeMulti::Perf_Objdetect_QRCode_Multi::("5_qrcodes.png", "contours_based")       11.788  11.576     1.02   
decodeMulti::Perf_Objdetect_QRCode_Multi::("6_qrcodes.png", "aruco_based")          25.887  25.979     1.00   
decodeMulti::Perf_Objdetect_QRCode_Multi::("6_qrcodes.png", "contours_based")       26.183  25.627     1.02   
decodeMulti::Perf_Objdetect_QRCode_Multi::("7_qrcodes.png", "aruco_based")          32.786  32.253     1.02   
decodeMulti::Perf_Objdetect_QRCode_Multi::("7_qrcodes.png", "contours_based")       24.290  24.435     0.99   
decodeMulti::Perf_Objdetect_QRCode_Multi::("8_close_qrcodes.png", "aruco_based")    89.696  89.247     1.01   
decodeMulti::Perf_Objdetect_QRCode_Multi::("8_close_qrcodes.png", "contours_based") 89.872  89.600     1.00
```
  • Loading branch information
dkurt authored and thewoz committed May 29, 2024
1 parent 100d366 commit 10aa5ad
Show file tree
Hide file tree
Showing 10 changed files with 656 additions and 151 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ OCV_OPTION(WITH_IMGCODEC_PXM "Include PNM (PBM,PGM,PPM) and PAM formats support"
OCV_OPTION(WITH_IMGCODEC_PFM "Include PFM formats support" ON
VISIBLE_IF TRUE
VERIFY HAVE_IMGCODEC_PFM)
OCV_OPTION(WITH_QUIRC "Include library QR-code decoding" ON
OCV_OPTION(WITH_QUIRC "Include library QR-code decoding" OFF
VISIBLE_IF TRUE
VERIFY HAVE_QUIRC)
OCV_OPTION(WITH_ANDROID_MEDIANDK "Use Android Media NDK for Video I/O (Android)" (ANDROID_NATIVE_API_LEVEL GREATER 20)
Expand Down
6 changes: 0 additions & 6 deletions modules/objdetect/perf/perf_qrcode_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ PERF_TEST_P_(Perf_Objdetect_QRCode, detect)
SANITY_CHECK_NOTHING();
}

#ifdef HAVE_QUIRC
PERF_TEST_P_(Perf_Objdetect_QRCode, decode)
{
const std::string name_current_image = GetParam();
Expand All @@ -52,7 +51,6 @@ PERF_TEST_P_(Perf_Objdetect_QRCode, decode)
check_qr(root, name_current_image, "test_images", corners, {decoded_info}, pixels_error);
SANITY_CHECK_NOTHING();
}
#endif

typedef ::perf::TestBaseWithParam<std::tuple<std::string, std::string>> Perf_Objdetect_QRCode_Multi;

Expand All @@ -78,7 +76,6 @@ PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, detectMulti)
SANITY_CHECK_NOTHING();
}

#ifdef HAVE_QUIRC
PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, decodeMulti)
{
const std::string name_current_image = get<0>(GetParam());
Expand Down Expand Up @@ -116,7 +113,6 @@ PERF_TEST_P_(Perf_Objdetect_QRCode_Multi, decodeMulti)
check_qr(root, name_current_image, "multiple_images", corners_result, decoded_info, pixels_error, true);
SANITY_CHECK_NOTHING();
}
#endif

INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_QRCode,
::testing::Values(
Expand Down Expand Up @@ -163,7 +159,6 @@ PERF_TEST_P_(Perf_Objdetect_Not_QRCode, detect)
SANITY_CHECK_NOTHING();
}

#ifdef HAVE_QUIRC
PERF_TEST_P_(Perf_Objdetect_Not_QRCode, decode)
{
Mat straight_barcode;
Expand Down Expand Up @@ -195,7 +190,6 @@ PERF_TEST_P_(Perf_Objdetect_Not_QRCode, decode)
TEST_CYCLE() ASSERT_TRUE(qrcode.decode(not_qr_code, corners, straight_barcode).empty());
SANITY_CHECK_NOTHING();
}
#endif

INSTANTIATE_TEST_CASE_P(/*nothing*/, Perf_Objdetect_Not_QRCode,
::testing::Combine(
Expand Down
14 changes: 13 additions & 1 deletion modules/objdetect/src/graphical_code_detector_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ struct GraphicalCodeDetector::Impl {
OutputArray points, OutputArrayOfArrays straight_code) const = 0;
};

class QRCodeDecoder {
public:
virtual ~QRCodeDecoder();

static Ptr<QRCodeDecoder> create();

virtual bool decode(const Mat& straight, String& decoded_info) = 0;

QRCodeEncoder::EncodeMode mode;
QRCodeEncoder::ECIEncodings eci;
};

}

#endif
#endif
82 changes: 45 additions & 37 deletions modules/objdetect/src/qrcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2727,7 +2727,6 @@ bool QRDecode::samplingForVersion()
return true;
}


static bool checkASCIIcompatible(const uint8_t* str, const size_t size) {
for (size_t i = 0; i < size; ++i) {
uint8_t byte = str[i];
Expand Down Expand Up @@ -2781,6 +2780,10 @@ static std::string encodeUTF8_bytesarray(const uint8_t* str, const size_t size)

bool QRDecode::decodingProcess()
{
QRCodeEncoder::EncodeMode mode;
QRCodeEncoder::ECIEncodings eci;
const uint8_t* payload;
size_t payload_len;
#ifdef HAVE_QUIRC
if (straight.empty()) { return false; }

Expand Down Expand Up @@ -2810,90 +2813,95 @@ bool QRDecode::decodingProcess()

CV_LOG_INFO(NULL, "QR: decoded with .version=" << qr_code_data.version << " .data_type=" << qr_code_data.data_type << " .eci=" << qr_code_data.eci << " .payload_len=" << qr_code_data.payload_len)

switch (qr_code_data.data_type)
mode = static_cast<QRCodeEncoder::EncodeMode>(qr_code_data.data_type);
eci = static_cast<QRCodeEncoder::ECIEncodings>(qr_code_data.eci);
payload = qr_code_data.payload;
payload_len = qr_code_data.payload_len;
#else
auto decoder = QRCodeDecoder::create();
if (!decoder->decode(straight, result_info))
return false;
mode = decoder->mode;
eci = decoder->eci;
payload = reinterpret_cast<const uint8_t*>(result_info.c_str());
payload_len = result_info.size();
#endif

// Check output string format
switch (mode)
{
case QUIRC_DATA_TYPE_NUMERIC:
if (!checkASCIIcompatible(qr_code_data.payload, qr_code_data.payload_len)) {
case QRCodeEncoder::EncodeMode::MODE_NUMERIC:
if (!checkASCIIcompatible(payload, payload_len)) {
CV_LOG_INFO(NULL, "QR: DATA_TYPE_NUMERIC payload must be ACSII compatible string");
return false;
}
result_info.assign((const char*)qr_code_data.payload, qr_code_data.payload_len);
result_info.assign((const char*)payload, payload_len);
return true;
case QUIRC_DATA_TYPE_ALPHA:
if (!checkASCIIcompatible(qr_code_data.payload, qr_code_data.payload_len)) {
case QRCodeEncoder::EncodeMode::MODE_ALPHANUMERIC:
if (!checkASCIIcompatible(payload, payload_len)) {
CV_LOG_INFO(NULL, "QR: DATA_TYPE_ALPHA payload must be ASCII compatible string");
return false;
}
result_info.assign((const char*)qr_code_data.payload, qr_code_data.payload_len);
result_info.assign((const char*)payload, payload_len);
return true;
case QUIRC_DATA_TYPE_BYTE:
case QRCodeEncoder::EncodeMode::MODE_BYTE:
// https://en.wikipedia.org/wiki/Extended_Channel_Interpretation
if (qr_code_data.eci == QUIRC_ECI_UTF_8) {
if (eci == QRCodeEncoder::ECIEncodings::ECI_UTF8) {
CV_LOG_INFO(NULL, "QR: payload ECI is UTF-8");
if (!checkUTF8(qr_code_data.payload, qr_code_data.payload_len)) {
if (!checkUTF8(payload, payload_len)) {
CV_LOG_INFO(NULL, "QUIRC_DATA_TYPE_BYTE with UTF-8 ECI must be UTF-8 compatible string");
return false;
}
result_info.assign((const char*)qr_code_data.payload, qr_code_data.payload_len);
} else if (qr_code_data.eci == 25/*ECI_UTF_16BE*/) {
result_info.assign((const char*)payload, payload_len);
} else if (eci == 25/*ECI_UTF_16BE*/) {
CV_LOG_INFO(NULL, "QR: UTF-16BE ECI is not supported");
return false;
} else if (checkASCIIcompatible(qr_code_data.payload, qr_code_data.payload_len)) {
} else if (checkASCIIcompatible(payload, payload_len)) {
CV_LOG_INFO(NULL, "QR: payload is ASCII compatible (special handling for symbols encoding is not needed)");
result_info.assign((const char*)qr_code_data.payload, qr_code_data.payload_len);
result_info.assign((const char*)payload, payload_len);
} else {
if (checkUTF8(qr_code_data.payload, qr_code_data.payload_len)) {
if (checkUTF8(payload, payload_len)) {
CV_LOG_INFO(NULL, "QR: payload QUIRC_DATA_TYPE_BYTE is UTF-8 compatible, return as-is");
result_info.assign((const char*)qr_code_data.payload, qr_code_data.payload_len);
result_info.assign((const char*)payload, payload_len);
} else {
CV_LOG_INFO(NULL, "QR: assume 1-byte per symbol encoding");
result_info = encodeUTF8_bytesarray(qr_code_data.payload, qr_code_data.payload_len);
result_info = encodeUTF8_bytesarray(payload, payload_len);
}
}
return true;
case QUIRC_DATA_TYPE_KANJI:
case QRCodeEncoder::EncodeMode::MODE_KANJI:
// FIXIT BUG: we must return UTF-8 compatible string
CV_LOG_WARNING(NULL, "QR: Kanji is not supported properly");
result_info.assign((const char*)qr_code_data.payload, qr_code_data.payload_len);
result_info.assign((const char*)payload, payload_len);
return true;
case QRCodeEncoder::EncodeMode::MODE_ECI:
CV_LOG_WARNING(NULL, "QR: ECI is not supported properly");
result_info.assign((const char*)payload, payload_len);
return true;
default:
CV_LOG_WARNING(NULL, "QR: unsupported QR data type");
return false;
}

CV_LOG_WARNING(NULL, "QR: unsupported QR data type");
return false;
#else
return false;
#endif

}

bool QRDecode::straightDecodingProcess()
{
#ifdef HAVE_QUIRC
if (!updatePerspective(getHomography())) { return false; }
if (!versionDefinition()) { return false; }
if (useAlignmentMarkers)
detectAlignment();
if (!samplingForVersion()) { return false; }
if (!decodingProcess()) { return false; }
return true;
#else
std::cout << "Library QUIRC is not linked. No decoding is performed. Take it to the OpenCV repository." << std::endl;
return false;
#endif
}

bool QRDecode::curvedDecodingProcess()
{
#ifdef HAVE_QUIRC
if (!preparingCurvedQRCodes()) { return false; }
if (!versionDefinition()) { return false; }
if (!samplingForVersion()) { return false; }
if (!decodingProcess()) { return false; }
return true;
#else
std::cout << "Library QUIRC is not linked. No decoding is performed. Take it to the OpenCV repository." << std::endl;
return false;
#endif
}

QRDecode::QRDecode(bool _useAlignmentMarkers):
Expand Down
Loading

0 comments on commit 10aa5ad

Please sign in to comment.