1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

Merge branch 4.x

This commit is contained in:
Alexander Smorkalov
2024-01-15 17:23:10 +03:00
531 changed files with 20900 additions and 12934 deletions
@@ -33,7 +33,7 @@ struct CV_EXPORTS_W_SIMPLE DetectorParameters {
polygonalApproxAccuracyRate = 0.03;
minCornerDistanceRate = 0.05;
minDistanceToBorder = 3;
minMarkerDistanceRate = 0.05;
minMarkerDistanceRate = 0.125;
cornerRefinementMethod = (int)CORNER_REFINE_NONE;
cornerRefinementWinSize = 5;
relativeCornerRefinmentWinSize = 0.3f;
@@ -100,12 +100,26 @@ struct CV_EXPORTS_W_SIMPLE DetectorParameters {
/// minimum distance of any corner to the image border for detected markers (in pixels) (default 3)
CV_PROP_RW int minDistanceToBorder;
/** @brief minimum mean distance beetween two marker corners to be considered imilar, so that the smaller one is removed.
/** @brief minimum average distance between the corners of the two markers to be grouped (default 0.125).
*
* The rate is relative to the smaller perimeter of the two markers (default 0.05).
* The rate is relative to the smaller perimeter of the two markers.
* Two markers are grouped if average distance between the corners of the two markers is less than
* min(MarkerPerimeter1, MarkerPerimeter2)*minMarkerDistanceRate.
*
* default value is 0.125 because 0.125*MarkerPerimeter = (MarkerPerimeter / 4) * 0.5 = half the side of the marker.
*
* @note default value was changed from 0.05 after 4.8.1 release, because the filtering algorithm has been changed.
* Now a few candidates from the same group can be added to the list of candidates if they are far from each other.
* @sa minGroupDistance.
*/
CV_PROP_RW double minMarkerDistanceRate;
/** @brief minimum average distance between the corners of the two markers in group to add them to the list of candidates
*
* The average distance between the corners of the two markers is calculated relative to its module size (default 0.21).
*/
CV_PROP_RW float minGroupDistance = 0.21f;
/** @brief default value CORNER_REFINE_NONE */
CV_PROP_RW int cornerRefinementMethod;
@@ -71,7 +71,7 @@ public:
*/
CV_WRAP virtual int detect(InputArray image, OutputArray faces) = 0;
/** @brief Creates an instance of this class with given parameters
/** @brief Creates an instance of face detector class with given parameters
*
* @param model the path to the requested model
* @param config the path to the config file for compability, which is not requested for ONNX models
@@ -90,6 +90,29 @@ public:
int top_k = 5000,
int backend_id = 0,
int target_id = 0);
/** @overload
*
* @param framework Name of origin framework
* @param bufferModel A buffer with a content of binary file with weights
* @param bufferConfig A buffer with a content of text file contains network configuration
* @param input_size the size of the input image
* @param score_threshold the threshold to filter out bounding boxes of score smaller than the given value
* @param nms_threshold the threshold to suppress bounding boxes of IoU bigger than the given value
* @param top_k keep top K bboxes before NMS
* @param backend_id the id of backend
* @param target_id the id of target device
*/
CV_WRAP static Ptr<FaceDetectorYN> create(const String& framework,
const std::vector<uchar>& bufferModel,
const std::vector<uchar>& bufferConfig,
const Size& input_size,
float score_threshold = 0.9f,
float nms_threshold = 0.3f,
int top_k = 5000,
int backend_id = 0,
int target_id = 0);
};
/** @brief DNN-based face recognizer
@@ -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();
@@ -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;
@@ -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());
@@ -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(
@@ -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;
@@ -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(
+262 -259
View File
@@ -50,6 +50,7 @@ static inline bool readWrite(DetectorParameters &params, const FileNode* readNod
readNode, writeStorage);
check |= readWriteParameter("minOtsuStdDev", params.minOtsuStdDev, readNode, writeStorage);
check |= readWriteParameter("errorCorrectionRate", params.errorCorrectionRate, readNode, writeStorage);
check |= readWriteParameter("minGroupDistance", params.minGroupDistance, readNode, writeStorage);
// new aruco 3 functionality
check |= readWriteParameter("useAruco3Detection", params.useAruco3Detection, readNode, writeStorage);
check |= readWriteParameter("minSideLengthCanonicalImg", params.minSideLengthCanonicalImg, readNode, writeStorage);
@@ -212,149 +213,65 @@ static void _reorderCandidatesCorners(vector<vector<Point2f> > &candidates) {
}
}
/**
* @brief to make sure that the corner's order of both candidates (default/white) is the same
*/
static vector<Point2f> alignContourOrder(Point2f corner, vector<Point2f> candidate) {
uint8_t r=0;
double min = norm( Vec2f( corner - candidate[0] ), NORM_L2SQR);
for(uint8_t pos=1; pos < 4; pos++) {
double nDiff = norm( Vec2f( corner - candidate[pos] ), NORM_L2SQR);
if(nDiff < min){
r = pos;
min =nDiff;
}
static float getAverageModuleSize(const vector<Point2f>& markerCorners, int markerSize, int markerBorderBits) {
float averageArucoModuleSize = 0.f;
for (size_t i = 0ull; i < 4ull; i++) {
averageArucoModuleSize += sqrt(normL2Sqr<float>(Point2f(markerCorners[i] - markerCorners[(i+1ull) % 4ull])));
}
std::rotate(candidate.begin(), candidate.begin() + r, candidate.end());
return candidate;
int numModules = markerSize + markerBorderBits * 2;
averageArucoModuleSize /= ((float)markerCorners.size()*numModules);
return averageArucoModuleSize;
}
/**
* @brief Check candidates that are too close to each other, save the potential candidates
* (i.e. biggest/smallest contour) and remove the rest
*/
static void _filterTooCloseCandidates(const vector<vector<Point2f> > &candidatesIn,
vector<vector<vector<Point2f> > > &candidatesSetOut,
const vector<vector<Point> > &contoursIn,
vector<vector<vector<Point> > > &contoursSetOut,
double minMarkerDistanceRate, bool detectInvertedMarker) {
static bool checkMarker1InMarker2(const vector<Point2f>& marker1, const vector<Point2f>& marker2) {
return pointPolygonTest(marker2, marker1[0], false) >= 0 && pointPolygonTest(marker2, marker1[1], false) >= 0 &&
pointPolygonTest(marker2, marker1[2], false) >= 0 && pointPolygonTest(marker2, marker1[3], false) >= 0;
}
CV_Assert(minMarkerDistanceRate >= 0);
vector<int> candGroup;
candGroup.resize(candidatesIn.size(), -1);
vector<vector<unsigned int> > groupedCandidates;
for(unsigned int i = 0; i < candidatesIn.size(); i++) {
bool isSingleContour = true;
for(unsigned int j = i + 1; j < candidatesIn.size(); j++) {
struct MarkerCandidate {
vector<Point2f> corners;
vector<Point> contour;
float perimeter = 0.f;
};
int minimumPerimeter = min((int)contoursIn[i].size(), (int)contoursIn[j].size() );
struct MarkerCandidateTree : MarkerCandidate{
int parent = -1;
int depth = 0;
vector<MarkerCandidate> closeContours;
// fc is the first corner considered on one of the markers, 4 combinations are possible
for(int fc = 0; fc < 4; fc++) {
double distSq = 0;
for(int c = 0; c < 4; c++) {
// modC is the corner considering first corner is fc
int modC = (c + fc) % 4;
distSq += (candidatesIn[i][modC].x - candidatesIn[j][c].x) *
(candidatesIn[i][modC].x - candidatesIn[j][c].x) +
(candidatesIn[i][modC].y - candidatesIn[j][c].y) *
(candidatesIn[i][modC].y - candidatesIn[j][c].y);
}
distSq /= 4.;
MarkerCandidateTree() {}
// if mean square distance is too low, remove the smaller one of the two markers
double minMarkerDistancePixels = double(minimumPerimeter) * minMarkerDistanceRate;
if(distSq < minMarkerDistancePixels * minMarkerDistancePixels) {
isSingleContour = false;
// i and j are not related to a group
if(candGroup[i]<0 && candGroup[j]<0){
// mark candidates with their corresponding group number
candGroup[i] = candGroup[j] = (int)groupedCandidates.size();
// create group
vector<unsigned int> grouped;
grouped.push_back(i);
grouped.push_back(j);
groupedCandidates.push_back( grouped );
}
// i is related to a group
else if(candGroup[i] > -1 && candGroup[j] == -1){
int group = candGroup[i];
candGroup[j] = group;
// add to group
groupedCandidates[group].push_back( j );
}
// j is related to a group
else if(candGroup[j] > -1 && candGroup[i] == -1){
int group = candGroup[j];
candGroup[i] = group;
// add to group
groupedCandidates[group].push_back( i );
}
}
}
}
if (isSingleContour && candGroup[i] < 0)
{
candGroup[i] = (int)groupedCandidates.size();
vector<unsigned int> grouped;
grouped.push_back(i);
grouped.push_back(i); // step "save possible candidates" require minimum 2 elements
groupedCandidates.push_back(grouped);
MarkerCandidateTree(vector<Point2f>&& corners_, vector<Point>&& contour_) {
corners = std::move(corners_);
contour = std::move(contour_);
perimeter = 0.f;
for (size_t i = 0ull; i < 4ull; i++) {
perimeter += sqrt(normL2Sqr<float>(Point2f(corners[i] - corners[(i+1ull) % 4ull])));
}
}
// save possible candidates
candidatesSetOut.clear();
contoursSetOut.clear();
vector<vector<Point2f> > biggerCandidates;
vector<vector<Point> > biggerContours;
vector<vector<Point2f> > smallerCandidates;
vector<vector<Point> > smallerContours;
// save possible candidates
for(unsigned int i = 0; i < groupedCandidates.size(); i++) {
unsigned int smallerIdx = groupedCandidates[i][0];
unsigned int biggerIdx = smallerIdx;
double smallerArea = contourArea(candidatesIn[smallerIdx]);
double biggerArea = smallerArea;
// evaluate group elements
for(unsigned int j = 1; j < groupedCandidates[i].size(); j++) {
unsigned int currIdx = groupedCandidates[i][j];
double currArea = contourArea(candidatesIn[currIdx]);
// check if current contour is bigger
if(currArea >= biggerArea) {
biggerIdx = currIdx;
biggerArea = currArea;
}
// check if current contour is smaller
if(currArea < smallerArea && detectInvertedMarker) {
smallerIdx = currIdx;
smallerArea = currArea;
}
}
// add contours and candidates
biggerCandidates.push_back(candidatesIn[biggerIdx]);
biggerContours.push_back(contoursIn[biggerIdx]);
if(detectInvertedMarker) {
smallerCandidates.push_back(alignContourOrder(candidatesIn[biggerIdx][0], candidatesIn[smallerIdx]));
smallerContours.push_back(contoursIn[smallerIdx]);
}
bool operator<(const MarkerCandidateTree& m) const {
// sorting the contors in descending order
return perimeter > m.perimeter;
}
// to preserve the structure :: candidateSet< defaultCandidates, whiteCandidates >
// default candidates
candidatesSetOut.push_back(biggerCandidates);
contoursSetOut.push_back(biggerContours);
// white candidates
candidatesSetOut.push_back(smallerCandidates);
contoursSetOut.push_back(smallerContours);
};
// returns the average distance between the marker points
float static inline getAverageDistance(const std::vector<Point2f>& marker1, const std::vector<Point2f>& marker2) {
float minDistSq = std::numeric_limits<float>::max();
// fc is the first corner considered on one of the markers, 4 combinations are possible
for(int fc = 0; fc < 4; fc++) {
float distSq = 0;
for(int c = 0; c < 4; c++) {
// modC is the corner considering first corner is fc
int modC = (c + fc) % 4;
distSq += normL2Sqr<float>(marker1[modC] - marker2[c]);
}
distSq /= 4.f;
minDistSq = min(minDistSq, distSq);
}
return sqrt(minDistSq);
}
/**
@@ -403,29 +320,6 @@ static void _detectInitialCandidates(const Mat &grey, vector<vector<Point2f> > &
}
/**
* @brief Detect square candidates in the input image
*/
static void _detectCandidates(InputArray _grayImage, vector<vector<vector<Point2f> > >& candidatesSetOut,
vector<vector<vector<Point> > >& contoursSetOut, const DetectorParameters &_params) {
Mat grey = _grayImage.getMat();
CV_DbgAssert(grey.total() != 0);
CV_DbgAssert(grey.type() == CV_8UC1);
/// 1. DETECT FIRST SET OF CANDIDATES
vector<vector<Point2f> > candidates;
vector<vector<Point> > contours;
_detectInitialCandidates(grey, candidates, contours, _params);
/// 2. SORT CORNERS
_reorderCandidatesCorners(candidates);
/// 3. FILTER OUT NEAR CANDIDATE PAIRS
// save the outter/inner border (i.e. potential candidates)
_filterTooCloseCandidates(candidates, candidatesSetOut, contours, contoursSetOut,
_params.minMarkerDistanceRate, _params.detectInvertedMarker);
}
/**
* @brief Given an input image and candidate corners, extract the bits of the candidate, including
* the border bits
@@ -527,12 +421,10 @@ static int _getBorderErrors(const Mat &bits, int markerSize, int borderSize) {
* 1 if the candidate is a black candidate (default candidate)
* 2 if the candidate is a white candidate
*/
static uint8_t _identifyOneCandidate(const Dictionary& dictionary, InputArray _image,
static uint8_t _identifyOneCandidate(const Dictionary& dictionary, const Mat& _image,
const vector<Point2f>& _corners, int& idx,
const DetectorParameters& params, int& rotation,
const float scale = 1.f) {
CV_DbgAssert(_corners.size() == 4);
CV_DbgAssert(_image.getMat().total() != 0);
CV_DbgAssert(params.markerBorderBits > 0);
uint8_t typ=1;
// get bits
@@ -610,87 +502,6 @@ static size_t _findOptPyrImageForCanonicalImg(
return optLevel;
}
/**
* @brief Identify square candidates according to a marker dictionary
*/
static void _identifyCandidates(InputArray grey,
const vector<Mat>& image_pyr,
vector<vector<vector<Point2f> > >& _candidatesSet,
vector<vector<vector<Point> > >& _contoursSet, const Dictionary &_dictionary,
vector<vector<Point2f> >& _accepted, vector<vector<Point> >& _contours, vector<int>& ids,
const DetectorParameters &params,
OutputArrayOfArrays _rejected = noArray()) {
CV_DbgAssert(grey.getMat().total() != 0);
CV_DbgAssert(grey.getMat().type() == CV_8UC1);
int ncandidates = (int)_candidatesSet[0].size();
vector<vector<Point2f> > accepted;
vector<vector<Point2f> > rejected;
vector<vector<Point> > contours;
vector<int> idsTmp(ncandidates, -1);
vector<int> rotated(ncandidates, 0);
vector<uint8_t> validCandidates(ncandidates, 0);
//// Analyze each of the candidates
parallel_for_(Range(0, ncandidates), [&](const Range &range) {
const int begin = range.start;
const int end = range.end;
vector<vector<Point2f> >& candidates = params.detectInvertedMarker ? _candidatesSet[1] : _candidatesSet[0];
vector<vector<Point> >& contourS = params.detectInvertedMarker ? _contoursSet[1] : _contoursSet[0];
for(int i = begin; i < end; i++) {
int currId = -1;
// implements equation (4)
if (params.useAruco3Detection) {
const int perimeterOfContour = static_cast<int>(contourS[i].size());
const int min_perimeter = params.minSideLengthCanonicalImg * 4;
const size_t nearestImgId = _findOptPyrImageForCanonicalImg(image_pyr, grey.cols(), perimeterOfContour, min_perimeter);
const float scale = image_pyr[nearestImgId].cols / static_cast<float>(grey.cols());
validCandidates[i] = _identifyOneCandidate(_dictionary, image_pyr[nearestImgId], candidates[i], currId, params, rotated[i], scale);
}
else {
validCandidates[i] = _identifyOneCandidate(_dictionary, grey, candidates[i], currId, params, rotated[i]);
}
if(validCandidates[i] > 0)
idsTmp[i] = currId;
}
});
for(int i = 0; i < ncandidates; i++) {
if(validCandidates[i] > 0) {
// to choose the right set of candidates :: 0 for default, 1 for white markers
uint8_t set = validCandidates[i]-1;
// shift corner positions to the correct rotation
correctCornerPosition(_candidatesSet[set][i], rotated[i]);
if( !params.detectInvertedMarker && validCandidates[i] == 2 )
continue;
// add valid candidate
accepted.push_back(_candidatesSet[set][i]);
ids.push_back(idsTmp[i]);
contours.push_back(_contoursSet[set][i]);
} else {
rejected.push_back(_candidatesSet[0][i]);
}
}
// parse output
_accepted = accepted;
_contours= contours;
if(_rejected.needed()) {
_copyVector2Output(rejected, _rejected);
}
}
/**
* Line fitting A * B = C :: Called from function refineCandidateLines
@@ -846,16 +657,210 @@ struct ArucoDetector::ArucoDetectorImpl {
ArucoDetectorImpl(const Dictionary &_dictionary, const DetectorParameters &_detectorParams,
const RefineParameters& _refineParams): dictionary(_dictionary),
detectorParams(_detectorParams), refineParams(_refineParams) {}
/**
* @brief Detect square candidates in the input image
*/
void detectCandidates(const Mat& grey, vector<vector<Point2f> >& candidates, vector<vector<Point> >& contours) {
/// 1. DETECT FIRST SET OF CANDIDATES
_detectInitialCandidates(grey, candidates, contours, detectorParams);
/// 2. SORT CORNERS
_reorderCandidatesCorners(candidates);
}
float getAverageArucoPinSize(vector<Point2f> markerCorners) {
float averageArucoModuleSize = 0.f;
int numPins = dictionary.markerSize + detectorParams.markerBorderBits * 2;
for (size_t i = 0ull; i < markerCorners.size(); i++) {
averageArucoModuleSize += sqrt(normL2Sqr<float>(Point2f(markerCorners[i] - markerCorners[(i+1ull)%markerCorners.size()])));
/**
* @brief FILTER OUT NEAR CANDIDATE PAIRS
*
* save the outter/inner border (i.e. potential candidates) to vector<MarkerCandidateTree>,
* clear candidates and contours
*/
vector<MarkerCandidateTree>
filterTooCloseCandidates(vector<vector<Point2f> > &candidates, vector<vector<Point> > &contours) {
CV_Assert(detectorParams.minMarkerDistanceRate >= 0.);
vector<MarkerCandidateTree> candidateTree(candidates.size());
for(size_t i = 0ull; i < candidates.size(); i++) {
candidateTree[i] = MarkerCandidateTree(std::move(candidates[i]), std::move(contours[i]));
}
averageArucoModuleSize /= ((float)markerCorners.size()*numPins);
return averageArucoModuleSize;
}
candidates.clear();
contours.clear();
// sort candidates from big to small
std::sort(candidateTree.begin(), candidateTree.end());
// group index for each candidate
vector<int> groupId(candidateTree.size(), -1);
vector<vector<size_t> > groupedCandidates;
vector<bool> isSelectedContours(candidateTree.size(), true);
size_t countSelectedContours = 0ull;
for (size_t i = 0ull; i < candidateTree.size(); i++) {
for (size_t j = i + 1ull; j < candidateTree.size(); j++) {
float minDist = getAverageDistance(candidateTree[i].corners, candidateTree[j].corners);
// if mean distance is too low, group markers
// the distance between the points of two independent markers should be more than half the side of the marker
// half the side of the marker = (perimeter / 4) * 0.5 = perimeter * 0.125
if(minDist < candidateTree[j].perimeter*(float)detectorParams.minMarkerDistanceRate) {
isSelectedContours[i] = false;
isSelectedContours[j] = false;
// i and j are not related to a group
if(groupId[i] < 0 && groupId[j] < 0){
// mark candidates with their corresponding group number
groupId[i] = groupId[j] = (int)groupedCandidates.size();
// create group
groupedCandidates.push_back({i, j});
}
// i is related to a group
else if(groupId[i] > -1 && groupId[j] == -1) {
int group = groupId[i];
groupId[j] = group;
// add to group
groupedCandidates[group].push_back(j);
}
// j is related to a group
else if(groupId[j] > -1 && groupId[i] == -1) {
int group = groupId[j];
groupId[i] = group;
// add to group
groupedCandidates[group].push_back(i);
}
}
}
countSelectedContours += isSelectedContours[i];
}
for (vector<size_t>& grouped : groupedCandidates) {
if (detectorParams.detectInvertedMarker) // if detectInvertedMarker choose smallest contours
std::sort(grouped.begin(), grouped.end(), [](const size_t &a, const size_t &b) {
return a > b;
});
else // if detectInvertedMarker==false choose largest contours
std::sort(grouped.begin(), grouped.end());
size_t currId = grouped[0];
isSelectedContours[currId] = true;
for (size_t i = 1ull; i < grouped.size(); i++) {
size_t id = grouped[i];
float dist = getAverageDistance(candidateTree[id].corners, candidateTree[currId].corners);
float moduleSize = getAverageModuleSize(candidateTree[id].corners, dictionary.markerSize, detectorParams.markerBorderBits);
if (dist > detectorParams.minGroupDistance*moduleSize) {
currId = id;
candidateTree[grouped[0]].closeContours.push_back(candidateTree[id]);
}
}
}
vector<MarkerCandidateTree> selectedCandidates(countSelectedContours + groupedCandidates.size());
countSelectedContours = 0ull;
for (size_t i = 0ull; i < candidateTree.size(); i++) {
if (isSelectedContours[i]) {
selectedCandidates[countSelectedContours] = std::move(candidateTree[i]);
countSelectedContours++;
}
}
// find hierarchy in the candidate tree
for (int i = (int)selectedCandidates.size()-1; i >= 0; i--) {
for (int j = i - 1; j >= 0; j--) {
if (checkMarker1InMarker2(selectedCandidates[i].corners, selectedCandidates[j].corners)) {
selectedCandidates[i].parent = j;
selectedCandidates[j].depth = max(selectedCandidates[j].depth, selectedCandidates[i].depth + 1);
break;
}
}
}
return selectedCandidates;
}
/**
* @brief Identify square candidates according to a marker dictionary
*/
void identifyCandidates(const Mat& grey, const vector<Mat>& image_pyr, vector<MarkerCandidateTree>& selectedContours,
vector<vector<Point2f> >& accepted, vector<vector<Point> >& contours,
vector<int>& ids, OutputArrayOfArrays _rejected = noArray()) {
size_t ncandidates = selectedContours.size();
vector<vector<Point2f> > rejected;
vector<int> idsTmp(ncandidates, -1);
vector<int> rotated(ncandidates, 0);
vector<uint8_t> validCandidates(ncandidates, 0);
vector<bool> was(ncandidates, false);
bool checkCloseContours = true;
int maxDepth = 0;
for (size_t i = 0ull; i < selectedContours.size(); i++)
maxDepth = max(selectedContours[i].depth, maxDepth);
vector<vector<size_t>> depths(maxDepth+1);
for (size_t i = 0ull; i < selectedContours.size(); i++) {
depths[selectedContours[i].depth].push_back(i);
}
//// Analyze each of the candidates
int depth = 0;
size_t counter = 0;
while (counter < ncandidates) {
parallel_for_(Range(0, (int)depths[depth].size()), [&](const Range& range) {
const int begin = range.start;
const int end = range.end;
for (int i = begin; i < end; i++) {
size_t v = depths[depth][i];
was[v] = true;
Mat img = grey;
// implements equation (4)
if (detectorParams.useAruco3Detection) {
const int minPerimeter = detectorParams.minSideLengthCanonicalImg * 4;
const size_t nearestImgId = _findOptPyrImageForCanonicalImg(image_pyr, grey.cols, static_cast<int>(selectedContours[v].contour.size()), minPerimeter);
img = image_pyr[nearestImgId];
}
const float scale = detectorParams.useAruco3Detection ? img.cols / static_cast<float>(grey.cols) : 1.f;
validCandidates[v] = _identifyOneCandidate(dictionary, img, selectedContours[v].corners, idsTmp[v], detectorParams, rotated[v], scale);
if (validCandidates[v] == 0 && checkCloseContours) {
for (const MarkerCandidate& closeMarkerCandidate: selectedContours[v].closeContours) {
validCandidates[v] = _identifyOneCandidate(dictionary, img, closeMarkerCandidate.corners, idsTmp[v], detectorParams, rotated[v], scale);
if (validCandidates[v] > 0) {
selectedContours[v].corners = closeMarkerCandidate.corners;
selectedContours[v].contour = closeMarkerCandidate.contour;
break;
}
}
}
}
});
// visit the parent vertices of the detected markers to skip identify parent contours
for(size_t v : depths[depth]) {
if(validCandidates[v] > 0) {
int parent = selectedContours[v].parent;
while (parent != -1) {
if (!was[parent]) {
was[parent] = true;
counter++;
}
parent = selectedContours[parent].parent;
}
}
counter++;
}
depth++;
}
for (size_t i = 0ull; i < selectedContours.size(); i++) {
if (validCandidates[i] > 0) {
// shift corner positions to the correct rotation
correctCornerPosition(selectedContours[i].corners, rotated[i]);
accepted.push_back(selectedContours[i].corners);
contours.push_back(selectedContours[i].contour);
ids.push_back(idsTmp[i]);
}
else {
rejected.push_back(selectedContours[i].corners);
}
}
// parse output
if(_rejected.needed()) {
_copyVector2Output(rejected, _rejected);
}
}
};
@@ -929,23 +934,21 @@ void ArucoDetector::detectMarkers(InputArray _image, OutputArrayOfArrays _corner
vector<vector<Point> > contours;
vector<int> ids;
vector<vector<vector<Point2f> > > candidatesSet;
vector<vector<vector<Point> > > contoursSet;
/// STEP 2.a Detect marker candidates :: using AprilTag
if(detectorParams.cornerRefinementMethod == (int)CORNER_REFINE_APRILTAG){
_apriltag(grey, detectorParams, candidates, contours);
candidatesSet.push_back(candidates);
contoursSet.push_back(contours);
}
/// STEP 2.b Detect marker candidates :: traditional way
else
_detectCandidates(grey, candidatesSet, contoursSet, detectorParams);
else {
arucoDetectorImpl->detectCandidates(grey, candidates, contours);
}
/// STEP 2.c FILTER OUT NEAR CANDIDATE PAIRS
auto selectedCandidates = arucoDetectorImpl->filterTooCloseCandidates(candidates, contours);
/// STEP 2: Check candidate codification (identify markers)
_identifyCandidates(grey, grey_pyramid, candidatesSet, contoursSet, dictionary,
candidates, contours, ids, detectorParams, _rejectedImgPoints);
arucoDetectorImpl->identifyCandidates(grey, grey_pyramid, selectedCandidates, candidates, contours,
ids, _rejectedImgPoints);
/// STEP 3: Corner refinement :: use corner subpix
if (detectorParams.cornerRefinementMethod == (int)CORNER_REFINE_SUBPIX) {
@@ -963,7 +966,7 @@ void ArucoDetector::detectMarkers(InputArray _image, OutputArrayOfArrays _corner
}
else {
int cornerRefinementWinSize = std::max(1, cvRound(detectorParams.relativeCornerRefinmentWinSize*
arucoDetectorImpl->getAverageArucoPinSize(candidates[i])));
getAverageModuleSize(candidates[i], dictionary.markerSize, detectorParams.markerBorderBits)));
cornerRefinementWinSize = min(cornerRefinementWinSize, detectorParams.cornerRefinementWinSize);
cornerSubPix(grey, Mat(candidates[i]), Size(cornerRefinementWinSize, cornerRefinementWinSize), Size(-1, -1),
TermCriteria(TermCriteria::MAX_ITER | TermCriteria::EPS,
@@ -1238,7 +1241,7 @@ void ArucoDetector::refineDetectedMarkers(InputArray _image, const Board& _board
std::vector<Point2f> marker(closestRotatedMarker.begin<Point2f>(), closestRotatedMarker.end<Point2f>());
int cornerRefinementWinSize = std::max(1, cvRound(detectorParams.relativeCornerRefinmentWinSize*
arucoDetectorImpl->getAverageArucoPinSize(marker)));
getAverageModuleSize(marker, dictionary.markerSize, detectorParams.markerBorderBits)));
cornerRefinementWinSize = min(cornerRefinementWinSize, detectorParams.cornerRefinementWinSize);
cornerSubPix(grey, closestRotatedMarker,
Size(cornerRefinementWinSize, cornerRefinementWinSize),
@@ -314,7 +314,9 @@ struct CharucoDetector::CharucoDetectorImpl {
vector<vector<Point2f> > rejectedMarkers;
arucoDetector.detectMarkers(image, _markerCorners, _markerIds, rejectedMarkers);
if (charucoParameters.tryRefineMarkers)
arucoDetector.refineDetectedMarkers(image, board, _markerCorners, _markerIds, rejectedMarkers);
arucoDetector.refineDetectedMarkers(image, board, _markerCorners, _markerIds, rejectedMarkers);
if (_markerCorners.empty() && _markerIds.empty())
return;
}
// if camera parameters are avaible, use approximated calibration
if(!charucoParameters.cameraMatrix.empty())
+47
View File
@@ -48,6 +48,35 @@ public:
topK = top_k;
}
FaceDetectorYNImpl(const String& framework,
const std::vector<uchar>& bufferModel,
const std::vector<uchar>& bufferConfig,
const Size& input_size,
float score_threshold,
float nms_threshold,
int top_k,
int backend_id,
int target_id)
:divisor(32),
strides({8, 16, 32})
{
net = dnn::readNet(framework, bufferModel, bufferConfig);
CV_Assert(!net.empty());
net.setPreferableBackend(backend_id);
net.setPreferableTarget(target_id);
inputW = input_size.width;
inputH = input_size.height;
padW = (int((inputW - 1) / divisor) + 1) * divisor;
padH = (int((inputH - 1) / divisor) + 1) * divisor;
scoreThreshold = score_threshold;
nmsThreshold = nms_threshold;
topK = top_k;
}
void setInputSize(const Size& input_size) override
{
inputW = input_size.width;
@@ -264,4 +293,22 @@ Ptr<FaceDetectorYN> FaceDetectorYN::create(const String& model,
#endif
}
Ptr<FaceDetectorYN> FaceDetectorYN::create(const String& framework,
const std::vector<uchar>& bufferModel,
const std::vector<uchar>& bufferConfig,
const Size& input_size,
const float score_threshold,
const float nms_threshold,
const int top_k,
const int backend_id,
const int target_id)
{
#ifdef HAVE_OPENCV_DNN
return makePtr<FaceDetectorYNImpl>(framework, bufferModel, bufferConfig, input_size, score_threshold, nms_threshold, top_k, backend_id, target_id);
#else
CV_UNUSED(bufferModel); CV_UNUSED(bufferConfig); CV_UNUSED(input_size); CV_UNUSED(score_threshold); CV_UNUSED(nms_threshold); CV_UNUSED(top_k); CV_UNUSED(backend_id); CV_UNUSED(target_id);
CV_Error(cv::Error::StsNotImplemented, "cv::FaceDetectorYN requires enabled 'dnn' module.");
#endif
}
} // namespace cv
@@ -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
+48 -51
View File
@@ -2728,7 +2728,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];
@@ -2782,6 +2781,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; }
@@ -2811,65 +2814,79 @@ 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)
@@ -2877,24 +2894,15 @@ bool QRDecode::straightDecodingProcess()
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):
@@ -4477,25 +4485,14 @@ static
vector<QRCode> analyzeFinderPatterns(const vector<vector<Point2f> > &corners, const Mat& img,
const QRCodeDetectorAruco::Params& qrDetectorParameters) {
vector<QRCode> qrCodes;
vector<FinderPatternInfo> patterns;
vector<FinderPatternInfo> patterns(corners.size());
if (img.empty())
return qrCodes;
float maxModuleSize = 0.f;
for (size_t i = 0ull; i < corners.size(); i++) {
FinderPatternInfo pattern = FinderPatternInfo(corners[i]);
// TODO: improve thinning Aruco markers
bool isUniq = true;
for (const FinderPatternInfo& tmp : patterns) {
Point2f dist = pattern.center - tmp.center;
if (max(abs(dist.x), abs(dist.y)) < 3.f * tmp.moduleSize) {
isUniq = false;
break;
}
}
if (isUniq) {
patterns.push_back(pattern);
maxModuleSize = max(maxModuleSize, patterns.back().moduleSize);
}
patterns[i] = pattern;
maxModuleSize = max(maxModuleSize, pattern.moduleSize);
}
const int threshold = cvRound(qrDetectorParameters.minModuleSizeInPyramid * 12.5f) +
(cvRound(qrDetectorParameters.minModuleSizeInPyramid * 12.5f) % 2 ? 0 : 1);
+656 -90
View File
@@ -6,6 +6,8 @@
#include "precomp.hpp"
#include "qrcode_encoder_table.inl.hpp"
#include "graphical_code_detector_impl.hpp"
namespace cv
{
using std::vector;
@@ -19,6 +21,7 @@ const uint8_t INVALID_REGION_VALUE = 110;
static void decToBin(const int dec_number, const int total_bits, std::vector<uint8_t> &bin_number);
static uint8_t gfPow(uint8_t x, int power);
static uint8_t gfMul(const uint8_t x, const uint8_t y);
static uint8_t gfDiv(const uint8_t x, const uint8_t y);
static void gfPolyMul(const vector<uint8_t> &p, const vector<uint8_t> &q, vector<uint8_t> &product);
static void gfPolyDiv(const vector<uint8_t> &dividend, const vector<uint8_t> &divisor, const int ecc_num, vector<uint8_t> &quotient);
static void polyGenerator(const int n, vector<uint8_t> &result);
@@ -51,6 +54,13 @@ static uint8_t gfMul(const uint8_t x, const uint8_t y)
return gf_exp[(gf_log[x] + gf_log[y]) % 255];
}
static uint8_t gfDiv(const uint8_t x, const uint8_t y)
{
if (x == 0 || y == 0)
return 0;
return gf_exp[(gf_log[x] + 255 - gf_log[y]) % 255];
}
static void gfPolyMul(const vector<uint8_t> &p, const vector<uint8_t> &q, vector<uint8_t> &product)
{
int len_p = (int)p.size();
@@ -141,6 +151,8 @@ static int mapSymbol(char c)
return -1;
}
static void maskData(const Mat& original, const int mask_type_num, Mat &masked);
QRCodeEncoder::QRCodeEncoder()
{
// nothing
@@ -196,17 +208,18 @@ protected:
uint8_t total_num;
vector<Mat> final_qrcodes;
Ptr<VersionInfo> version_info;
Ptr<BlockParams> cur_ecc_params;
const VersionInfo* version_info;
const BlockParams* cur_ecc_params;
bool isNumeric(const std::string& input);
bool isAlphaNumeric(const std::string& input);
bool isNumeric(const std::string& input) const;
bool isAlphaNumeric(const std::string& input) const;
EncodeMode autoEncodeMode(const std::string &input) const ;
bool encodeByte(const std::string& input, vector<uint8_t> &output);
bool encodeAlpha(const std::string& input, vector<uint8_t> &output);
bool encodeNumeric(const std::string& input, vector<uint8_t> &output);
bool encodeECI(const std::string& input, vector<uint8_t> &output);
bool encodeKanji(const std::string& input, vector<uint8_t> &output);
bool encodeAuto(const std::string& input, vector<uint8_t> &output);
bool encodeAuto(const std::string& input, vector<uint8_t> &output, EncodeMode *mode = nullptr);
bool encodeStructure(const std::string& input, vector<uint8_t> &output);
int eccLevelToCode(CorrectionLevel level);
void padBitStream();
@@ -220,11 +233,10 @@ protected:
void formatGenerate(const int mask_type_num, vector<uint8_t> &format_array);
void versionInfoGenerate(const int version_level_num, vector<uint8_t> &version_array);
void fillReserved(const vector<uint8_t> &format_array, Mat &masked);
void maskData(const int mask_type_num, Mat &masked);
void findAutoMaskType();
bool estimateVersion(const int input_length, vector<int> &possible_version);
bool estimateVersion(const int input_length, EncodeMode mode, vector<int> &possible_version);
int versionAuto(const std::string &input_str);
int findVersionCapacity(const int input_length, const int ecc, const int version_begin, const int version_end);
int findVersionCapacity(const int input_length, const int ecc, const std::vector<int>& possible_versions);
void generatingProcess(const std::string& input, Mat &qrcode);
void generateQR(const std::string& input);
};
@@ -247,17 +259,17 @@ int QRCodeEncoderImpl::eccLevelToCode(CorrectionLevel level)
"CORRECT_LEVEL_L, CORRECT_LEVEL_M, CORRECT_LEVEL_Q, CORRECT_LEVEL_H." );
}
int QRCodeEncoderImpl::findVersionCapacity(const int input_length, const int ecc, const int version_begin, const int version_end)
int QRCodeEncoderImpl::findVersionCapacity(const int input_length, const int ecc, const std::vector<int>& possible_versions)
{
int data_codewords, version_index = -1;
const int byte_len = 8;
version_index = -1;
for (int i = version_begin; i < version_end; i++)
for (int i : possible_versions)
{
Ptr<BlockParams> tmp_ecc_params = makePtr<BlockParams>(version_info_database[i].ecc[ecc]);
data_codewords = tmp_ecc_params->data_codewords_in_G1 * tmp_ecc_params->num_blocks_in_G1 +
tmp_ecc_params->data_codewords_in_G2 * tmp_ecc_params->num_blocks_in_G2;
auto& tmp_ecc_params = version_info_database[i].ecc[ecc];
data_codewords = tmp_ecc_params.data_codewords_in_G1 * tmp_ecc_params.num_blocks_in_G1 +
tmp_ecc_params.data_codewords_in_G2 * tmp_ecc_params.num_blocks_in_G2;
if (data_codewords * byte_len >= input_length)
{
@@ -268,53 +280,70 @@ int QRCodeEncoderImpl::findVersionCapacity(const int input_length, const int ecc
return version_index;
}
bool QRCodeEncoderImpl::estimateVersion(const int input_length, vector<int>& possible_version)
static inline int getCapacity(int version, QRCodeEncoder::CorrectionLevel ecc_level, QRCodeEncoder::EncodeMode mode) {
const int* capacity = version_capacity_database[version].ec_level[ecc_level].encoding_modes;
switch (mode) {
case QRCodeEncoder::EncodeMode::MODE_NUMERIC:
return capacity[0];
case QRCodeEncoder::EncodeMode::MODE_ALPHANUMERIC:
return capacity[1];
case QRCodeEncoder::EncodeMode::MODE_BYTE:
return capacity[2];
case QRCodeEncoder::EncodeMode::MODE_KANJI:
return capacity[3];
default:
CV_Error(Error::StsNotImplemented, format("Unexpected mode %d", mode));
}
}
bool QRCodeEncoderImpl::estimateVersion(const int input_length, EncodeMode mode, vector<int>& possible_version)
{
possible_version.clear();
if (input_length > version_capacity_database[40].ec_level[ecc_level].encoding_modes[1])
CV_Assert(mode != EncodeMode::MODE_AUTO);
if (input_length > getCapacity(MAX_VERSION, ecc_level, mode))
{
return false;
if (input_length <= version_capacity_database[9].ec_level[ecc_level].encoding_modes[3])
{
possible_version.push_back(1);
}
else if (input_length <= version_capacity_database[9].ec_level[ecc_level].encoding_modes[1])
int version = MAX_VERSION;
for (; version > 0; --version)
{
possible_version.push_back(1);
possible_version.push_back(2);
if (input_length > getCapacity(version, ecc_level, mode)) {
break;
}
}
else if (input_length <= version_capacity_database[26].ec_level[ecc_level].encoding_modes[3])
if (version < MAX_VERSION)
{
possible_version.push_back(2);
version += 1;
}
else if (input_length <= version_capacity_database[26].ec_level[ecc_level].encoding_modes[1])
possible_version.push_back(version);
if (version < MAX_VERSION)
{
possible_version.push_back(2);
possible_version.push_back(3);
}
else
{
possible_version.push_back(3);
possible_version.push_back(version + 1);
}
return true;
}
int QRCodeEncoderImpl::versionAuto(const std::string& input_str)
{
vector<int> possible_version;
estimateVersion((int)input_str.length(), possible_version);
int tmp_version = 0;
vector<uint8_t> payload_tmp;
int version_range[5] = {0, 1, 10, 27, 41};
for(size_t i = 0; i < possible_version.size(); i++)
{
int version_range_index = possible_version[i];
EncodeMode mode;
encodeAuto(input_str, payload_tmp, &mode);
encodeAuto(input_str, payload_tmp);
tmp_version = findVersionCapacity((int)payload_tmp.size(), ecc_level,
version_range[version_range_index], version_range[version_range_index + 1]);
if(tmp_version != -1)
break;
vector<int> possible_version;
if (!estimateVersion((int)input_str.length(), mode, possible_version)) {
return -1;
}
const auto tmp_version = findVersionCapacity((int)payload_tmp.size(), ecc_level, possible_version);
return tmp_version;
}
@@ -342,20 +371,23 @@ void QRCodeEncoderImpl::generateQR(const std::string &input)
std::string input_info = input.substr(segment_begin, segment_end);
string_itr += segment_end;
int detected_version = versionAuto(input_info);
CV_Assert(detected_version != -1);
if (version_level == 0)
version_level = detected_version;
else if (version_level < detected_version)
int tmp_version_level = version_level;
if (detected_version == -1)
CV_Error(Error::StsBadArg, "The given input exceeds the maximum capacity of a QR code with the selected encoding mode and error correction level " );
else if (tmp_version_level == 0)
tmp_version_level = detected_version;
else if (tmp_version_level < detected_version)
CV_Error(Error::StsBadArg, "The given version is not suitable for the given input string length ");
payload.clear();
payload.reserve(MAX_PAYLOAD_LEN);
format = vector<uint8_t> (15, 255);
version_reserved = vector<uint8_t> (18, 255);
version_size = (21 + (version_level - 1) * 4);
version_info = makePtr<VersionInfo>(version_info_database[version_level]);
cur_ecc_params = makePtr<BlockParams>(version_info->ecc[ecc_level]);
version_size = (21 + (tmp_version_level - 1) * 4);
version_info = &version_info_database[tmp_version_level];
cur_ecc_params = &version_info->ecc[ecc_level];
original = Mat(Size(version_size, version_size), CV_8UC1, Scalar(255));
masked_data = original.clone();
Mat qrcode = masked_data.clone();
@@ -366,36 +398,10 @@ void QRCodeEncoderImpl::generateQR(const std::string &input)
void QRCodeEncoderImpl::formatGenerate(const int mask_type_num, vector<uint8_t> &format_array)
{
const int mask_bits_num = 3;
const int level_bits_num = 2;
std::vector<uint8_t> mask_type_bin(mask_bits_num);
std::vector<uint8_t> ec_level_bin(level_bits_num);
decToBin(mask_type_num, mask_bits_num, mask_type_bin);
decToBin(eccLevelToCode(ecc_level), level_bits_num, ec_level_bin);
std::vector<uint8_t> format_bits;
hconcat(ec_level_bin, mask_type_bin, format_bits);
std::reverse(format_bits.begin(), format_bits.end());
const int ecc_info_bits = 10;
std::vector<uint8_t> shift(ecc_info_bits, 0);
std::vector<uint8_t> polynomial;
hconcat(shift, format_bits, polynomial);
const int generator_len = 11;
const uint8_t generator_arr[generator_len] = {1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1};
std::vector<uint8_t> format_generator (generator_arr, generator_arr + sizeof(generator_arr) / sizeof(generator_arr[0]));
vector<uint8_t> ecc_code;
gfPolyDiv(polynomial, format_generator, ecc_info_bits, ecc_code);
hconcat(ecc_code, format_bits, format_array);
const uint8_t mask_arr[MAX_FORMAT_LENGTH] = {0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1};
std::vector<uint8_t> system_mask (mask_arr, mask_arr + sizeof(mask_arr) / sizeof(mask_arr[0]));
for(int i = 0; i < MAX_FORMAT_LENGTH; i++)
{
format_array[i] ^= system_mask[i];
int idx = (eccLevelToCode(ecc_level) << 3) | mask_type_num;
format_array.resize(MAX_FORMAT_LENGTH);
for (int i = 0; i < MAX_FORMAT_LENGTH; ++i) {
format_array[i] = (formatInfoLUT[idx] >> i) & 1;
}
}
@@ -613,7 +619,7 @@ bool QRCodeEncoderImpl::encodeStructure(const std::string& input, vector<uint8_t
return encodeAuto(input, output);
}
bool QRCodeEncoderImpl::isNumeric(const std::string& input)
bool QRCodeEncoderImpl::isNumeric(const std::string& input) const
{
for (size_t i = 0; i < input.length(); i++)
{
@@ -623,7 +629,7 @@ bool QRCodeEncoderImpl::isNumeric(const std::string& input)
return true;
}
bool QRCodeEncoderImpl::isAlphaNumeric(const std::string& input)
bool QRCodeEncoderImpl::isAlphaNumeric(const std::string& input) const
{
for (size_t i = 0; i < input.length(); i++)
{
@@ -633,14 +639,56 @@ bool QRCodeEncoderImpl::isAlphaNumeric(const std::string& input)
return true;
}
bool QRCodeEncoderImpl::encodeAuto(const std::string& input, vector<uint8_t>& output)
QRCodeEncoder::EncodeMode QRCodeEncoderImpl::autoEncodeMode(const std::string &input) const
{
if (isNumeric(input))
encodeNumeric(input, output);
else if (isAlphaNumeric(input))
encodeAlpha(input, output);
else
encodeByte(input, output);
{
return EncodeMode::MODE_NUMERIC;
}
if (isAlphaNumeric(input))
{
return EncodeMode::MODE_ALPHANUMERIC;
}
return EncodeMode::MODE_BYTE;
}
bool QRCodeEncoderImpl::encodeAuto(const std::string& input, vector<uint8_t>& output, EncodeMode *mode)
{
const auto selected_mode = autoEncodeMode(input);
CV_Assert(selected_mode != EncodeMode::MODE_AUTO);
switch (selected_mode)
{
case EncodeMode::MODE_NUMERIC:
encodeNumeric(input, output);
break;
case EncodeMode::MODE_ALPHANUMERIC:
encodeAlpha(input, output);
break;
case EncodeMode::MODE_STRUCTURED_APPEND:
encodeByte(input, output);
break;
case EncodeMode::MODE_BYTE:
encodeByte(input, output);
break;
case EncodeMode::MODE_KANJI:
encodeKanji(input, output);
break;
case EncodeMode::MODE_ECI:
encodeECI(input, output);
break;
default:
break;
}
if (mode != nullptr)
{
*mode = selected_mode;
}
return true;
}
@@ -787,7 +835,7 @@ void QRCodeEncoderImpl::findAutoMaskType()
{
Mat test_result = masked_data.clone();
vector<uint8_t> test_format = format;
maskData(cur_type, test_result);
maskData(original, cur_type, test_result);
formatGenerate(cur_type, test_format);
fillReserved(test_format, test_result);
int continued_num = 0;
@@ -899,8 +947,9 @@ void QRCodeEncoderImpl::findAutoMaskType()
mask_type = best_index;
}
void QRCodeEncoderImpl::maskData(const int mask_type_num, Mat& masked)
void maskData(const Mat& original, const int mask_type_num, Mat& masked)
{
int version_size = original.rows;
for (int i = 0; i < version_size; i++)
{
for (int j = 0; j < version_size; j++)
@@ -1204,7 +1253,7 @@ void QRCodeEncoderImpl::structureFinalMessage()
writeReservedArea();
writeData();
findAutoMaskType();
maskData(mask_type, masked_data);
maskData(original, mask_type, masked_data);
formatGenerate(mask_type, format);
versionInfoGenerate(version_level, version_reserved);
fillReserved(format, masked_data);
@@ -1260,4 +1309,521 @@ Ptr<QRCodeEncoder> QRCodeEncoder::create(const QRCodeEncoder::Params& parameters
return makePtr<QRCodeEncoderImpl>(parameters);
}
class QRCodeDecoderImpl : public QRCodeDecoder {
public:
bool decode(const Mat& straight, String& decoded_info) CV_OVERRIDE;
private:
QRCodeEncoder::CorrectionLevel level;
int version;
struct Bitstream {
int next(int bits) {
CV_Assert(idx < data.size());
int val = 0;
while (bits >= actualBits) {
val |= data[idx++] << (bits - actualBits);
bits -= actualBits;
actualBits = 8;
}
if (bits) {
val |= data[idx] >> (actualBits - bits);
actualBits -= bits;
data[idx] &= 255 >> (8 - actualBits);
}
return val;
}
bool empty() {
return idx >= data.size();
}
std::vector<uint8_t> data;
int actualBits = 8;
size_t idx = 0;
} bitstream;
bool run(const Mat& straight, String& decoded_info);
bool decodeFormatInfo(const Mat& straight, int& mask);
bool correctFormatInfo(uint16_t& format_info);
void extractCodewords(Mat& source, std::vector<uint8_t>& codewords);
bool errorCorrection(std::vector<uint8_t>& codewords);
bool errorCorrectionBlock(std::vector<uint8_t>& codewords);
void decodeSymbols(String& result);
void decodeNumeric(String& result);
void decodeAlpha(String& result);
void decodeByte(String& result);
void decodeECI(String& result);
void decodeKanji(String& result);
};
QRCodeDecoder::~QRCodeDecoder()
{
// nothing
}
Ptr<QRCodeDecoder> QRCodeDecoder::create() {
return makePtr<QRCodeDecoderImpl>();
}
bool QRCodeDecoderImpl::decode(const Mat& _straight, String& decoded_info) {
Mat straight = ~_straight; // Invert modules
bool decoded = run(straight, decoded_info);
if (!decoded) {
cv::transpose(straight, straight);
decoded = run(straight, decoded_info);
}
return decoded;
}
// Unmask format info bits and apply error correction
bool QRCodeDecoderImpl::correctFormatInfo(uint16_t& format_info) {
static const uint16_t mask_pattern = 0b101010000010010;
cv::Hamming hd;
for (int i = 0; i < 32; ++i) {
// Compute Hamming distance
int distance = hd(reinterpret_cast<const unsigned char*>(&formatInfoLUT[i]),
reinterpret_cast<const unsigned char*>(&format_info), 2);
// Up to 3 bit errors might be corrected.
// So if distance is less or equal than 3 - we found a correct format info.
if (distance <= 3) {
format_info = formatInfoLUT[i] ^ mask_pattern;
return true;
}
}
return false;
}
bool QRCodeDecoderImpl::decodeFormatInfo(const Mat& straight, int& mask) {
// Read left-top format info
uint16_t format_info = 0;
for (int i = 0; i < 6; ++i)
format_info |= (straight.at<uint8_t>(i, 8) & 1) << i;
format_info |= (straight.at<uint8_t>(7, 8) & 1) << 6;
format_info |= (straight.at<uint8_t>(8, 8) & 1) << 7;
format_info |= (straight.at<uint8_t>(8, 7) & 1) << 8;
for (int i = 9; i < 15; ++i)
format_info |= (straight.at<uint8_t>(8, 14 - i) & 1) << i;
bool correct = correctFormatInfo(format_info);
// Format information 15bit sequence appears twice.
// Try extract format info from different position.
uint16_t format_info_dup = 0;
for (int i = 0; i < 8; ++i)
format_info_dup |= (straight.at<uint8_t>(8, straight.cols - 1 - i) & 1) << i;
for (int i = 0; i < 7; ++i)
format_info_dup |= (straight.at<uint8_t>(straight.rows - 7 + i, 8) & 1) << (i + 8);
if (correctFormatInfo(format_info_dup)) {
// Both strings must be the same
if (correct && format_info != format_info_dup)
return false;
format_info = format_info_dup;
} else {
if (!correct)
return false;
}
switch((format_info >> 13) & 0b11) {
case 0: level = QRCodeEncoder::CorrectionLevel::CORRECT_LEVEL_M; break;
case 1: level = QRCodeEncoder::CorrectionLevel::CORRECT_LEVEL_L; break;
case 2: level = QRCodeEncoder::CorrectionLevel::CORRECT_LEVEL_H; break;
case 3: level = QRCodeEncoder::CorrectionLevel::CORRECT_LEVEL_Q; break;
};
mask = (format_info >> 10) & 0b111;
return true;
}
bool QRCodeDecoderImpl::run(const Mat& straight, String& decoded_info) {
CV_Assert(straight.rows == straight.cols);
version = (straight.rows - 21) / 4 + 1;
decoded_info = "";
mode = static_cast<QRCodeEncoder::EncodeMode>(0);
eci = static_cast<QRCodeEncoder::ECIEncodings>(0);
// Decode format info
int maskPattern;
bool decoded = decodeFormatInfo(straight, maskPattern);
if (!decoded) {
return false;
}
// Generate data mask
Mat masked = straight.clone();
maskData(straight, maskPattern, masked);
extractCodewords(masked, bitstream.data);
if (!errorCorrection(bitstream.data)) {
return false;
}
decodeSymbols(decoded_info);
return true;
}
bool QRCodeDecoderImpl::errorCorrection(std::vector<uint8_t>& codewords) {
CV_CheckEQ((int)codewords.size(), version_info_database[version].total_codewords,
"Number of codewords");
int numBlocks = version_info_database[version].ecc[level].num_blocks_in_G1 +
version_info_database[version].ecc[level].num_blocks_in_G2;
if (numBlocks == 1) {
return errorCorrectionBlock(codewords);
}
size_t numData = 0;
std::vector<int> blockSizes;
blockSizes.reserve(numBlocks);
for (int i = 0; i < version_info_database[version].ecc[level].num_blocks_in_G1; ++i) {
blockSizes.push_back(version_info_database[version].ecc[level].data_codewords_in_G1);
numData += blockSizes.back();
}
for (int i = 0; i < version_info_database[version].ecc[level].num_blocks_in_G2; ++i) {
blockSizes.push_back(version_info_database[version].ecc[level].data_codewords_in_G2);
numData += blockSizes.back();
}
// TODO: parallel_for
std::vector<std::vector<uint8_t>> blocks(numBlocks);
int minBlockSize = *std::min_element(blockSizes.begin(), blockSizes.end());
size_t offset = 0;
for (int i = 0; i < minBlockSize; ++i) {
for (int j = 0; j < numBlocks; ++j) {
blocks[j].push_back(codewords[offset++]);
}
}
// Put remaining data codewords
for (int j = 0; j < numBlocks; ++j) {
CV_Assert(blockSizes[j] == minBlockSize || blockSizes[j] == minBlockSize + 1);
if (blockSizes[j] > minBlockSize)
blocks[j].push_back(codewords[offset++]);
}
// Copy error correction codewords
int numEcc = version_info_database[version].ecc[level].ecc_codewords;
for (int i = 0; i < numEcc; ++i) {
for (int j = 0; j < numBlocks; ++j) {
blocks[j].push_back(codewords[offset++]);
}
}
parallel_for_(Range(0, numBlocks), [&](const Range& r) {
for (int i = r.start; i < r.end; ++i) {
if (!errorCorrectionBlock(blocks[i])) {
blocks[i].clear();
return;
}
}
});
// Collect blocks back after error correction. Trim error correction codewords.
codewords.resize(numData);
offset = 0;
for (size_t i = 0; i < blocks.size(); ++i) {
if (blocks[i].empty())
return false;
std::copy(blocks[i].begin(), blocks[i].end(), codewords.begin() + offset);
offset += blocks[i].size();
}
return true;
}
bool QRCodeDecoderImpl::errorCorrectionBlock(std::vector<uint8_t>& codewords) {
size_t numEcc = version_info_database[version].ecc[level].ecc_codewords;
size_t numSyndromes = numEcc;
// According to the ISO there is a formula for a number of the syndromes.
// However several tests don't pass the error correction step because of less number of syndromes:
// 1M: qrcodes/detection/lots/image001.jpg from BoofCV (8 syndromes by formula, 10 needed)
// 1L: Objdetect_QRCode_Multi.regression/13 (4 syndromes by formula, 6 needed)
// 2L: qrcodes/detection/brightness/image011.jpg from BoofCV (8 syndromes by formula, 10 needed)
if (numSyndromes % 2 == 1)
numSyndromes -= 1;
// Compute syndromes
bool hasError = false;
std::vector<uint8_t> syndromes(numSyndromes, codewords[0]);
for (size_t i = 0; i < syndromes.size(); ++i) {
for (size_t j = 1; j < codewords.size(); ++j) {
syndromes[i] = gfMul(syndromes[i], gfPow(2, static_cast<int>(i))) ^ codewords[j];
}
hasError |= syndromes[i] != 0;
}
if (!hasError) {
// Trim error correction codewords
codewords.resize(codewords.size() - numEcc);
return true;
}
// Run BerlekampMassey algorithm to find error positions (coefficients of locator poly)
size_t L = 0; // number of assumed errors
size_t m = 1; // shift value (between C and B)
uint8_t b = 1; // discrepancy from last L update
std::vector<uint8_t> C(numSyndromes, 0); // Error locator polynomial
std::vector<uint8_t> B(numSyndromes, 0); // A copy of error locator from previos L update
C[0] = B[0] = 1;
for (size_t i = 0; i < numSyndromes; ++i) {
CV_Assert(m + L - 1 < C.size()); // m >= 1 on any iteration
uint8_t discrepancy = syndromes[i];
for (size_t j = 1; j <= L; ++j) {
discrepancy ^= gfMul(C[j], syndromes[i - j]);
}
if (discrepancy == 0) {
m += 1;
} else {
std::vector<uint8_t> C_copy = C;
uint8_t inv_b = gfDiv(1, b);
uint8_t tmp = gfMul(discrepancy, inv_b);
for (size_t j = 0; j < L; ++j) {
C[m + j] ^= gfMul(tmp, B[j]);
}
if (2 * L <= i) {
L = i + 1 - L;
B = C_copy;
b = discrepancy;
m = 1;
} else {
m += 1;
}
}
}
// There is an error at i-th position if i is a root of locator poly
std::vector<size_t> errLocs;
errLocs.reserve(L);
for (size_t i = 0; i < codewords.size(); ++i) {
uint8_t val = 1;
uint8_t pos = gfPow(2, static_cast<int>(i));
for (size_t j = 1; j <= L; ++j) {
val = gfMul(val, pos) ^ C[j];
}
if (val == 0) {
errLocs.push_back(static_cast<int>(codewords.size() - 1 - i));
}
}
// Number of assumed errors does not match number of error locations
if (errLocs.size() != L)
return false;
// Forney algorithm for error correction using syndromes and known error locations
std::vector<uint8_t> errEval;
gfPolyMul(C, syndromes, errEval);
for (size_t i = 0; i < errLocs.size(); ++i) {
uint8_t numenator = 0, denominator = 0;
uint8_t X = gfPow(2, static_cast<int>(codewords.size() - 1 - errLocs[i]));
uint8_t inv_X = gfDiv(1, X);
for (size_t j = 0; j < L; ++j) {
numenator = gfMul(numenator, inv_X) ^ errEval[L - 1 - j];
}
// Compute demoninator as a product of (1-X_i * X_k) for i != k
// TODO: optimize, there is a dubplicated compute
denominator = 1;
for (size_t j = 0; j < errLocs.size(); ++j) {
if (i == j)
continue;
uint8_t Xj = gfPow(2, static_cast<int>(codewords.size() - 1 - errLocs[j]));
denominator = gfMul(denominator, 1 ^ gfMul(inv_X, Xj));
}
uint8_t errValue = gfDiv(numenator, denominator);
codewords[errLocs[i]] ^= errValue;
}
// Trim error correction codewords
codewords.resize(codewords.size() - numEcc);
return true;
}
void QRCodeDecoderImpl::extractCodewords(Mat& source, std::vector<uint8_t>& codewords) {
const VersionInfo& version_info = version_info_database[version];
// Mask alignment markers
std::vector<int> alignCenters;
alignCenters.reserve(MAX_ALIGNMENT);
for (int i = 0; i < MAX_ALIGNMENT && version_info.alignment_pattern[i]; i++)
alignCenters.push_back(version_info.alignment_pattern[i]);
for (size_t i = 0; i < alignCenters.size(); i++)
{
for (size_t j = 0; j < alignCenters.size(); j++)
{
if ((i == alignCenters.size() - 1 && j == 0) || (i == 0 && j == 0) ||
(j == alignCenters.size() - 1 && i == 0))
continue;
int x = alignCenters[i];
int y = alignCenters[j];
Mat area = source({x - 2, x + 3}, {y - 2, y + 3});
area.setTo(INVALID_REGION_VALUE);
}
}
// Mask detection markers
source.rowRange(0, 9).colRange(source.cols - 8, source.cols).setTo(INVALID_REGION_VALUE);
source.rowRange(0, 9).colRange(0, 9).setTo(INVALID_REGION_VALUE);
source.colRange(0, 9).rowRange(source.rows - 8, source.rows).setTo(INVALID_REGION_VALUE);
// Mask Version Information blocks
if (version >= 7) {
source.rowRange(0, 6).colRange(source.cols - 12, source.cols - 9).setTo(INVALID_REGION_VALUE);
source.colRange(0, 6).rowRange(source.rows - 12, source.rows - 9).setTo(INVALID_REGION_VALUE);
}
// Mask timing pattern
source.row(6) = INVALID_REGION_VALUE;
std::vector<uint8_t> bits;
bits.reserve(source.total() - source.cols);
bool moveUpwards = true;
for (auto& data : {source.colRange(7, source.cols), source.colRange(0, 6)}) {
for (int i = data.cols / 2 - 1; i >= 0; --i) {
Mat col0 = data.col(i * 2);
Mat col1 = data.col(i * 2 + 1);
for (int j = 0; j < data.rows; ++j) {
if (moveUpwards) {
bits.push_back(col1.at<uint8_t>(data.rows - 1 - j));
bits.push_back(col0.at<uint8_t>(data.rows - 1 - j));
} else {
bits.push_back(col1.at<uint8_t>(j));
bits.push_back(col0.at<uint8_t>(j));
}
}
moveUpwards = !moveUpwards;
}
}
// Combine bits to codewords
size_t numCodewords = version_info.total_codewords;
codewords.resize(numCodewords);
size_t offset = 0;
for (size_t i = 0; i < numCodewords; ++i) {
codewords[i] = 0;
for (size_t j = 0; j < 8; ++j) {
while (bits[offset] == INVALID_REGION_VALUE) {
offset += 1;
CV_Assert(offset < bits.size());
}
codewords[i] |= (bits[offset] & 1) << (7 - j);
offset += 1;
}
}
}
void QRCodeDecoderImpl::decodeSymbols(String& result) {
CV_Assert(!bitstream.empty());
// Decode depends on the mode
result = "";
while (!bitstream.empty()) {
// Determine mode
auto currMode = static_cast<QRCodeEncoder::EncodeMode>(bitstream.next(4));
if (this->mode == 0) {
mode = currMode;
}
if (currMode == 0 || bitstream.empty())
return;
if (currMode == QRCodeEncoder::EncodeMode::MODE_NUMERIC)
decodeNumeric(result);
else if (currMode == QRCodeEncoder::EncodeMode::MODE_ALPHANUMERIC)
decodeAlpha(result);
else if (currMode == QRCodeEncoder::EncodeMode::MODE_BYTE)
decodeByte(result);
else if (currMode == QRCodeEncoder::EncodeMode::MODE_ECI)
decodeECI(result);
else if (currMode == QRCodeEncoder::EncodeMode::MODE_KANJI)
decodeKanji(result);
else
CV_Error(Error::StsNotImplemented, format("mode %d", currMode));
}
}
void QRCodeDecoderImpl::decodeNumeric(String& result) {
int numDigits = bitstream.next(version <= 9 ? 10 : (version <= 26 ? 12 : 14));
for (int i = 0; i < numDigits / 3; ++i) {
int triple = bitstream.next(10);
result += static_cast<char>('0' + triple / 100);
result += static_cast<char>('0' + (triple / 10) % 10);
result += static_cast<char>('0' + triple % 10);
}
int remainingDigits = numDigits % 3;
if (remainingDigits) {
int triple = bitstream.next(remainingDigits == 1 ? 4 : 7);
if (remainingDigits == 2)
result += '0' + (triple / 10) % 10;
result += '0' + triple % 10;
}
}
void QRCodeDecoderImpl::decodeAlpha(String& result) {
static const char map[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', ' ', '$', '%', '*',
'+', '-', '.', '/', ':'};
int num = bitstream.next(version <= 9 ? 9 : (version <= 26 ? 11 : 13));
for (int i = 0; i < num / 2; ++i) {
int tuple = bitstream.next(11);
result += map[tuple / 45];
result += map[tuple % 45];
}
if (num % 2) {
int value = bitstream.next(6);
result += map[value];
}
}
void QRCodeDecoderImpl::decodeByte(String& result) {
int num = bitstream.next(version <= 9 ? 8 : 16);
for (int i = 0; i < num; ++i) {
result += static_cast<char>(bitstream.next(8));
}
}
void QRCodeDecoderImpl::decodeECI(String& result) {
int eciAssignValue = bitstream.next(8);
for (int i = 0; i < 8; ++i) {
if (eciAssignValue & 1 << (7 - i))
eciAssignValue |= bitstream.next(8) << (i + 1) * 8;
else
break;
}
if (this->eci == 0) {
this->eci = static_cast<QRCodeEncoder::ECIEncodings>(eciAssignValue);
}
decodeSymbols(result);
}
void QRCodeDecoderImpl::decodeKanji(String& result) {
int num = bitstream.next(version <= 9 ? 8 : (version <= 26 ? 10 : 12));
for (int i = 0; i < num; ++i) {
int data = bitstream.next(13);
int high_byte = data / 0xC0;
int low_byte = data - high_byte * 0xC0;
int symbol = (high_byte << 8) + low_byte;
if (0 <= symbol && symbol <= 0x9FFC - 0x8140) {
symbol += 0x8140;
} else if (0xE040 - 0xC140 <= symbol && symbol <= 0xEBBF - 0xC140) {
symbol += 0xC140;
}
result += (symbol >> 8) & 0xff;
result += symbol & 0xff;
}
}
}
@@ -857,4 +857,13 @@ static const uint8_t gf_log[256] = {
0x4f, 0xae, 0xd5, 0xe9, 0xe6, 0xe7, 0xad, 0xe8,
0x74, 0xd6, 0xf4, 0xea, 0xa8, 0x50, 0x58, 0xaf
};
// There are only 32 combinations of format info sequences.
static const uint16_t formatInfoLUT[32] = {
0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0,
0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976,
0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b,
0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed
};
}
@@ -613,6 +613,32 @@ TEST(CV_ArucoDetectMarkers, regression_2492)
}
}
TEST(CV_ArucoDetectMarkers, regression_contour_24220)
{
aruco::ArucoDetector detector;
vector<int> markerIds;
vector<vector<Point2f> > markerCorners;
string imgPath = cvtest::findDataFile("aruco/failmask9.png");
Mat image = imread(imgPath);
const size_t N = 1ull;
const int goldCorners[8] = {392,175, 99,257, 117,109, 365,44};
const int goldCornersId = 0;
detector.detectMarkers(image, markerCorners, markerIds);
ASSERT_EQ(N, markerIds.size());
ASSERT_EQ(4ull, markerCorners[0].size());
ASSERT_EQ(goldCornersId, markerIds[0]);
for (int j = 0; j < 4; j++)
{
EXPECT_NEAR(static_cast<float>(goldCorners[j * 2]), markerCorners[0][j].x, 1.f);
EXPECT_NEAR(static_cast<float>(goldCorners[j * 2 + 1]), markerCorners[0][j].y, 1.f);
}
}
struct ArucoThreading: public testing::TestWithParam<aruco::CornerRefineMethod>
{
struct NumThreadsSetter {
@@ -652,7 +652,7 @@ TEST(Charuco, issue_14014)
EXPECT_EQ(Size(4, 1), corners[0].size()); // check dimension of detected corners
size_t numRejPoints = rejectedPoints.size();
ASSERT_EQ(rejectedPoints.size(), 26ull); // optional check to track regressions
ASSERT_EQ(rejectedPoints.size(), 24ull); // optional check to track regressions
EXPECT_EQ(Size(4, 1), rejectedPoints[0].size()); // check dimension of detected corners
detector.refineDetectedMarkers(img, board, corners, ids, rejectedPoints);
+2 -5
View File
@@ -10,9 +10,6 @@ void check_qr(const string& root, const string& name_current_image, const string
const std::vector<Point>& corners,
const std::vector<string>& decoded_info, const int max_pixel_error,
bool isMulti = false) {
#ifndef HAVE_QUIRC
CV_UNUSED(decoded_info);
#endif
const std::string dataset_config = findDataFile(root + "dataset_config.json");
FileStorage file_config(dataset_config, FileStorage::READ);
ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
@@ -50,7 +47,7 @@ void check_qr(const string& root, const string& name_current_image, const string
EXPECT_NEAR(y, corners[i].y, max_pixel_error);
}
}
#ifdef HAVE_QUIRC
if (decoded_info.size() == 0ull)
return;
if (isMulti) {
@@ -70,7 +67,7 @@ void check_qr(const string& root, const string& name_current_image, const string
std::string original_info = config["info"];
EXPECT_EQ(decoded_info[0], original_info);
}
#endif
return; // done
}
}
+10 -54
View File
@@ -56,9 +56,8 @@ TEST(Objdetect_QRCode, generate_test_data)
std::string decoded_info;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
EXPECT_TRUE(detectQRCode(src, corners));
#ifdef HAVE_QUIRC
EXPECT_TRUE(decodeQRCode(src, corners, decoded_info, straight_barcode));
#endif
file_config << "x" << "[:";
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
file_config << "]";
@@ -95,9 +94,8 @@ TEST(Objdetect_QRCode_Close, generate_test_data)
Size new_size(width, height);
resize(src, barcode, new_size, 0, 0, INTER_LINEAR_EXACT);
EXPECT_TRUE(detectQRCode(barcode, corners));
#ifdef HAVE_QUIRC
EXPECT_TRUE(decodeQRCode(barcode, corners, decoded_info, straight_barcode));
#endif
file_config << "x" << "[:";
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
file_config << "]";
@@ -133,9 +131,8 @@ TEST(Objdetect_QRCode_Monitor, generate_test_data)
Size new_size(width, height);
resize(src, barcode, new_size, 0, 0, INTER_LINEAR_EXACT);
EXPECT_TRUE(detectQRCode(barcode, corners));
#ifdef HAVE_QUIRC
EXPECT_TRUE(decodeQRCode(barcode, corners, decoded_info, straight_barcode));
#endif
file_config << "x" << "[:";
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
file_config << "]";
@@ -165,9 +162,8 @@ TEST(Objdetect_QRCode_Curved, generate_test_data)
std::string decoded_info;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
EXPECT_TRUE(detectQRCode(src, corners));
#ifdef HAVE_QUIRC
EXPECT_TRUE(decodeCurvedQRCode(src, corners, decoded_info, straight_barcode));
#endif
file_config << "x" << "[:";
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
file_config << "]";
@@ -198,11 +194,10 @@ TEST(Objdetect_QRCode_Multi, generate_test_data)
std::vector<Point> corners;
QRCodeDetector qrcode;
EXPECT_TRUE(qrcode.detectMulti(src, corners));
#ifdef HAVE_QUIRC
std::vector<cv::String> decoded_info;
std::vector<Mat> straight_barcode;
EXPECT_TRUE(qrcode.decodeMulti(src, corners, decoded_info, straight_barcode));
#endif
file_config << "x" << "[:";
for(size_t j = 0; j < corners.size(); j += 4)
{
@@ -256,15 +251,11 @@ TEST_P(Objdetect_QRCode, regression)
std::vector<Point> corners;
std::string decoded_info;
QRCodeDetector qrcode;
#ifdef HAVE_QUIRC
decoded_info = qrcode.detectAndDecode(src, corners, straight_barcode);
ASSERT_FALSE(corners.empty());
ASSERT_FALSE(decoded_info.empty());
int expected_barcode_type = CV_8UC1;
EXPECT_EQ(expected_barcode_type, straight_barcode.type());
#else
ASSERT_TRUE(qrcode.detect(src, corners));
#endif
check_qr(root, name_current_image, "test_images", corners, {decoded_info}, pixels_error);
}
@@ -287,15 +278,11 @@ TEST_P(Objdetect_QRCode_Close, regression)
std::vector<Point> corners;
std::string decoded_info;
QRCodeDetector qrcode;
#ifdef HAVE_QUIRC
decoded_info = qrcode.detectAndDecode(barcode, corners, straight_barcode);
ASSERT_FALSE(corners.empty());
ASSERT_FALSE(decoded_info.empty());
int expected_barcode_type = CV_8UC1;
EXPECT_EQ(expected_barcode_type, straight_barcode.type());
#else
ASSERT_TRUE(qrcode.detect(barcode, corners));
#endif
check_qr(root, name_current_image, "close_images", corners, {decoded_info}, pixels_error);
}
@@ -318,15 +305,11 @@ TEST_P(Objdetect_QRCode_Monitor, regression)
std::vector<Point> corners;
std::string decoded_info;
QRCodeDetector qrcode;
#ifdef HAVE_QUIRC
decoded_info = qrcode.detectAndDecode(barcode, corners, straight_barcode);
ASSERT_FALSE(corners.empty());
ASSERT_FALSE(decoded_info.empty());
int expected_barcode_type = CV_8UC1;
EXPECT_EQ(expected_barcode_type, straight_barcode.type());
#else
ASSERT_TRUE(qrcode.detect(barcode, corners));
#endif
check_qr(root, name_current_image, "monitor_images", corners, {decoded_info}, pixels_error);
}
@@ -344,15 +327,11 @@ TEST_P(Objdetect_QRCode_Curved, regression)
std::vector<Point> corners;
std::string decoded_info;
QRCodeDetector qrcode;
#ifdef HAVE_QUIRC
decoded_info = qrcode.detectAndDecodeCurved(src, corners, straight_barcode);
ASSERT_FALSE(corners.empty());
ASSERT_FALSE(decoded_info.empty());
int expected_barcode_type = CV_8UC1;
EXPECT_EQ(expected_barcode_type, straight_barcode.type());
#else
ASSERT_TRUE(qrcode.detect(src, corners));
#endif
check_qr(root, name_current_image, "test_images", corners, {decoded_info}, pixels_error);
}
@@ -375,7 +354,6 @@ TEST_P(Objdetect_QRCode_Multi, regression)
}
std::vector<Point> corners;
std::vector<cv::String> decoded_info;
#ifdef HAVE_QUIRC
std::vector<Mat> straight_barcode;
EXPECT_TRUE(qrcode.detectAndDecodeMulti(src, decoded_info, corners, straight_barcode));
ASSERT_FALSE(corners.empty());
@@ -383,9 +361,6 @@ TEST_P(Objdetect_QRCode_Multi, regression)
int expected_barcode_type = CV_8UC1;
for(size_t i = 0; i < straight_barcode.size(); i++)
EXPECT_EQ(expected_barcode_type, straight_barcode[i].type());
#else
ASSERT_TRUE(qrcode.detectMulti(src, corners));
#endif
check_qr(root, name_current_image, "multiple_images", corners, decoded_info, pixels_error, true);
}
@@ -398,7 +373,6 @@ INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Multi, testing::Combine(testing::
TEST(Objdetect_QRCode_decodeMulti, decode_regression_16491)
{
#ifdef HAVE_QUIRC
Mat zero_image = Mat::zeros(256, 256, CV_8UC1);
Point corners_[] = {Point(16, 16), Point(128, 16), Point(128, 128), Point(16, 128),
Point(16, 16), Point(128, 16), Point(128, 128), Point(16, 128)};
@@ -413,7 +387,6 @@ TEST(Objdetect_QRCode_decodeMulti, decode_regression_16491)
Mat mat_corners(2, 4, CV_32SC2, (void*)&vec_corners[0]);
QRCodeDetector mat_qrcode;
EXPECT_NO_THROW(mat_qrcode.decodeMulti(zero_image, mat_corners, decoded_info, straight_barcode));
#endif
}
typedef testing::TestWithParam<std::string> Objdetect_QRCode_detectMulti;
@@ -449,7 +422,6 @@ TEST_P(Objdetect_QRCode_detectAndDecodeMulti, check_output_parameters_type_19363
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path);
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
#ifdef HAVE_QUIRC
GraphicalCodeDetector qrcode = QRCodeDetector();
if (method == "aruco_based") {
qrcode = QRCodeDetectorAruco();
@@ -467,7 +439,6 @@ TEST_P(Objdetect_QRCode_detectAndDecodeMulti, check_output_parameters_type_19363
ASSERT_FALSE(corners.empty());
for(size_t i = 0; i < straight_barcode.size(); i++)
EXPECT_EQ(expected_barcode_type, straight_barcode[i].type());
#endif
}
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_detectAndDecodeMulti, testing::Values("contours_based", "aruco_based"));
@@ -487,9 +458,7 @@ TEST(Objdetect_QRCode_detect, detect_regression_20882)
cv::String decoded_info;
EXPECT_TRUE(qrcode.detect(src, corners));
EXPECT_TRUE(!corners.empty());
#ifdef HAVE_QUIRC
EXPECT_NO_THROW(qrcode.decode(src, corners, straight_barcode));
#endif
}
TEST(Objdetect_QRCode_basic, not_found_qrcode)
@@ -500,10 +469,8 @@ TEST(Objdetect_QRCode_basic, not_found_qrcode)
Mat zero_image = Mat::zeros(256, 256, CV_8UC1);
QRCodeDetector qrcode;
EXPECT_FALSE(qrcode.detect(zero_image, corners));
#ifdef HAVE_QUIRC
corners = std::vector<Point>(4);
EXPECT_ANY_THROW(qrcode.decode(zero_image, corners, straight_barcode));
#endif
}
TEST(Objdetect_QRCode_detect, detect_regression_21287)
@@ -521,9 +488,7 @@ TEST(Objdetect_QRCode_detect, detect_regression_21287)
cv::String decoded_info;
EXPECT_TRUE(qrcode.detect(src, corners));
EXPECT_TRUE(!corners.empty());
#ifdef HAVE_QUIRC
EXPECT_NO_THROW(qrcode.decode(src, corners, straight_barcode));
#endif
}
TEST(Objdetect_QRCode_detect_flipped, regression_23249)
@@ -549,12 +514,10 @@ TEST(Objdetect_QRCode_detect_flipped, regression_23249)
EXPECT_TRUE(qrcode.detect(src, corners));
EXPECT_TRUE(!corners.empty());
std::string decoded_msg;
#ifdef HAVE_QUIRC
const std::string &expect_msg = flipped_image.second;
EXPECT_NO_THROW(decoded_msg = qrcode.decode(src, corners, straight_barcode));
ASSERT_FALSE(straight_barcode.empty()) << "Can't decode qrimage.";
EXPECT_EQ(expect_msg, decoded_msg);
#endif
const std::string &expect_msg = flipped_image.second;
EXPECT_NO_THROW(decoded_msg = qrcode.decode(src, corners, straight_barcode));
ASSERT_FALSE(straight_barcode.empty()) << "Can't decode qrimage.";
EXPECT_EQ(expect_msg, decoded_msg);
}
}
@@ -577,12 +540,10 @@ TEST(Objdetect_QRCode_decode, decode_regression_21929)
EXPECT_TRUE(qrcode.detect(src, corners));
EXPECT_TRUE(!corners.empty());
#ifdef HAVE_QUIRC
cv::String decoded_msg;
EXPECT_NO_THROW(decoded_msg = qrcode.decode(src, corners, straight_barcode));
ASSERT_FALSE(straight_barcode.empty()) << "Can't decode qrimage.";
EXPECT_EQ(expect_msg, decoded_msg);
#endif
}
TEST(Objdetect_QRCode_decode, decode_regression_version_25)
@@ -603,12 +564,11 @@ TEST(Objdetect_QRCode_decode, decode_regression_version_25)
EXPECT_TRUE(qrcode.detect(src, corners));
EXPECT_TRUE(!corners.empty());
#ifdef HAVE_QUIRC
cv::String decoded_msg;
EXPECT_NO_THROW(decoded_msg = qrcode.decode(src, corners, straight_barcode));
ASSERT_FALSE(straight_barcode.empty()) << "Can't decode qrimage.";
EXPECT_EQ(expect_msg, decoded_msg);
#endif
}
TEST_P(Objdetect_QRCode_detectAndDecodeMulti, decode_9_qrcodes_version7)
@@ -639,9 +599,6 @@ TEST_P(Objdetect_QRCode_detectAndDecodeMulti, decode_9_qrcodes_version7)
TEST(Objdetect_QRCode_detectAndDecode, utf8_output)
{
#ifndef HAVE_QUIRC
throw SkipTestException("Quirc is required for decoding");
#else
const std::string name_current_image = "umlaut.png";
const std::string root = "qrcode/";
@@ -655,7 +612,6 @@ TEST(Objdetect_QRCode_detectAndDecode, utf8_output)
std::string decoded_info = qrcode.detectAndDecode(src, corners, straight);
EXPECT_FALSE(decoded_info.empty());
EXPECT_NE(decoded_info.find("M\xc3\xbcllheimstrasse"), std::string::npos);
#endif // HAVE_QUIRC
}
}} // namespace
+56 -12
View File
@@ -118,9 +118,7 @@ TEST(Objdetect_QRCode_Encode, generate_test_data)
}
std::string decoded_info = "";
#ifdef HAVE_QUIRC
EXPECT_TRUE(decodeQRCode(resized_src, corners, decoded_info, straight_barcode)) << "The QR code cannot be decoded: " << image_path;
#endif
file_config << "info" << decoded_info;
file_config << "}";
}
@@ -306,7 +304,6 @@ TEST(Objdetect_QRCode_Encode_Decode, regression)
corners[k].y = corners[k].y * height_ratio;
}
#ifdef HAVE_QUIRC
Mat straight_barcode;
std::string output_info = QRCodeDetector().decode(resized_src, corners, straight_barcode);
EXPECT_FALSE(output_info.empty())
@@ -314,7 +311,6 @@ TEST(Objdetect_QRCode_Encode_Decode, regression)
<< " version: " << version << " error correction level: " << (int)level;
EXPECT_EQ(input_info, output_info) << "The generated QRcode is not same as test data." << " Mode: " << (int)mode <<
" version: " << version << " error correction level: " << (int)level;
#endif
}
}
}
@@ -356,12 +352,10 @@ TEST(Objdetect_QRCode_Encode_Kanji, regression)
corners[j].y = corners[j].y * height_ratio;
}
#ifdef HAVE_QUIRC
Mat straight_barcode;
std::string decoded_info = QRCodeDetector().decode(resized_src, corners, straight_barcode);
EXPECT_FALSE(decoded_info.empty()) << "The generated QRcode cannot be decoded.";
EXPECT_EQ(input_info, decoded_info);
#endif
}
}
@@ -423,21 +417,15 @@ TEST(Objdetect_QRCode_Encode_Decode_Structured_Append, DISABLED_regression)
corners[m].y = corners[m].y * height_ratio;
}
#ifdef HAVE_QUIRC
Mat straight_barcode;
std::string decoded_info = QRCodeDetector().decode(resized_src, corners, straight_barcode);
EXPECT_FALSE(decoded_info.empty())
<< "The generated QRcode cannot be decoded." << " Mode: " << modes[i]
<< " structures number: " << k << "/" << j;
output_info += decoded_info;
#endif
}
#ifdef HAVE_QUIRC
EXPECT_EQ(input_info, output_info) << "The generated QRcode is not same as test data." << " Mode: " << mode <<
" structures number: " << j;
#else
std::cout << "Mode=" << mode << ": Unable to verify generated QR codes - QUIRC is disabled" << std::endl;
#endif
}
}
}
@@ -548,4 +536,60 @@ TEST(Objdetect_QRCode_Encode_Decode, regression_issue22029)
}
}
// This test reproduces issue https://github.com/opencv/opencv/issues/24366 only in a loop
TEST(Objdetect_QRCode_Encode_Decode, auto_version_pick)
{
cv::QRCodeEncoder::Params params;
params.correction_level = cv::QRCodeEncoder::CORRECT_LEVEL_L;
params.mode = cv::QRCodeEncoder::EncodeMode::MODE_AUTO;
cv::Ptr<cv::QRCodeEncoder> encoder = cv::QRCodeEncoder::create(params);
for (int len = 1; len < 19; len++) {
std::string input;
input.resize(len);
cv::randu(Mat(1, len, CV_8U, &input[0]), 'a', 'z' + 1);
cv::Mat qrcode;
encoder->encode(input, qrcode);
}
}
// Test two QR codes which error correction procedure requires more number of
// syndroms that described in the ISO/IEC 18004
typedef testing::TestWithParam<std::pair<std::string, std::string>> Objdetect_QRCode_decoding;
TEST_P(Objdetect_QRCode_decoding, error_correction)
{
const std::string filename = get<0>(GetParam());
const std::string expected = get<1>(GetParam());
QRCodeDetector qrcode;
cv::String decoded_msg;
Mat src = cv::imread(findDataFile("qrcode/" + filename), IMREAD_GRAYSCALE);
std::vector<Point2f> corners(4);
corners[0] = Point2f(0, 0);
corners[1] = Point2f(src.cols * 1.0f, 0);
corners[2] = Point2f(src.cols * 1.0f, src.rows * 1.0f);
corners[3] = Point2f(0, src.rows * 1.0f);
Mat resized_src;
resize(src, resized_src, fixed_size, 0, 0, INTER_AREA);
float width_ratio = resized_src.cols * 1.0f / src.cols;
float height_ratio = resized_src.rows * 1.0f / src.rows;
for(size_t m = 0; m < corners.size(); m++)
{
corners[m].x = corners[m].x * width_ratio;
corners[m].y = corners[m].y * height_ratio;
}
Mat straight_barcode;
EXPECT_NO_THROW(decoded_msg = qrcode.decode(resized_src, corners, straight_barcode));
ASSERT_FALSE(straight_barcode.empty()) << "Can't decode qrimage " << filename;
EXPECT_EQ(expected, decoded_msg);
}
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_decoding, testing::ValuesIn(std::vector<std::pair<std::string, std::string>>{
{"err_correct_1M.png", "New"},
{"err_correct_2L.png", "Version 2 QR Code Test Image"},
}));
}} // namespace