chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:06:04 +08:00
commit 86c9b1c39f
7743 changed files with 3316339 additions and 0 deletions
@@ -0,0 +1,140 @@
///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
// Niko Li, newlife20080214@gmail.com
// Jia Haipeng, jiahaipeng95@gmail.com
// Shengen Yan, yanshengen@gmail.com
// Jiang Liyuan,jlyuan001.good@163.com
// Rock Li, Rock.Li@amd.com
// Zailong Wu, bullet@yeah.net
// Yao Wang, bitwangyaoyao@gmail.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "../test_precomp.hpp"
#include "opencv2/ts/ocl_test.hpp"
#ifdef HAVE_OPENCL
namespace opencv_test {
namespace ocl {
///////////////////// HOG /////////////////////////////
PARAM_TEST_CASE(HOG, Size, MatType)
{
Size winSize;
int type;
Mat img;
UMat uimg;
virtual void SetUp()
{
winSize = GET_PARAM(0);
type = GET_PARAM(1);
img = readImage("cascadeandhog/images/image_00000000_0.png", IMREAD_GRAYSCALE);
ASSERT_FALSE(img.empty());
img.copyTo(uimg);
}
};
OCL_TEST_P(HOG, GetDescriptors)
{
HOGDescriptor hog;
hog.gammaCorrection = true;
hog.setSVMDetector(hog.getDefaultPeopleDetector());
std::vector<float> cpu_descriptors;
std::vector<float> gpu_descriptors;
OCL_OFF(hog.compute(img, cpu_descriptors, hog.winSize));
OCL_ON(hog.compute(uimg, gpu_descriptors, hog.winSize));
Mat cpu_desc(cpu_descriptors), gpu_desc(gpu_descriptors);
EXPECT_MAT_SIMILAR(cpu_desc, gpu_desc, 1e-1);
}
OCL_TEST_P(HOG, SVMDetector)
{
HOGDescriptor hog_first, hog_second;
// empty -> empty
hog_first.copyTo(hog_second);
// first -> both
hog_first.setSVMDetector(hog_first.getDefaultPeopleDetector());
hog_first.copyTo(hog_second);
// both -> both
hog_first.copyTo(hog_second);
// second -> empty
hog_first.setSVMDetector(cv::noArray());
hog_first.copyTo(hog_second);
}
OCL_TEST_P(HOG, Detect)
{
HOGDescriptor hog;
hog.winSize = winSize;
hog.gammaCorrection = true;
if (winSize.width == 48 && winSize.height == 96)
hog.setSVMDetector(hog.getDaimlerPeopleDetector());
else
hog.setSVMDetector(hog.getDefaultPeopleDetector());
std::vector<Rect> cpu_found;
std::vector<Rect> gpu_found;
OCL_OFF(hog.detectMultiScale(img, cpu_found, 0, Size(8, 8), Size(0, 0), 1.05, 6));
OCL_ON(hog.detectMultiScale(uimg, gpu_found, 0, Size(8, 8), Size(0, 0), 1.05, 6));
EXPECT_LT(checkRectSimilarity(img.size(), cpu_found, gpu_found), 0.05);
}
INSTANTIATE_TEST_CASE_P(OCL_ObjDetect, HOG, testing::Combine(
testing::Values(Size(64, 128), Size(48, 96)),
testing::Values( MatType(CV_8UC1) ) ) );
}} // namespace
#endif
@@ -0,0 +1,249 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include "opencv2/objdetect/aruco_detector.hpp"
namespace opencv_test { namespace {
TEST(CV_ArucoTutorial, can_find_singlemarkersoriginal)
{
string img_path = cvtest::findDataFile("aruco/singlemarkersoriginal.jpg");
Mat image = imread(img_path);
aruco::ArucoDetector detector(aruco::getPredefinedDictionary(aruco::DICT_6X6_250));
vector<int> ids;
vector<vector<Point2f> > corners, rejected;
const size_t N = 6ull;
// corners of ArUco markers with indices goldCornersIds
const int goldCorners[N][8] = { {359,310, 404,310, 410,350, 362,350}, {427,255, 469,256, 477,289, 434,288},
{233,273, 190,273, 196,241, 237,241}, {298,185, 334,186, 335,212, 297,211},
{425,163, 430,186, 394,186, 390,162}, {195,155, 230,155, 227,178, 190,178} };
const int goldCornersIds[N] = { 40, 98, 62, 23, 124, 203};
map<int, const int*> mapGoldCorners;
for (size_t i = 0; i < N; i++)
mapGoldCorners[goldCornersIds[i]] = goldCorners[i];
detector.detectMarkers(image, corners, ids, rejected);
ASSERT_EQ(N, ids.size());
for (size_t i = 0; i < N; i++)
{
int arucoId = ids[i];
ASSERT_EQ(4ull, corners[i].size());
ASSERT_TRUE(mapGoldCorners.find(arucoId) != mapGoldCorners.end());
for (int j = 0; j < 4; j++)
{
EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j * 2]), corners[i][j].x, 1.f);
EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j * 2 + 1]), corners[i][j].y, 1.f);
}
}
}
TEST(CV_ArucoTutorial, can_find_gboriginal)
{
string imgPath = cvtest::findDataFile("aruco/gboriginal.jpg");
Mat image = imread(imgPath);
string dictPath = cvtest::findDataFile("aruco/tutorial_dict.yml");
aruco::Dictionary dictionary;
FileStorage fs(dictPath, FileStorage::READ);
dictionary.aruco::Dictionary::readDictionary(fs.root()); // set marker from tutorial_dict.yml
aruco::DetectorParameters detectorParams;
aruco::ArucoDetector detector(dictionary, detectorParams);
vector<int> ids;
vector<vector<Point2f> > corners, rejected;
const size_t N = 35ull;
// corners of ArUco markers with indices 0, 1, ..., 34
const int goldCorners[N][8] = { {252,74, 286,81, 274,102, 238,95}, {295,82, 330,89, 319,111, 282,104},
{338,91, 375,99, 365,121, 327,113}, {383,100, 421,107, 412,130, 374,123},
{429,109, 468,116, 461,139, 421,132}, {235,100, 270,108, 257,130, 220,122},
{279,109, 316,117, 304,140, 266,133}, {324,119, 362,126, 352,150, 313,143},
{371,128, 410,136, 400,161, 360,152}, {418,139, 459,145, 451,170, 410,163},
{216,128, 253,136, 239,161, 200,152}, {262,138, 300,146, 287,172, 248,164},
{309,148, 349,156, 337,183, 296,174}, {358,158, 398,167, 388,194, 346,185},
{407,169, 449,176, 440,205, 397,196}, {196,158, 235,168, 218,195, 179,185},
{243,170, 283,178, 269,206, 228,197}, {293,180, 334,190, 321,218, 279,209},
{343,192, 385,200, 374,230, 330,220}, {395,203, 438,211, 429,241, 384,233},
{174,192, 215,201, 197,231, 156,221}, {223,204, 265,213, 249,244, 207,234},
{275,215, 317,225, 303,257, 259,246}, {327,227, 371,238, 359,270, 313,259},
{381,240, 426,249, 416,282, 369,273}, {151,228, 193,238, 173,271, 130,260},
{202,241, 245,251, 228,285, 183,274}, {255,254, 300,264, 284,299, 238,288},
{310,267, 355,278, 342,314, 295,302}, {366,281, 413,290, 402,327, 353,317},
{125,267, 168,278, 147,314, 102,303}, {178,281, 223,293, 204,330, 157,317},
{233,296, 280,307, 263,346, 214,333}, {291,310, 338,322, 323,363, 274,349},
{349,325, 399,336, 386,378, 335,366} };
map<int, const int*> mapGoldCorners;
for (int i = 0; i < static_cast<int>(N); i++)
mapGoldCorners[i] = goldCorners[i];
detector.detectMarkers(image, corners, ids, rejected);
ASSERT_EQ(N, ids.size());
for (size_t i = 0; i < N; i++)
{
int arucoId = ids[i];
ASSERT_EQ(4ull, corners[i].size());
ASSERT_TRUE(mapGoldCorners.find(arucoId) != mapGoldCorners.end());
for (int j = 0; j < 4; j++)
{
EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j*2]), corners[i][j].x, 1.f);
EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j*2+1]), corners[i][j].y, 1.f);
}
}
}
TEST(CV_ArucoTutorial, can_find_choriginal)
{
string imgPath = cvtest::findDataFile("aruco/choriginal.jpg");
Mat image = imread(imgPath);
aruco::ArucoDetector detector(aruco::getPredefinedDictionary(aruco::DICT_6X6_250));
vector< int > ids;
vector< vector< Point2f > > corners, rejected;
const size_t N = 17ull;
// corners of aruco markers with indices goldCornersIds
const int goldCorners[N][8] = { {268,77, 290,80, 286,97, 263,94}, {360,90, 382,93, 379,111, 357,108},
{211,106, 233,109, 228,127, 205,123}, {306,120, 328,124, 325,142, 302,138},
{402,135, 425,139, 423,157, 400,154}, {247,152, 271,155, 267,174, 242,171},
{347,167, 371,171, 369,191, 344,187}, {185,185, 209,189, 203,210, 178,206},
{288,201, 313,206, 309,227, 284,223}, {393,218, 418,222, 416,245, 391,241},
{223,240, 250,244, 244,268, 217,263}, {333,258, 359,262, 356,286, 329,282},
{152,281, 179,285, 171,312, 143,307}, {267,300, 294,305, 289,331, 261,327},
{383,319, 410,324, 408,351, 380,347}, {194,347, 223,352, 216,382, 186,377},
{315,368, 345,373, 341,403, 310,398} };
map<int, const int*> mapGoldCorners;
for (int i = 0; i < static_cast<int>(N); i++)
mapGoldCorners[i] = goldCorners[i];
detector.detectMarkers(image, corners, ids, rejected);
ASSERT_EQ(N, ids.size());
for (size_t i = 0; i < N; i++)
{
int arucoId = ids[i];
ASSERT_EQ(4ull, corners[i].size());
ASSERT_TRUE(mapGoldCorners.find(arucoId) != mapGoldCorners.end());
for (int j = 0; j < 4; j++)
{
EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j * 2]), corners[i][j].x, 1.f);
EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j * 2 + 1]), corners[i][j].y, 1.f);
}
}
}
TEST(CV_ArucoTutorial, can_find_chocclusion)
{
string imgPath = cvtest::findDataFile("aruco/chocclusion_original.jpg");
Mat image = imread(imgPath);
aruco::ArucoDetector detector(aruco::getPredefinedDictionary(aruco::DICT_6X6_250));
vector< int > ids;
vector< vector< Point2f > > corners, rejected;
const size_t N = 13ull;
// corners of aruco markers with indices goldCornersIds
const int goldCorners[N][8] = { {301,57, 322,62, 317,79, 295,73}, {391,80, 413,85, 408,103, 386,97},
{242,79, 264,85, 256,102, 234,96}, {334,103, 357,109, 352,126, 329,121},
{428,129, 451,134, 448,152, 425,146}, {274,128, 296,134, 290,153, 266,147},
{371,154, 394,160, 390,180, 366,174}, {208,155, 232,161, 223,181, 199,175},
{309,182, 333,188, 327,209, 302,203}, {411,210, 436,216, 432,238, 407,231},
{241,212, 267,219, 258,242, 232,235}, {167,244, 194,252, 183,277, 156,269},
{202,314, 230,322, 220,349, 191,341} };
map<int, const int*> mapGoldCorners;
const int goldCornersIds[N] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15};
for (int i = 0; i < static_cast<int>(N); i++)
mapGoldCorners[goldCornersIds[i]] = goldCorners[i];
detector.detectMarkers(image, corners, ids, rejected);
ASSERT_EQ(N, ids.size());
for (size_t i = 0; i < N; i++)
{
int arucoId = ids[i];
ASSERT_EQ(4ull, corners[i].size());
ASSERT_TRUE(mapGoldCorners.find(arucoId) != mapGoldCorners.end());
for (int j = 0; j < 4; j++)
{
EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j * 2]), corners[i][j].x, 1.f);
EXPECT_NEAR(static_cast<float>(mapGoldCorners[arucoId][j * 2 + 1]), corners[i][j].y, 1.f);
}
}
}
TEST(CV_ArucoTutorial, can_find_diamondmarkers)
{
string imgPath = cvtest::findDataFile("aruco/diamondmarkers.jpg");
Mat image = imread(imgPath);
string dictPath = cvtest::findDataFile("aruco/tutorial_dict.yml");
aruco::Dictionary dictionary;
FileStorage fs(dictPath, FileStorage::READ);
dictionary.aruco::Dictionary::readDictionary(fs.root()); // set marker from tutorial_dict.yml
string detectorPath = cvtest::findDataFile("aruco/detector_params.yml");
fs = FileStorage(detectorPath, FileStorage::READ);
aruco::DetectorParameters detectorParams;
detectorParams.readDetectorParameters(fs.root());
detectorParams.cornerRefinementMethod = aruco::CORNER_REFINE_APRILTAG;
detectorParams.validBitIdThreshold = 0.5f;
aruco::CharucoBoard charucoBoard(Size(3, 3), 0.4f, 0.25f, dictionary);
aruco::CharucoDetector detector(charucoBoard, aruco::CharucoParameters(), detectorParams);
vector<int> ids;
vector<vector<Point2f> > corners, diamondCorners;
vector<Vec4i> diamondIds;
const size_t N = 12ull;
// corner indices of ArUco markers
const int goldCornersIds[N] = { 4, 12, 11, 3, 12, 10, 12, 10, 10, 11, 2, 11 };
map<int, int> counterGoldCornersIds;
for (int i = 0; i < static_cast<int>(N); i++)
counterGoldCornersIds[goldCornersIds[i]]++;
const size_t diamondsN = 3;
// corners of diamonds with Vec4i indices
// Note: Values adjusted by -0.5px after fixing the systematic offset bug in charuco_detector.cpp
// The fix removes the incorrect +0.5 offset that was added after cornerSubPix
const float goldDiamondCorners[diamondsN][8] = {{195.1f,150.4f, 213.0f,200.7f, 135.9f,214.8f, 121.9f,163.0f},
{500.6f,170.8f, 501.4f,208.0f, 445.7f,199.3f, 447.3f,162.8f},
{342.9f,360.7f, 359.2f,328.2f, 400.3f,344.1f, 385.2f,377.9f}};
auto comp = [](const Vec4i& a, const Vec4i& b) {
for (int i = 0; i < 3; i++)
if (a[i] != b[i]) return a[i] < b[i];
return a[3] < b[3];
};
map<Vec4i, const float*, decltype(comp)> goldDiamonds(comp);
goldDiamonds[Vec4i(10, 4, 11, 12)] = goldDiamondCorners[0];
goldDiamonds[Vec4i(10, 3, 11, 12)] = goldDiamondCorners[1];
goldDiamonds[Vec4i(10, 2, 11, 12)] = goldDiamondCorners[2];
detector.detectDiamonds(image, diamondCorners, diamondIds, corners, ids);
map<int, int> counterRes;
ASSERT_EQ(N, ids.size());
for (size_t i = 0; i < N; i++)
{
int arucoId = ids[i];
counterRes[arucoId]++;
}
ASSERT_EQ(counterGoldCornersIds, counterRes); // check the number of ArUco markers
ASSERT_EQ(goldDiamonds.size(), diamondIds.size()); // check the number of diamonds
for (size_t i = 0; i < goldDiamonds.size(); i++)
{
Vec4i diamondId = diamondIds[i];
ASSERT_TRUE(goldDiamonds.find(diamondId) != goldDiamonds.end());
for (int j = 0; j < 4; j++)
{
EXPECT_NEAR(goldDiamonds[diamondId][j * 2], diamondCorners[i][j].x, 0.5f);
EXPECT_NEAR(goldDiamonds[diamondId][j * 2 + 1], diamondCorners[i][j].y, 0.5f);
}
}
}
}} // namespace
+205
View File
@@ -0,0 +1,205 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include "test_aruco_utils.hpp"
namespace opencv_test {
vector<Point2f> getAxis(InputArray _cameraMatrix, InputArray _distCoeffs, InputArray _rvec,
InputArray _tvec, float length, const Point2f offset) {
vector<Point3f> axis;
axis.push_back(Point3f(offset.x, offset.y, 0.f));
axis.push_back(Point3f(length+offset.x, offset.y, 0.f));
axis.push_back(Point3f(offset.x, length+offset.y, 0.f));
axis.push_back(Point3f(offset.x, offset.y, length));
vector<Point2f> axis_to_img;
projectPoints(axis, _rvec, _tvec, _cameraMatrix, _distCoeffs, axis_to_img);
return axis_to_img;
}
vector<Point2f> getMarkerById(int id, const vector<vector<Point2f> >& corners, const vector<int>& ids) {
for (size_t i = 0ull; i < ids.size(); i++)
if (ids[i] == id)
return corners[i];
return vector<Point2f>();
}
void getSyntheticRT(double yaw, double pitch, double distance, Mat& rvec, Mat& tvec) {
rvec = Mat::zeros(3, 1, CV_64FC1);
tvec = Mat::zeros(3, 1, CV_64FC1);
// rotate "scene" in pitch axis (x-axis)
Mat rotPitch(3, 1, CV_64FC1);
rotPitch.at<double>(0) = -pitch;
rotPitch.at<double>(1) = 0;
rotPitch.at<double>(2) = 0;
// rotate "scene" in yaw (y-axis)
Mat rotYaw(3, 1, CV_64FC1);
rotYaw.at<double>(0) = 0;
rotYaw.at<double>(1) = yaw;
rotYaw.at<double>(2) = 0;
// compose both rotations
composeRT(rotPitch, Mat(3, 1, CV_64FC1, Scalar::all(0)), rotYaw,
Mat(3, 1, CV_64FC1, Scalar::all(0)), rvec, tvec);
// Tvec, just move in z (camera) direction the specific distance
tvec.at<double>(0) = 0.;
tvec.at<double>(1) = 0.;
tvec.at<double>(2) = distance;
}
void projectMarker(Mat& img, const aruco::Board& board, int markerIndex, Mat cameraMatrix, Mat rvec, Mat tvec,
int markerBorder) {
// canonical image
Mat markerImg;
const int markerSizePixels = 100;
aruco::generateImageMarker(board.getDictionary(), board.getIds()[markerIndex], markerSizePixels, markerImg, markerBorder);
// projected corners
Mat distCoeffs(5, 1, CV_64FC1, Scalar::all(0));
vector<Point2f> corners;
// get max coordinate of board
Point3f maxCoord = board.getRightBottomCorner();
// copy objPoints
vector<Point3f> objPoints = board.getObjPoints()[markerIndex];
// move the marker to the origin
for (size_t i = 0; i < objPoints.size(); i++)
objPoints[i] -= (maxCoord / 2.f);
projectPoints(objPoints, rvec, tvec, cameraMatrix, distCoeffs, corners);
// get perspective transform
vector<Point2f> originalCorners;
originalCorners.push_back(Point2f(0, 0));
originalCorners.push_back(Point2f((float)markerSizePixels, 0));
originalCorners.push_back(Point2f((float)markerSizePixels, (float)markerSizePixels));
originalCorners.push_back(Point2f(0, (float)markerSizePixels));
Mat transformation = getPerspectiveTransform(originalCorners, corners);
// apply transformation
Mat aux;
const char borderValue = 127;
warpPerspective(markerImg, aux, transformation, img.size(), INTER_NEAREST, BORDER_CONSTANT,
Scalar::all(borderValue));
// copy only not-border pixels
for (int y = 0; y < aux.rows; y++) {
for (int x = 0; x < aux.cols; x++) {
if (aux.at< unsigned char >(y, x) == borderValue) continue;
img.at< unsigned char >(y, x) = aux.at< unsigned char >(y, x);
}
}
}
Mat projectBoard(const aruco::GridBoard& board, Mat cameraMatrix, double yaw, double pitch, double distance,
Size imageSize, int markerBorder) {
Mat rvec, tvec;
getSyntheticRT(yaw, pitch, distance, rvec, tvec);
Mat img = Mat(imageSize, CV_8UC1, Scalar::all(255));
for (unsigned int index = 0; index < board.getIds().size(); index++)
projectMarker(img, board, index, cameraMatrix, rvec, tvec, markerBorder);
return img;
}
/** Check if a set of 3d points are enough for calibration. Z coordinate is ignored.
* Only axis parallel lines are considered */
static bool _arePointsEnoughForPoseEstimation(const std::vector<Point3f> &points) {
if(points.size() < 4) return false;
std::vector<double> sameXValue; // different x values in points
std::vector<int> sameXCounter; // number of points with the x value in sameXValue
for(unsigned int i = 0; i < points.size(); i++) {
bool found = false;
for(unsigned int j = 0; j < sameXValue.size(); j++) {
if(sameXValue[j] == points[i].x) {
found = true;
sameXCounter[j]++;
}
}
if(!found) {
sameXValue.push_back(points[i].x);
sameXCounter.push_back(1);
}
}
// count how many x values has more than 2 points
int moreThan2 = 0;
for(unsigned int i = 0; i < sameXCounter.size(); i++) {
if(sameXCounter[i] >= 2) moreThan2++;
}
// if we have more than 1 two xvalues with more than 2 points, calibration is ok
if(moreThan2 > 1)
return true;
return false;
}
bool getCharucoBoardPose(InputArray charucoCorners, InputArray charucoIds, const aruco::CharucoBoard &board,
InputArray cameraMatrix, InputArray distCoeffs, InputOutputArray rvec, InputOutputArray tvec,
bool useExtrinsicGuess) {
CV_Assert((charucoCorners.getMat().total() == charucoIds.getMat().total()));
if(charucoIds.getMat().total() < 4) return false; // need, at least, 4 corners
std::vector<Point3f> objPoints;
objPoints.reserve(charucoIds.getMat().total());
for(unsigned int i = 0; i < charucoIds.getMat().total(); i++) {
int currId = charucoIds.getMat().at< int >(i);
CV_Assert(currId >= 0 && currId < (int)board.getChessboardCorners().size());
objPoints.push_back(board.getChessboardCorners()[currId]);
}
// points need to be in different lines, check if detected points are enough
if(!_arePointsEnoughForPoseEstimation(objPoints)) return false;
solvePnP(objPoints, charucoCorners, cameraMatrix, distCoeffs, rvec, tvec, useExtrinsicGuess);
return true;
}
/**
* @brief Return object points for the system centered in a middle (by default) or in a top left corner of single
* marker, given the marker length
*/
static Mat _getSingleMarkerObjectPoints(float markerLength, bool use_aruco_ccw_center) {
CV_Assert(markerLength > 0);
Mat objPoints(4, 1, CV_32FC3);
// set coordinate system in the top-left corner of the marker, with Z pointing out
if (use_aruco_ccw_center) {
objPoints.ptr<Vec3f>(0)[0] = Vec3f(-markerLength/2.f, markerLength/2.f, 0);
objPoints.ptr<Vec3f>(0)[1] = Vec3f(markerLength/2.f, markerLength/2.f, 0);
objPoints.ptr<Vec3f>(0)[2] = Vec3f(markerLength/2.f, -markerLength/2.f, 0);
objPoints.ptr<Vec3f>(0)[3] = Vec3f(-markerLength/2.f, -markerLength/2.f, 0);
}
else {
objPoints.ptr<Vec3f>(0)[0] = Vec3f(0.f, 0.f, 0);
objPoints.ptr<Vec3f>(0)[1] = Vec3f(markerLength, 0.f, 0);
objPoints.ptr<Vec3f>(0)[2] = Vec3f(markerLength, markerLength, 0);
objPoints.ptr<Vec3f>(0)[3] = Vec3f(0.f, markerLength, 0);
}
return objPoints;
}
void getMarkersPoses(InputArrayOfArrays corners, float markerLength, InputArray cameraMatrix, InputArray distCoeffs,
OutputArray _rvecs, OutputArray _tvecs, OutputArray objPoints,
bool use_aruco_ccw_center, SolvePnPMethod solvePnPMethod) {
CV_Assert(markerLength > 0);
Mat markerObjPoints = _getSingleMarkerObjectPoints(markerLength, use_aruco_ccw_center);
int nMarkers = (int)corners.total();
_rvecs.create(nMarkers, 1, CV_64FC3);
_tvecs.create(nMarkers, 1, CV_64FC3);
Mat rvecs = _rvecs.getMat(), tvecs = _tvecs.getMat();
for (int i = 0; i < nMarkers; i++)
solvePnP(markerObjPoints, corners.getMat(i), cameraMatrix, distCoeffs, rvecs.at<Vec3d>(i), tvecs.at<Vec3d>(i),
false, solvePnPMethod);
if(objPoints.needed())
markerObjPoints.convertTo(objPoints, -1);
}
}
@@ -0,0 +1,42 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include "opencv2/calib3d.hpp"
namespace opencv_test {
static inline double deg2rad(double deg) { return deg * CV_PI / 180.; }
vector<Point2f> getAxis(InputArray _cameraMatrix, InputArray _distCoeffs, InputArray _rvec, InputArray _tvec,
float length, const Point2f offset = Point2f(0, 0));
vector<Point2f> getMarkerById(int id, const vector<vector<Point2f> >& corners, const vector<int>& ids);
/**
* @brief Get rvec and tvec from yaw, pitch and distance
*/
void getSyntheticRT(double yaw, double pitch, double distance, Mat& rvec, Mat& tvec);
/**
* @brief Project a synthetic marker
*/
void projectMarker(Mat& img, const aruco::Board& board, int markerIndex, Mat cameraMatrix, Mat rvec, Mat tvec,
int markerBorder);
/**
* @brief Get a synthetic image of GridBoard in perspective
*/
Mat projectBoard(const aruco::GridBoard& board, Mat cameraMatrix, double yaw, double pitch, double distance,
Size imageSize, int markerBorder);
bool getCharucoBoardPose(InputArray charucoCorners, InputArray charucoIds, const aruco::CharucoBoard &board,
InputArray cameraMatrix, InputArray distCoeffs, InputOutputArray rvec,
InputOutputArray tvec, bool useExtrinsicGuess = false);
void getMarkersPoses(InputArrayOfArrays corners, float markerLength, InputArray cameraMatrix, InputArray distCoeffs,
OutputArray _rvecs, OutputArray _tvecs, OutputArray objPoints = noArray(),
bool use_aruco_ccw_center = true, SolvePnPMethod solvePnPMethod = SolvePnPMethod::SOLVEPNP_ITERATIVE);
}
File diff suppressed because it is too large Load Diff
+230
View File
@@ -0,0 +1,230 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include "opencv2/objdetect/barcode.hpp"
#include <set>
using namespace std;
namespace opencv_test{namespace{
typedef std::set<string> StringSet;
// Convert ';'-separated strings to a set
inline static StringSet toSet(const string &line)
{
StringSet res;
string::size_type it = 0, ti;
while (true)
{
ti = line.find(';', it);
if (ti == string::npos)
{
res.insert(string(line, it, line.size() - it));
break;
}
res.insert(string(line, it, ti - it));
it = ti + 1;
}
return res;
}
// Convert vector of strings to a set
inline static StringSet toSet(const vector<string> &lines)
{
StringSet res;
for (const string & line : lines)
res.insert(line);
return res;
}
// Get all keys of a map in a vector
template<typename T, typename V>
inline static vector<T> getKeys(const map<T, V> &m)
{
vector<T> res;
for (const auto & it : m)
res.push_back(it.first);
return res;
}
struct BarcodeResult
{
string type;
string data;
};
map<string, BarcodeResult> testResults {
{ "single/book.jpg", {"EAN_13", "9787115279460"} },
{ "single/bottle_1.jpg", {"EAN_13", "6922255451427"} },
{ "single/bottle_2.jpg", {"EAN_13", "6921168509256"} },
{ "multiple/4_barcodes.jpg", {"EAN_13;EAN_13;EAN_13;EAN_13", "9787564350840;9783319200064;9787118081473;9787122276124"} },
};
typedef testing::TestWithParam< string > BarcodeDetector_main;
TEST_P(BarcodeDetector_main, interface)
{
const string fname = GetParam();
const string image_path = findDataFile(string("barcode/") + fname);
const StringSet expected_lines = toSet(testResults[fname].data);
const StringSet expected_types = toSet(testResults[fname].type);
const size_t expected_count = expected_lines.size(); // assume codes are unique
// TODO: verify points location
Mat img = imread(image_path);
ASSERT_FALSE(img.empty()) << "Can't read image: " << image_path;
barcode::BarcodeDetector det;
vector<Point2f> points;
vector<string> types;
vector<string> lines;
// common interface (single)
{
bool res = det.detect(img, points);
ASSERT_TRUE(res);
EXPECT_EQ(expected_count * 4, points.size());
}
{
string res = det.decode(img, points);
ASSERT_FALSE(res.empty());
EXPECT_EQ(1u, expected_lines.count(res));
}
{
string res = det.detectAndDecode(img, points);
ASSERT_FALSE(res.empty());
EXPECT_EQ(1u, expected_lines.count(res));
EXPECT_EQ(4u, points.size());
}
// common interface (multi)
{
bool res = det.detectMulti(img, points);
ASSERT_TRUE(res);
EXPECT_EQ(expected_count * 4, points.size());
}
{
bool res = det.decodeMulti(img, points, lines);
ASSERT_TRUE(res);
EXPECT_EQ(expected_lines, toSet(lines));
}
// specific interface
{
bool res = det.decodeWithType(img, points, lines, types);
ASSERT_TRUE(res);
EXPECT_EQ(expected_types, toSet(types));
EXPECT_EQ(expected_lines, toSet(lines));
}
{
bool res = det.detectAndDecodeWithType(img, lines, types, points);
ASSERT_TRUE(res);
EXPECT_EQ(expected_types, toSet(types));
EXPECT_EQ(expected_lines, toSet(lines));
}
}
INSTANTIATE_TEST_CASE_P(/**/, BarcodeDetector_main, testing::ValuesIn(getKeys(testResults)));
TEST(BarcodeDetector_base, invalid)
{
auto bardet = barcode::BarcodeDetector();
std::vector<Point> corners;
vector<cv::String> decoded_info;
Mat zero_image = Mat::zeros(256, 256, CV_8UC1);
EXPECT_FALSE(bardet.detectMulti(zero_image, corners));
corners = std::vector<Point>(4);
EXPECT_ANY_THROW(bardet.decodeMulti(zero_image, corners, decoded_info));
}
struct ParamStruct
{
double down_thresh;
vector<float> scales;
double grad_thresh;
unsigned res_count;
};
inline static std::ostream &operator<<(std::ostream &out, const ParamStruct &p)
{
out << "(" << p.down_thresh << ", ";
for(float val : p.scales)
out << val << ", ";
out << p.grad_thresh << ")";
return out;
}
ParamStruct param_list[] = {
{ 512, {0.01f, 0.03f, 0.06f, 0.08f}, 64, 4 }, // default values -> 4 codes
{ 512, {0.01f, 0.03f, 0.06f, 0.08f}, 1024, 2 },
{ 512, {0.01f, 0.03f, 0.06f, 0.08f}, 2048, 0 },
{ 128, {0.01f, 0.03f, 0.06f, 0.08f}, 64, 3 },
{ 64, {0.01f, 0.03f, 0.06f, 0.08f}, 64, 2 },
{ 128, {0.0000001f}, 64, 1 },
{ 128, {0.0000001f, 0.0001f}, 64, 1 },
{ 128, {0.0000001f, 0.1f}, 64, 1 },
{ 512, {0.1f}, 64, 0 },
};
typedef testing::TestWithParam<ParamStruct> BarcodeDetector_parameters_tune;
TEST_P(BarcodeDetector_parameters_tune, accuracy)
{
const ParamStruct param = GetParam();
const string fname = "multiple/4_barcodes.jpg";
const string image_path = findDataFile(string("barcode/") + fname);
const Mat img = imread(image_path);
ASSERT_FALSE(img.empty()) << "Can't read image: " << image_path;
auto bardet = barcode::BarcodeDetector();
bardet.setDownsamplingThreshold(param.down_thresh);
bardet.setDetectorScales(param.scales);
bardet.setGradientThreshold(param.grad_thresh);
vector<Point2f> points;
bardet.detectMulti(img, points);
EXPECT_EQ(points.size() / 4, param.res_count);
}
INSTANTIATE_TEST_CASE_P(/**/, BarcodeDetector_parameters_tune, testing::ValuesIn(param_list));
TEST(BarcodeDetector_parameters, regression)
{
const double expected_dt = 1024, expected_gt = 256;
const vector<float> expected_ds = {0.1f};
vector<float> ds_value = {0.0f};
auto bardet = barcode::BarcodeDetector();
bardet.setDownsamplingThreshold(expected_dt).setDetectorScales(expected_ds).setGradientThreshold(expected_gt);
double dt_value = bardet.getDownsamplingThreshold();
bardet.getDetectorScales(ds_value);
double gt_value = bardet.getGradientThreshold();
EXPECT_EQ(expected_dt, dt_value);
EXPECT_EQ(expected_ds, ds_value);
EXPECT_EQ(expected_gt, gt_value);
}
TEST(BarcodeDetector_parameters, invalid)
{
auto bardet = barcode::BarcodeDetector();
EXPECT_ANY_THROW(bardet.setDownsamplingThreshold(-1));
EXPECT_ANY_THROW(bardet.setDetectorScales(vector<float> {}));
EXPECT_ANY_THROW(bardet.setDetectorScales(vector<float> {-1}));
EXPECT_ANY_THROW(bardet.setDetectorScales(vector<float> {1.5}));
EXPECT_ANY_THROW(bardet.setDetectorScales(vector<float> (17, 0.5)));
EXPECT_ANY_THROW(bardet.setGradientThreshold(-0.1));
}
}} // opencv_test::<anonymous>::
@@ -0,0 +1,615 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include "test_aruco_utils.hpp"
namespace opencv_test { namespace {
enum class ArucoAlgParams
{
USE_DEFAULT = 0,
USE_ARUCO3 = 1
};
/**
* @brief Check pose estimation of aruco board
*/
class CV_ArucoBoardPose : public cvtest::BaseTest {
public:
CV_ArucoBoardPose(ArucoAlgParams arucoAlgParams)
{
aruco::DetectorParameters params;
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(aruco::DICT_6X6_250);
params.minDistanceToBorder = 3;
if (arucoAlgParams == ArucoAlgParams::USE_ARUCO3) {
params.useAruco3Detection = true;
params.cornerRefinementMethod = (int)aruco::CORNER_REFINE_SUBPIX;
params.minSideLengthCanonicalImg = 16;
params.errorCorrectionRate = 0.8;
}
detector = aruco::ArucoDetector(dictionary, params);
}
protected:
aruco::ArucoDetector detector;
void run(int);
};
void CV_ArucoBoardPose::run(int) {
int iter = 0;
Mat cameraMatrix = Mat::eye(3, 3, CV_64FC1);
Size imgSize(500, 500);
cameraMatrix.at< double >(0, 0) = cameraMatrix.at< double >(1, 1) = 650;
cameraMatrix.at< double >(0, 2) = imgSize.width / 2;
cameraMatrix.at< double >(1, 2) = imgSize.height / 2;
Mat distCoeffs(5, 1, CV_64FC1, Scalar::all(0));
const int sizeX = 3, sizeY = 3;
aruco::DetectorParameters detectorParameters = detector.getDetectorParameters();
// for different perspectives
for(double distance : {0.2, 0.35}) {
for(int yaw = -55; yaw <= 50; yaw += 25) {
for(int pitch = -55; pitch <= 50; pitch += 25) {
vector<int> tmpIds;
for(int i = 0; i < sizeX*sizeY; i++)
tmpIds.push_back((iter + int(i)) % 250);
aruco::GridBoard gridboard(Size(sizeX, sizeY), 0.02f, 0.005f, detector.getDictionary(), tmpIds);
int markerBorder = iter % 2 + 1;
iter++;
// create synthetic image
Mat img = projectBoard(gridboard, cameraMatrix, deg2rad(yaw), deg2rad(pitch), distance,
imgSize, markerBorder);
vector<vector<Point2f> > corners;
vector<int> ids;
detectorParameters.markerBorderBits = markerBorder;
detectorParameters.validBitIdThreshold = 0.5f;
detector.setDetectorParameters(detectorParameters);
detector.detectMarkers(img, corners, ids);
ASSERT_EQ(ids.size(), gridboard.getIds().size());
// estimate pose
Mat rvec, tvec;
{
Mat objPoints, imgPoints; // get object and image points for the solvePnP function
gridboard.matchImagePoints(corners, ids, objPoints, imgPoints);
solvePnP(objPoints, imgPoints, cameraMatrix, distCoeffs, rvec, tvec);
}
// check axes
vector<Point2f> axes = getAxis(cameraMatrix, distCoeffs, rvec, tvec, gridboard.getRightBottomCorner().x);
vector<Point2f> topLeft = getMarkerById(gridboard.getIds()[0], corners, ids);
ASSERT_NEAR(topLeft[0].x, axes[0].x, 2.f);
ASSERT_NEAR(topLeft[0].y, axes[0].y, 2.f);
vector<Point2f> topRight = getMarkerById(gridboard.getIds()[2], corners, ids);
ASSERT_NEAR(topRight[1].x, axes[1].x, 2.f);
ASSERT_NEAR(topRight[1].y, axes[1].y, 2.f);
vector<Point2f> bottomLeft = getMarkerById(gridboard.getIds()[6], corners, ids);
ASSERT_NEAR(bottomLeft[3].x, axes[2].x, 2.f);
ASSERT_NEAR(bottomLeft[3].y, axes[2].y, 2.f);
// check estimate result
for(unsigned int i = 0; i < ids.size(); i++) {
int foundIdx = -1;
for(unsigned int j = 0; j < gridboard.getIds().size(); j++) {
if(gridboard.getIds()[j] == ids[i]) {
foundIdx = int(j);
break;
}
}
if(foundIdx == -1) {
ts->printf(cvtest::TS::LOG, "Marker detected with wrong ID in Board test");
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
return;
}
vector< Point2f > projectedCorners;
projectPoints(gridboard.getObjPoints()[foundIdx], rvec, tvec, cameraMatrix, distCoeffs,
projectedCorners);
for(int c = 0; c < 4; c++) {
double repError = cv::norm(projectedCorners[c] - corners[i][c]); // TODO cvtest
if(repError > 5.) {
ts->printf(cvtest::TS::LOG, "Corner reprojection error too high");
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
return;
}
}
}
}
}
}
}
/**
* @brief Check refine strategy
*/
class CV_ArucoRefine : public cvtest::BaseTest {
public:
CV_ArucoRefine(ArucoAlgParams arucoAlgParams)
{
vector<aruco::Dictionary> dictionaries = {aruco::getPredefinedDictionary(aruco::DICT_6X6_250),
aruco::getPredefinedDictionary(aruco::DICT_5X5_250),
aruco::getPredefinedDictionary(aruco::DICT_4X4_250),
aruco::getPredefinedDictionary(aruco::DICT_7X7_250)};
aruco::DetectorParameters params;
params.minDistanceToBorder = 3;
params.cornerRefinementMethod = (int)aruco::CORNER_REFINE_SUBPIX;
if (arucoAlgParams == ArucoAlgParams::USE_ARUCO3)
params.useAruco3Detection = true;
aruco::RefineParameters refineParams(10.f, 3.f, true);
detector = aruco::ArucoDetector(dictionaries, params, refineParams);
}
protected:
aruco::ArucoDetector detector;
void run(int);
};
void CV_ArucoRefine::run(int) {
int iter = 0;
Mat cameraMatrix = Mat::eye(3, 3, CV_64FC1);
Size imgSize(500, 500);
cameraMatrix.at< double >(0, 0) = cameraMatrix.at< double >(1, 1) = 650;
cameraMatrix.at< double >(0, 2) = imgSize.width / 2;
cameraMatrix.at< double >(1, 2) = imgSize.height / 2;
Mat distCoeffs(5, 1, CV_64FC1, Scalar::all(0));
aruco::DetectorParameters detectorParameters = detector.getDetectorParameters();
// for different perspectives
for(double distance : {0.2, 0.4}) {
for(int yaw = -60; yaw < 60; yaw += 30) {
for(int pitch = -60; pitch <= 60; pitch += 30) {
aruco::GridBoard gridboard(Size(3, 3), 0.02f, 0.005f, detector.getDictionary());
int markerBorder = iter % 2 + 1;
iter++;
// create synthetic image
Mat img = projectBoard(gridboard, cameraMatrix, deg2rad(yaw), deg2rad(pitch), distance,
imgSize, markerBorder);
// detect markers
vector<vector<Point2f> > corners, rejected;
vector<int> ids;
detectorParameters.markerBorderBits = markerBorder;
detector.setDetectorParameters(detectorParameters);
detector.detectMarkers(img, corners, ids, rejected);
// remove a marker from detection
int markersBeforeDelete = (int)ids.size();
if(markersBeforeDelete < 2) continue;
rejected.push_back(corners[0]);
corners.erase(corners.begin(), corners.begin() + 1);
ids.erase(ids.begin(), ids.begin() + 1);
// try to refind the erased marker
detector.refineDetectedMarkers(img, gridboard, corners, ids, rejected, cameraMatrix,
distCoeffs, noArray());
// check result
if((int)ids.size() < markersBeforeDelete) {
ts->printf(cvtest::TS::LOG, "Error in refine detected markers");
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
return;
}
}
}
}
}
// Find the position of a given marker id in the detection results, or -1 if absent.
static int findMarkerIndex(const vector<int>& ids, int markerId) {
for(size_t i = 0; i < ids.size(); i++) {
if(ids[i] == markerId)
return (int)i;
}
return -1;
}
// Warp a marker image onto an arbitrary quad in the scene and paint it over the
// background. A neutral grey (127) is used as the "background", the marker
// only contains black/white pixels, so everything that stays 127 after the warp
// is background and is left untouched.
static void drawMarkerAtCorners(Mat& image, const Mat& marker, const vector<Point2f>& corners) {
vector<Point2f> originalCorners = {
Point2f(0.f, 0.f),
Point2f((float)marker.cols - 1.f, 0.f),
Point2f((float)marker.cols - 1.f, (float)marker.rows - 1.f),
Point2f(0.f, (float)marker.rows - 1.f)
};
Mat transformation = getPerspectiveTransform(originalCorners, corners);
Mat warped(image.size(), image.type(), Scalar::all(127));
warpPerspective(marker, warped, transformation, image.size(), INTER_NEAREST, BORDER_CONSTANT, Scalar::all(127));
Mat mask = warped != 127;
warped.copyTo(image, mask);
}
// Degrade the marker image: find its first black inner cell and partially fill it with white so
// that the cell's white-pixel ratio becomes ~whiteRatio. This lets the test control how far a
// single cell drifts from its ground-truth bit, which is what validBitIdThreshold gates.
static bool setFirstBlackInnerCellWhiteRatio(Mat& marker, const aruco::Dictionary& dictionary,
int markerId, int markerBorderBits, float whiteRatio) {
const int markerSizeWithBorders = dictionary.markerSize + 2 * markerBorderBits;
const int cellSize = marker.rows / markerSizeWithBorders;
if(marker.cols != marker.rows || cellSize * markerSizeWithBorders != marker.rows)
return false;
Mat markerBits = dictionary.getMarkerBits(markerId);
for(int y = 0; y < dictionary.markerSize; y++) {
for(int x = 0; x < dictionary.markerSize; x++) {
if(markerBits.ptr<float>(y)[x] != 0.f)
continue; // skip white cells
Rect cell((x + markerBorderBits) * cellSize, (y + markerBorderBits) * cellSize,
cellSize, cellSize);
marker(cell).setTo(Scalar::all(0));
// A centred white square of side sqrt(whiteRatio)*cellSize covers ~whiteRatio of the cell.
int whiteSide = cvRound(cellSize * std::sqrt(whiteRatio));
whiteSide = std::max(1, std::min(cellSize, whiteSide));
const int offset = (cellSize - whiteSide) / 2;
marker(Rect(cell.x + offset, cell.y + offset, whiteSide, whiteSide)).setTo(Scalar::all(255));
return true;
}
}
return false;
}
// Drop a marker from the detection results and move its corners to the rejected list, so that
// refineDetectedMarkers() has a rejected candidate to try to recover.
static bool removeMarkerAndMakeRejected(int markerId, vector<vector<Point2f>>& corners,
vector<int>& ids, vector<vector<Point2f>>& rejected) {
const int markerIndex = findMarkerIndex(ids, markerId);
if(markerIndex < 0)
return false;
rejected.clear();
rejected.push_back(corners[(size_t)markerIndex]);
corners.erase(corners.begin() + markerIndex);
ids.erase(ids.begin() + markerIndex);
return true;
}
// Render a flat board image and detect its markers.
// Returns true only when every board marker was found.
static bool generateBoardForRefine(const aruco::GridBoard& board, int markerBorderBits,
Mat& image, const aruco::ArucoDetector& detector,
vector<vector<Point2f>>& corners, vector<int>& ids) {
board.generateImage(Size(760, 760), image, 50, markerBorderBits);
vector<vector<Point2f>> rejected;
detector.detectMarkers(image, corners, ids, rejected);
return board.getIds().size() == ids.size();
}
TEST(CV_ArucoBoardPose, accuracy) {
CV_ArucoBoardPose test(ArucoAlgParams::USE_DEFAULT);
test.safe_run();
}
typedef CV_ArucoBoardPose CV_Aruco3BoardPose;
TEST(CV_Aruco3BoardPose, accuracy) {
CV_Aruco3BoardPose test(ArucoAlgParams::USE_ARUCO3);
test.safe_run();
}
typedef CV_ArucoRefine CV_Aruco3Refine;
TEST(CV_ArucoRefine, accuracy) {
CV_ArucoRefine test(ArucoAlgParams::USE_DEFAULT);
test.safe_run();
}
TEST(CV_Aruco3Refine, accuracy) {
CV_Aruco3Refine test(ArucoAlgParams::USE_ARUCO3);
test.safe_run();
}
// refineDetectedMarkers() must use detectorParams.validBitIdThreshold when matching a rejected
// candidate's cell ratios against the expected marker code. Both cases below refine the very same
// image: a board whose dropped marker 0 is redrawn with one black cell brightened to a 0.6 white
// ratio and differ only in the threshold: the strict default (0.49) treats that cell as a bit
// error and leaves the marker rejected, while a relaxed 0.7 tolerates the deviation and recovers it.
class CV_ArucoRefineValidBitIdThreshold : public testing::Test {
protected:
void SetUp() override {
const int markerBorderBits = 1;
const int markerSidePixels = 300;
dictionary = aruco::getPredefinedDictionary(aruco::DICT_4X4_50);
board = aruco::GridBoard(Size(2, 2), 1.f, 0.2f, dictionary);
detectorParameters.markerBorderBits = markerBorderBits;
detectorParameters.perspectiveRemovePixelPerCell = 20;
detectorParameters.perspectiveRemoveIgnoredMarginPerCell = 0.;
const aruco::ArucoDetector detector(dictionary, detectorParameters, refineParameters);
// Start from a fully detected board (clean markers, so the threshold is irrelevant here).
ASSERT_TRUE(generateBoardForRefine(board, markerBorderBits, image, detector, corners, ids));
// Drop marker 0 so it becomes a rejected candidate for refinement.
ASSERT_TRUE(removeMarkerAndMakeRejected(markerId, corners, ids, rejected));
// Draw a degraded version of marker 0 (one black cell at 0.6 white ratio) at its location.
Mat marker;
dictionary.generateImageMarker(markerId, markerSidePixels, marker, markerBorderBits);
ASSERT_TRUE(setFirstBlackInnerCellWhiteRatio(marker, dictionary, markerId, markerBorderBits, 0.6f));
drawMarkerAtCorners(image, marker, rejected[0]);
}
// Refine the shared image with a given threshold and report whether marker 0 was recovered.
// refineDetectedMarkers() mutates its inputs, so each attempt runs on its own copy.
bool isMarkerRecovered(float validBitIdThreshold) const {
aruco::DetectorParameters attemptParameters = detectorParameters;
attemptParameters.validBitIdThreshold = validBitIdThreshold;
const aruco::ArucoDetector attemptDetector(dictionary, attemptParameters, refineParameters);
vector<vector<Point2f>> attemptCorners = corners;
vector<int> attemptIds = ids;
vector<vector<Point2f>> attemptRejected = rejected;
attemptDetector.refineDetectedMarkers(image, board, attemptCorners, attemptIds, attemptRejected);
return findMarkerIndex(attemptIds, markerId) >= 0;
}
const int markerId = 0;
aruco::Dictionary dictionary;
aruco::GridBoard board;
aruco::DetectorParameters detectorParameters;
aruco::RefineParameters refineParameters{10.f, 1.f, true};
Mat image;
vector<vector<Point2f>> corners;
vector<int> ids;
vector<vector<Point2f>> rejected;
};
// Strict threshold: the 0.6 white cell is treated as a bit error, so the marker is not recovered.
TEST_F(CV_ArucoRefineValidBitIdThreshold, strictThresholdKeepsMarkerRejected) {
EXPECT_FALSE(isMarkerRecovered(0.49f));
}
// Relaxed threshold: the deviation is tolerated, so the marker is recovered.
TEST_F(CV_ArucoRefineValidBitIdThreshold, relaxedThresholdRecoversMarker) {
EXPECT_TRUE(isMarkerRecovered(0.7f));
}
TEST(CV_ArucoBoardPose, CheckNegativeZ)
{
double matrixData[9] = { -3.9062571886921410e+02, 0., 4.2350000000000000e+02,
0., 3.9062571886921410e+02, 2.3950000000000000e+02,
0., 0., 1 };
cv::Mat cameraMatrix = cv::Mat(3, 3, CV_64F, matrixData);
vector<cv::Point3f> pts3d1, pts3d2;
pts3d1.push_back(cv::Point3f(0.326198f, -0.030621f, 0.303620f));
pts3d1.push_back(cv::Point3f(0.325340f, -0.100594f, 0.301862f));
pts3d1.push_back(cv::Point3f(0.255859f, -0.099530f, 0.293416f));
pts3d1.push_back(cv::Point3f(0.256717f, -0.029557f, 0.295174f));
pts3d2.push_back(cv::Point3f(-0.033144f, -0.034819f, 0.245216f));
pts3d2.push_back(cv::Point3f(-0.035507f, -0.104705f, 0.241987f));
pts3d2.push_back(cv::Point3f(-0.105289f, -0.102120f, 0.237120f));
pts3d2.push_back(cv::Point3f(-0.102926f, -0.032235f, 0.240349f));
vector<int> tmpIds = {0, 1};
vector<vector<Point3f> > tmpObjectPoints = {pts3d1, pts3d2};
aruco::Board board(tmpObjectPoints, aruco::getPredefinedDictionary(0), tmpIds);
vector<vector<Point2f> > corners;
vector<Point2f> pts2d;
pts2d.push_back(cv::Point2f(37.7f, 203.3f));
pts2d.push_back(cv::Point2f(38.5f, 120.5f));
pts2d.push_back(cv::Point2f(105.5f, 115.8f));
pts2d.push_back(cv::Point2f(104.2f, 202.7f));
corners.push_back(pts2d);
pts2d.clear();
pts2d.push_back(cv::Point2f(476.0f, 184.2f));
pts2d.push_back(cv::Point2f(479.6f, 73.8f));
pts2d.push_back(cv::Point2f(590.9f, 77.0f));
pts2d.push_back(cv::Point2f(587.5f, 188.1f));
corners.push_back(pts2d);
Vec3d rvec, tvec;
int nUsed = 0;
{
Mat objPoints, imgPoints; // get object and image points for the solvePnP function
board.matchImagePoints(corners, board.getIds(), objPoints, imgPoints);
nUsed = (int)objPoints.total()/4;
solvePnP(objPoints, imgPoints, cameraMatrix, Mat(), rvec, tvec);
}
ASSERT_EQ(nUsed, 2);
cv::Matx33d rotm; cv::Point3d out;
cv::Rodrigues(rvec, rotm);
out = cv::Point3d(tvec) + rotm*Point3d(board.getObjPoints()[0][0]);
ASSERT_GT(out.z, 0);
corners.clear(); pts2d.clear();
pts2d.push_back(cv::Point2f(38.4f, 204.5f));
pts2d.push_back(cv::Point2f(40.0f, 124.7f));
pts2d.push_back(cv::Point2f(102.0f, 119.1f));
pts2d.push_back(cv::Point2f(99.9f, 203.6f));
corners.push_back(pts2d);
pts2d.clear();
pts2d.push_back(cv::Point2f(476.0f, 184.3f));
pts2d.push_back(cv::Point2f(479.2f, 75.1f));
pts2d.push_back(cv::Point2f(588.7f, 79.2f));
pts2d.push_back(cv::Point2f(586.3f, 188.5f));
corners.push_back(pts2d);
nUsed = 0;
{
Mat objPoints, imgPoints; // get object and image points for the solvePnP function
board.matchImagePoints(corners, board.getIds(), objPoints, imgPoints);
nUsed = (int)objPoints.total()/4;
solvePnP(objPoints, imgPoints, cameraMatrix, Mat(), rvec, tvec, true);
}
ASSERT_EQ(nUsed, 2);
cv::Rodrigues(rvec, rotm);
out = cv::Point3d(tvec) + rotm*Point3d(board.getObjPoints()[0][0]);
ASSERT_GT(out.z, 0);
}
TEST(CV_ArucoGenerateBoard, regression_1226) {
int bwidth = 1600;
int bheight = 1200;
cv::aruco::Dictionary dict = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);
cv::aruco::CharucoBoard board(Size(7, 5), 1.0, 0.75, dict);
cv::Size sz(bwidth, bheight);
cv::Mat mat;
ASSERT_NO_THROW(
{
board.generateImage(sz, mat, 0, 1);
});
}
TEST(CV_ArucoDictionary, extendDictionary) {
aruco::Dictionary base_dictionary = aruco::getPredefinedDictionary(aruco::DICT_4X4_250);
aruco::Dictionary custom_dictionary = aruco::extendDictionary(150, 4, base_dictionary);
ASSERT_EQ(custom_dictionary.bytesList.rows, 150);
ASSERT_EQ(cv::norm(custom_dictionary.bytesList, base_dictionary.bytesList.rowRange(0, 150)), 0.);
}
// Unit-test both getDistanceToId() overloads on a known marker: the existing bit-based overload
// must keep its exact Hamming behaviour, and the new ratio-based overload must count a cell as an
// error only when it deviates from the expected bit by more than validBitIdThreshold.
TEST(CV_ArucoDictionary, getDistanceToIdCellPixelRatio) {
const int markerId = 0;
const float validBitIdThreshold = 0.49f;
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(aruco::DICT_4X4_50);
// Bit overload: the exact marker bits are at distance 0 from their own id.
Mat bits = aruco::Dictionary::getBitsFromByteList(dictionary.bytesList.rowRange(markerId, markerId + 1),
dictionary.markerSize);
EXPECT_EQ(0, dictionary.getDistanceToId(bits, markerId, false));
// Bit overload: flipping a single bit yields a Hamming distance of exactly 1.
Mat erroneousBits = bits.clone();
erroneousBits.ptr<uchar>(0)[0] = (uchar)!erroneousBits.ptr<uchar>(0)[0];
EXPECT_EQ(1, dictionary.getDistanceToId(erroneousBits, markerId, false));
// Ground-truth bit values (0.f or 1.f) for the ratio overload checks below.
Mat markerRatio = dictionary.getMarkerBits(markerId);
const float expectedBit = markerRatio.ptr<float>(0)[0];
// Ratio overload: a 0.4 drift toward the wrong value stays within the 0.49 tolerance -> no error.
Mat acceptedRatio = markerRatio.clone();
acceptedRatio.ptr<float>(0)[0] = expectedBit > 0.5f ? 0.6f : 0.4f;
EXPECT_EQ(0, dictionary.getDistanceToId(acceptedRatio, markerId, false, validBitIdThreshold));
// Ratio overload: a 0.6 drift exceeds the 0.49 tolerance -> the cell counts as one error.
Mat rejectedRatio = markerRatio.clone();
rejectedRatio.ptr<float>(0)[0] = expectedBit > 0.5f ? 0.4f : 0.6f;
EXPECT_EQ(1, dictionary.getDistanceToId(rejectedRatio, markerId, false, validBitIdThreshold));
}
// 5x5 markers leave one meaningful bit in the final packed byte. Flip only that cell
// far enough from its expected value and verify that the ratio distance counts it.
TEST(CV_ArucoDictionary, getDistanceToIdCellPixelRatioPartialByte) {
const int markerId = 15;
const float validBitIdThreshold = 0.49f;
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(aruco::DICT_5X5_50);
Mat markerRatio = dictionary.getMarkerBits(markerId);
EXPECT_EQ(0, dictionary.getDistanceToId(markerRatio, markerId, false, validBitIdThreshold));
Mat rotatedMarkerRatio = dictionary.getMarkerBits(markerId, 1);
EXPECT_EQ(0, dictionary.getDistanceToId(rotatedMarkerRatio, markerId, true, validBitIdThreshold));
Mat rejectedRatio = markerRatio.clone();
float& lastCellRatio = rejectedRatio.ptr<float>(dictionary.markerSize - 1)[dictionary.markerSize - 1];
lastCellRatio = lastCellRatio > 0.5f ? 0.4f : 0.6f;
EXPECT_EQ(1, dictionary.getDistanceToId(rejectedRatio, markerId, false, validBitIdThreshold));
}
TEST(CV_ArucoDictionary, identifyBitMask) {
const int markerId = 7;
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(aruco::DICT_4X4_50);
// Start with a 0/1 bit matrix for the marker and confirm that the bit-based
// identify overload handles it without any ratio threshold input.
Mat bits = aruco::Dictionary::getBitsFromByteList(dictionary.bytesList.rowRange(markerId, markerId + 1),
dictionary.markerSize);
int idx = -1;
int rotation = -1;
ASSERT_TRUE(dictionary.identify(bits, idx, rotation, 0.0));
EXPECT_EQ(markerId, idx);
EXPECT_EQ(0, rotation);
// OpenCV comparisons produce masks with values 0 and 255, not 0 and 1. The raw-bit
// identify overload must normalize those masks before delegating to the ratio path.
Mat bitMask;
bits.convertTo(bitMask, CV_8U, 255.0);
idx = -1;
rotation = -1;
ASSERT_TRUE(dictionary.identify(bitMask, idx, rotation, 0.0));
EXPECT_EQ(markerId, idx);
EXPECT_EQ(0, rotation);
}
TEST(CV_ArucoBoardGenerateImage_RotationTest, HandlesRotatedMarkersWithoutBoundingBoxError)
{
using namespace cv;
using namespace cv::aruco;
Dictionary dict = getPredefinedDictionary(DICT_4X4_50);
DetectorParameters detectorParams;
ArucoDetector detector(dict, detectorParams);
std::vector<float> angles = {0.0f, 45.0f, 90.0f, 135.0f};
for (auto angle_deg : angles)
{
float angle_rad = angle_deg * static_cast<float>(CV_PI) / 180.0f;
float c = cos(angle_rad);
float s = sin(angle_rad);
std::vector<Point3f> markerCorners(4);
markerCorners[0] = Point3f(0.f, 0.f, 0.f);
markerCorners[1] = Point3f(1.f, 0.f, 0.f);
markerCorners[2] = Point3f(1.f, 1.f, 0.f);
markerCorners[3] = Point3f(0.f, 1.f, 0.f);
for (auto &p : markerCorners)
{
float xNew = p.x * c - p.y * s;
float yNew = p.x * s + p.y * c;
p.x = xNew;
p.y = yNew;
}
std::vector<std::vector<Point3f>> allObjPoints{markerCorners};
std::vector<int> ids{0};
Board board(allObjPoints, dict, ids);
float markerSize = 1.0f;
float rotatedSize = markerSize * std::sqrt(2.0f);
int borderBits = 1;
int marginSize = 20;
int sidePixels = static_cast<int>((rotatedSize + 2.0f * borderBits) * 500) + 2 * marginSize;
Mat outImg;
Size outSize(sidePixels, sidePixels);
ASSERT_NO_THROW(board.generateImage(outSize, outImg, marginSize, borderBits))
<< "board.generateImage() threw an exception at angle " << angle_deg;
std::vector<int> detectedIds;
std::vector<std::vector<Point2f>> detectedCorners;
detector.detectMarkers(outImg, detectedCorners, detectedIds);
ASSERT_EQ(detectedIds.size(), (size_t)1)
<< "Failed to detect single marker at angle: " << angle_deg;
EXPECT_EQ(detectedIds[0], 0)
<< "Marker ID mismatch at angle: " << angle_deg;
}
}
}} // namespace
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,976 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include "test_aruco_utils.hpp"
namespace opencv_test { namespace {
/**
* @brief Get a synthetic image of Chessboard in perspective
*/
static Mat projectChessboard(int squaresX, int squaresY, float squareSize, Size imageSize,
Mat cameraMatrix, Mat rvec, Mat tvec, bool legacyPattern) {
Mat img(imageSize, CV_8UC1, Scalar::all(255));
Mat distCoeffs(5, 1, CV_64FC1, Scalar::all(0));
for(int y = 0; y < squaresY; y++) {
float startY = float(y) * squareSize;
for(int x = 0; x < squaresX; x++) {
if(legacyPattern && (squaresY % 2 == 0)) {
if((y + 1) % 2 != x % 2) continue;
} else {
if(y % 2 != x % 2) continue;
}
float startX = float(x) * squareSize;
vector< Point3f > squareCorners;
squareCorners.push_back(Point3f(startX, startY, 0) - Point3f(squaresX*squareSize/2.f, squaresY*squareSize/2.f, 0.f));
squareCorners.push_back(squareCorners[0] + Point3f(squareSize, 0, 0));
squareCorners.push_back(squareCorners[0] + Point3f(squareSize, squareSize, 0));
squareCorners.push_back(squareCorners[0] + Point3f(0, squareSize, 0));
vector< vector< Point2f > > projectedCorners;
projectedCorners.push_back(vector< Point2f >());
projectPoints(squareCorners, rvec, tvec, cameraMatrix, distCoeffs, projectedCorners[0]);
vector< vector< Point > > projectedCornersInt;
projectedCornersInt.push_back(vector< Point >());
for(int k = 0; k < 4; k++)
projectedCornersInt[0]
.push_back(Point((int)projectedCorners[0][k].x, (int)projectedCorners[0][k].y));
fillPoly(img, projectedCornersInt, Scalar::all(0));
}
}
return img;
}
/**
* @brief Check pose estimation of charuco board
*/
static Mat projectCharucoBoard(aruco::CharucoBoard& board, Mat cameraMatrix, double yaw,
double pitch, double distance, Size imageSize, int markerBorder,
Mat &rvec, Mat &tvec) {
getSyntheticRT(yaw, pitch, distance, rvec, tvec);
// project markers
Mat img = Mat(imageSize, CV_8UC1, Scalar::all(255));
for(unsigned int indexMarker = 0; indexMarker < board.getIds().size(); indexMarker++) {
projectMarker(img, board, indexMarker, cameraMatrix, rvec, tvec, markerBorder);
}
// project chessboard
Mat chessboard =
projectChessboard(board.getChessboardSize().width, board.getChessboardSize().height,
board.getSquareLength(), imageSize, cameraMatrix, rvec, tvec, board.getLegacyPattern());
for(unsigned int i = 0; i < chessboard.total(); i++) {
if(chessboard.ptr< unsigned char >()[i] == 0) {
img.ptr< unsigned char >()[i] = 0;
}
}
return img;
}
static bool borderPixelsHaveSameColor(const Mat& image, uint8_t color) {
for (int j = 0; j < image.cols; j++) {
if (image.at<uint8_t>(0, j) != color || image.at<uint8_t>(image.rows-1, j) != color)
return false;
}
for (int i = 0; i < image.rows; i++) {
if (image.at<uint8_t>(i, 0) != color || image.at<uint8_t>(i, image.cols-1) != color)
return false;
}
return true;
}
/**
* @brief Check Charuco detection
*/
class CV_CharucoDetection : public cvtest::BaseTest {
public:
CV_CharucoDetection(bool _legacyPattern) : legacyPattern(_legacyPattern) {}
protected:
void run(int);
bool legacyPattern;
};
void CV_CharucoDetection::run(int) {
int iter = 0;
Mat cameraMatrix = Mat::eye(3, 3, CV_64FC1);
Size imgSize(500, 500);
aruco::DetectorParameters params;
params.minDistanceToBorder = 3;
aruco::CharucoBoard board(Size(4, 4), 0.03f, 0.015f, aruco::getPredefinedDictionary(aruco::DICT_6X6_250));
board.setLegacyPattern(legacyPattern);
aruco::CharucoDetector detector(board, aruco::CharucoParameters(), params);
cameraMatrix.at<double>(0, 0) = cameraMatrix.at<double>(1, 1) = 600;
cameraMatrix.at<double>(0, 2) = imgSize.width / 2;
cameraMatrix.at<double>(1, 2) = imgSize.height / 2;
Mat distCoeffs(5, 1, CV_64FC1, Scalar::all(0));
// for different perspectives
for(double distance : {0.2, 0.4}) {
for(int yaw = -55; yaw <= 50; yaw += 25) {
for(int pitch = -55; pitch <= 50; pitch += 25) {
int markerBorder = iter % 2 + 1;
iter++;
// create synthetic image
Mat rvec, tvec;
Mat img = projectCharucoBoard(board, cameraMatrix, deg2rad(yaw), deg2rad(pitch),
distance, imgSize, markerBorder, rvec, tvec);
// detect markers and interpolate charuco corners
vector<vector<Point2f> > corners;
vector<Point2f> charucoCorners;
vector<int> ids, charucoIds;
params.markerBorderBits = markerBorder;
detector.setDetectorParameters(params);
//detector.detectMarkers(img, corners, ids);
if(iter % 2 == 0) {
detector.detectBoard(img, charucoCorners, charucoIds, corners, ids);
} else {
aruco::CharucoParameters charucoParameters;
charucoParameters.cameraMatrix = cameraMatrix;
charucoParameters.distCoeffs = distCoeffs;
detector.setCharucoParameters(charucoParameters);
detector.detectBoard(img, charucoCorners, charucoIds, corners, ids);
}
ASSERT_GT(ids.size(), std::vector< int >::size_type(0)) << "Marker detection failed";
// check results
vector< Point2f > projectedCharucoCorners;
// copy chessboardCorners
vector<Point3f> copyChessboardCorners = board.getChessboardCorners();
// move copyChessboardCorners points
for (size_t i = 0; i < copyChessboardCorners.size(); i++)
copyChessboardCorners[i] -= board.getRightBottomCorner() / 2.f;
projectPoints(copyChessboardCorners, rvec, tvec, cameraMatrix, distCoeffs,
projectedCharucoCorners);
for(unsigned int i = 0; i < charucoIds.size(); i++) {
int currentId = charucoIds[i];
ASSERT_LT(currentId, (int)board.getChessboardCorners().size()) << "Invalid Charuco corner id";
double repError = cv::norm(charucoCorners[i] - projectedCharucoCorners[currentId]); // TODO cvtest
ASSERT_LE(repError, 5.) << "Charuco corner reprojection error too high";
}
}
}
}
}
/**
* @brief Check charuco pose estimation
*/
class CV_CharucoPoseEstimation : public cvtest::BaseTest {
public:
CV_CharucoPoseEstimation(bool _legacyPattern) : legacyPattern(_legacyPattern) {}
protected:
void run(int);
bool legacyPattern;
};
void CV_CharucoPoseEstimation::run(int) {
int iter = 0;
Mat cameraMatrix = Mat::eye(3, 3, CV_64FC1);
Size imgSize(750, 750);
aruco::DetectorParameters params;
params.minDistanceToBorder = 3;
aruco::CharucoBoard board(Size(4, 4), 0.03f, 0.015f, aruco::getPredefinedDictionary(aruco::DICT_6X6_250));
board.setLegacyPattern(legacyPattern);
aruco::CharucoDetector detector(board, aruco::CharucoParameters(), params);
cameraMatrix.at<double>(0, 0) = cameraMatrix.at< double >(1, 1) = 1000;
cameraMatrix.at<double>(0, 2) = imgSize.width / 2;
cameraMatrix.at<double>(1, 2) = imgSize.height / 2;
Mat distCoeffs(5, 1, CV_64FC1, Scalar::all(0));
// for different perspectives
for(double distance : {0.2, 0.25}) {
for(int yaw = -55; yaw <= 50; yaw += 25) {
for(int pitch = -55; pitch <= 50; pitch += 25) {
int markerBorder = iter % 2 + 1;
iter++;
// get synthetic image
Mat rvec, tvec;
Mat img = projectCharucoBoard(board, cameraMatrix, deg2rad(yaw), deg2rad(pitch),
distance, imgSize, markerBorder, rvec, tvec);
// detect markers
vector<vector<Point2f> > corners;
vector<int> ids;
params.markerBorderBits = markerBorder;
detector.setDetectorParameters(params);
// detect markers and interpolate charuco corners
vector<Point2f> charucoCorners;
vector<int> charucoIds;
if(iter % 2 == 0) {
detector.detectBoard(img, charucoCorners, charucoIds, corners, ids);
} else {
aruco::CharucoParameters charucoParameters;
charucoParameters.cameraMatrix = cameraMatrix;
charucoParameters.distCoeffs = distCoeffs;
detector.setCharucoParameters(charucoParameters);
detector.detectBoard(img, charucoCorners, charucoIds, corners, ids);
}
ASSERT_EQ(ids.size(), board.getIds().size());
if(charucoIds.size() == 0) continue;
// estimate charuco pose
getCharucoBoardPose(charucoCorners, charucoIds, board, cameraMatrix, distCoeffs, rvec, tvec);
// check axes
const float aruco_offset = (board.getSquareLength() - board.getMarkerLength()) / 2.f;
Point2f offset;
vector<Point2f> topLeft, bottomLeft;
if(legacyPattern) { // white box in upper left corner for even row count chessboard patterns
offset = Point2f(aruco_offset + board.getSquareLength(), aruco_offset);
topLeft = getMarkerById(board.getIds()[1], corners, ids);
bottomLeft = getMarkerById(board.getIds()[2], corners, ids);
} else { // always a black box in the upper left corner
offset = Point2f(aruco_offset, aruco_offset);
topLeft = getMarkerById(board.getIds()[0], corners, ids);
bottomLeft = getMarkerById(board.getIds()[2], corners, ids);
}
vector<Point2f> axes = getAxis(cameraMatrix, distCoeffs, rvec, tvec, board.getSquareLength(), offset);
ASSERT_NEAR(topLeft[0].x, axes[1].x, 3.f);
ASSERT_NEAR(topLeft[0].y, axes[1].y, 3.f);
ASSERT_NEAR(bottomLeft[0].x, axes[2].x, 3.f);
ASSERT_NEAR(bottomLeft[0].y, axes[2].y, 3.f);
// check estimate result
vector< Point2f > projectedCharucoCorners;
projectPoints(board.getChessboardCorners(), rvec, tvec, cameraMatrix, distCoeffs,
projectedCharucoCorners);
for(unsigned int i = 0; i < charucoIds.size(); i++) {
int currentId = charucoIds[i];
ASSERT_LT(currentId, (int)board.getChessboardCorners().size()) << "Invalid Charuco corner id";
double repError = cv::norm(charucoCorners[i] - projectedCharucoCorners[currentId]); // TODO cvtest
ASSERT_LE(repError, 5.) << "Charuco corner reprojection error too high";
}
}
}
}
}
/**
* @brief Check diamond detection
*/
class CV_CharucoDiamondDetection : public cvtest::BaseTest {
public:
CV_CharucoDiamondDetection();
protected:
void run(int);
};
CV_CharucoDiamondDetection::CV_CharucoDiamondDetection() {}
void CV_CharucoDiamondDetection::run(int) {
int iter = 0;
Mat cameraMatrix = Mat::eye(3, 3, CV_64FC1);
Size imgSize(500, 500);
aruco::DetectorParameters params;
params.minDistanceToBorder = 0;
float squareLength = 0.03f;
float markerLength = 0.015f;
aruco::CharucoBoard board(Size(3, 3), squareLength, markerLength,
aruco::getPredefinedDictionary(aruco::DICT_6X6_250));
aruco::CharucoDetector detector(board);
cameraMatrix.at<double>(0, 0) = cameraMatrix.at< double >(1, 1) = 650;
cameraMatrix.at<double>(0, 2) = imgSize.width / 2;
cameraMatrix.at<double>(1, 2) = imgSize.height / 2;
Mat distCoeffs(5, 1, CV_64FC1, Scalar::all(0));
aruco::CharucoParameters charucoParameters;
charucoParameters.cameraMatrix = cameraMatrix;
charucoParameters.distCoeffs = distCoeffs;
detector.setCharucoParameters(charucoParameters);
// for different perspectives
for(double distance : {0.2, 0.22}) {
for(int yaw = -50; yaw <= 50; yaw += 25) {
for(int pitch = -50; pitch <= 50; pitch += 25) {
int markerBorder = iter % 2 + 1;
vector<int> idsTmp;
for(int i = 0; i < 4; i++)
idsTmp.push_back(4 * iter + i);
board = aruco::CharucoBoard(Size(3, 3), squareLength, markerLength,
aruco::getPredefinedDictionary(aruco::DICT_6X6_250), idsTmp);
detector.setBoard(board);
iter++;
// get synthetic image
Mat rvec, tvec;
Mat img = projectCharucoBoard(board, cameraMatrix, deg2rad(yaw), deg2rad(pitch),
distance, imgSize, markerBorder, rvec, tvec);
// detect markers
vector<vector<Point2f>> corners;
vector<int> ids;
params.markerBorderBits = markerBorder;
detector.setDetectorParameters(params);
//detector.detectMarkers(img, corners, ids);
// detect diamonds
vector<vector<Point2f>> diamondCorners;
vector<Vec4i> diamondIds;
detector.detectDiamonds(img, diamondCorners, diamondIds, corners, ids);
// check detect
if(ids.size() != 4) {
ts->printf(cvtest::TS::LOG, "Not enough markers for diamond detection");
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
return;
}
// check results
if(diamondIds.size() != 1) {
ts->printf(cvtest::TS::LOG, "Diamond not detected correctly");
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
return;
}
for(int i = 0; i < 4; i++) {
if(diamondIds[0][i] != board.getIds()[i]) {
ts->printf(cvtest::TS::LOG, "Incorrect diamond ids");
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
return;
}
}
vector< Point2f > projectedDiamondCorners;
// copy chessboardCorners
vector<Point3f> copyChessboardCorners = board.getChessboardCorners();
// move copyChessboardCorners points
for (size_t i = 0; i < copyChessboardCorners.size(); i++)
copyChessboardCorners[i] -= board.getRightBottomCorner() / 2.f;
projectPoints(copyChessboardCorners, rvec, tvec, cameraMatrix, distCoeffs,
projectedDiamondCorners);
vector< Point2f > projectedDiamondCornersReorder(4);
projectedDiamondCornersReorder[0] = projectedDiamondCorners[0];
projectedDiamondCornersReorder[1] = projectedDiamondCorners[1];
projectedDiamondCornersReorder[2] = projectedDiamondCorners[3];
projectedDiamondCornersReorder[3] = projectedDiamondCorners[2];
for(unsigned int i = 0; i < 4; i++) {
double repError = cv::norm(diamondCorners[0][i] - projectedDiamondCornersReorder[i]); // TODO cvtest
if(repError > 5.) {
ts->printf(cvtest::TS::LOG, "Diamond corner reprojection error too high");
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
return;
}
}
// estimate diamond pose
vector< Vec3d > estimatedRvec, estimatedTvec;
getMarkersPoses(diamondCorners, squareLength, cameraMatrix, distCoeffs, estimatedRvec,
estimatedTvec, noArray(), false);
// check result
vector< Point2f > projectedDiamondCornersPose;
vector< Vec3f > diamondObjPoints(4);
diamondObjPoints[0] = Vec3f(0.f, 0.f, 0);
diamondObjPoints[1] = Vec3f(squareLength, 0.f, 0);
diamondObjPoints[2] = Vec3f(squareLength, squareLength, 0);
diamondObjPoints[3] = Vec3f(0.f, squareLength, 0);
projectPoints(diamondObjPoints, estimatedRvec[0], estimatedTvec[0], cameraMatrix,
distCoeffs, projectedDiamondCornersPose);
for(unsigned int i = 0; i < 4; i++) {
double repError = cv::norm(projectedDiamondCornersReorder[i] - projectedDiamondCornersPose[i]); // TODO cvtest
if(repError > 5.) {
ts->printf(cvtest::TS::LOG, "Charuco pose error too high");
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
return;
}
}
}
}
}
}
/**
* @brief Check charuco board creation
*/
class CV_CharucoBoardCreation : public cvtest::BaseTest {
public:
CV_CharucoBoardCreation();
protected:
void run(int);
};
CV_CharucoBoardCreation::CV_CharucoBoardCreation() {}
void CV_CharucoBoardCreation::run(int)
{
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(aruco::DICT_5X5_250);
int n = 6;
float markerSizeFactor = 0.5f;
for (float squareSize_mm = 5.0f; squareSize_mm < 35.0f; squareSize_mm += 0.1f)
{
aruco::CharucoBoard board_meters(Size(n, n), squareSize_mm*1e-3f,
squareSize_mm * markerSizeFactor * 1e-3f, dictionary);
aruco::CharucoBoard board_millimeters(Size(n, n), squareSize_mm,
squareSize_mm * markerSizeFactor, dictionary);
for (size_t i = 0; i < board_meters.getNearestMarkerIdx().size(); i++)
{
if (board_meters.getNearestMarkerIdx()[i].size() != board_millimeters.getNearestMarkerIdx()[i].size() ||
board_meters.getNearestMarkerIdx()[i][0] != board_millimeters.getNearestMarkerIdx()[i][0])
{
ts->printf(cvtest::TS::LOG,
cv::format("Charuco board topology is sensitive to scale with squareSize=%.1f\n",
squareSize_mm).c_str());
ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
break;
}
}
}
}
TEST(CV_CharucoDetection, accuracy) {
const bool legacyPattern = false;
CV_CharucoDetection test(legacyPattern);
test.safe_run();
}
TEST(CV_CharucoDetection, accuracy_legacyPattern) {
const bool legacyPattern = true;
CV_CharucoDetection test(legacyPattern);
test.safe_run();
}
TEST(CV_CharucoPoseEstimation, accuracy) {
const bool legacyPattern = false;
CV_CharucoPoseEstimation test(legacyPattern);
test.safe_run();
}
TEST(CV_CharucoPoseEstimation, accuracy_legacyPattern) {
const bool legacyPattern = true;
CV_CharucoPoseEstimation test(legacyPattern);
test.safe_run();
}
TEST(CV_CharucoDiamondDetection, accuracy) {
CV_CharucoDiamondDetection test;
test.safe_run();
}
TEST(CV_CharucoBoardCreation, accuracy) {
CV_CharucoBoardCreation test;
test.safe_run();
}
TEST(Charuco, testCharucoCornersCollinear_true)
{
int squaresX = 13;
int squaresY = 28;
float squareLength = 300;
float markerLength = 150;
int dictionaryId = 11;
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(aruco::PredefinedDictionaryType(dictionaryId));
aruco::CharucoBoard charucoBoard(Size(squaresX, squaresY), squareLength, markerLength, dictionary);
// consistency with C++98
const int arrLine[9] = {192, 204, 216, 228, 240, 252, 264, 276, 288};
vector<int> charucoIdsAxisLine(9, 0);
for (int i = 0; i < 9; i++){
charucoIdsAxisLine[i] = arrLine[i];
}
const int arrDiag[7] = {198, 209, 220, 231, 242, 253, 264};
vector<int> charucoIdsDiagonalLine(7, 0);
for (int i = 0; i < 7; i++){
charucoIdsDiagonalLine[i] = arrDiag[i];
}
bool resultAxisLine = charucoBoard.checkCharucoCornersCollinear(charucoIdsAxisLine);
EXPECT_TRUE(resultAxisLine);
bool resultDiagonalLine = charucoBoard.checkCharucoCornersCollinear(charucoIdsDiagonalLine);
EXPECT_TRUE(resultDiagonalLine);
}
TEST(Charuco, testCharucoCornersCollinear_false)
{
int squaresX = 13;
int squaresY = 28;
float squareLength = 300;
float markerLength = 150;
int dictionaryId = 11;
aruco::Dictionary dictionary = aruco::getPredefinedDictionary(aruco::PredefinedDictionaryType(dictionaryId));
aruco::CharucoBoard charucoBoard(Size(squaresX, squaresY), squareLength, markerLength, dictionary);
// consistency with C++98
const int arr[63] = {192, 193, 194, 195, 196, 197, 198, 204, 205, 206, 207, 208,
209, 210, 216, 217, 218, 219, 220, 221, 222, 228, 229, 230,
231, 232, 233, 234, 240, 241, 242, 243, 244, 245, 246, 252,
253, 254, 255, 256, 257, 258, 264, 265, 266, 267, 268, 269,
270, 276, 277, 278, 279, 280, 281, 282, 288, 289, 290, 291,
292, 293, 294};
vector<int> charucoIds(63, 0);
for (int i = 0; i < 63; i++){
charucoIds[i] = arr[i];
}
bool result = charucoBoard.checkCharucoCornersCollinear(charucoIds);
EXPECT_FALSE(result);
}
// test that ChArUco board detection is subpixel accurate
TEST(Charuco, testBoardSubpixelCoords)
{
cv::Size res{500, 500};
cv::Mat K = (cv::Mat_<double>(3,3) <<
0.5*res.width, 0, 0.5*res.width,
0, 0.5*res.height, 0.5*res.height,
0, 0, 1);
// set expected_corners values
// Note: Values adjusted by -0.5px after fixing the systematic offset bug in charuco_detector.cpp
// The fix removes the incorrect +0.5 offset that was added after cornerSubPix
cv::Mat expected_corners = (cv::Mat_<float>(9,2) <<
199.5, 199.5,
249.5, 199.5,
299.5, 199.5,
199.5, 249.5,
249.5, 249.5,
299.5, 249.5,
199.5, 299.5,
249.5, 299.5,
299.5, 299.5
);
cv::Mat gray;
aruco::Dictionary dict = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_APRILTAG_36h11);
aruco::CharucoBoard board(Size(4, 4), 1.f, .8f, dict);
// generate ChArUco board
board.generateImage(Size(res.width, res.height), gray, 150);
cv::GaussianBlur(gray, gray, Size(5, 5), 1.0);
aruco::DetectorParameters params;
params.cornerRefinementMethod = (int)cv::aruco::CORNER_REFINE_APRILTAG;
aruco::CharucoParameters charucoParameters;
charucoParameters.cameraMatrix = K;
aruco::CharucoDetector detector(board, charucoParameters);
detector.setDetectorParameters(params);
std::vector<int> ids;
std::vector<std::vector<cv::Point2f>> corners;
cv::Mat c_ids, c_corners;
detector.detectBoard(gray, c_corners, c_ids, corners, ids);
ASSERT_EQ(ids.size(), size_t(8));
ASSERT_EQ(c_corners.rows, expected_corners.rows);
EXPECT_NEAR(0, cvtest::norm(expected_corners, c_corners.reshape(1), NORM_INF), 0.1);
}
TEST(Charuco, issue_14014)
{
string imgPath = cvtest::findDataFile("aruco/recover.png");
Mat img = imread(imgPath);
aruco::DetectorParameters detectorParams;
detectorParams.cornerRefinementMethod = (int)aruco::CORNER_REFINE_SUBPIX;
detectorParams.cornerRefinementMinAccuracy = 0.01;
aruco::ArucoDetector detector(aruco::getPredefinedDictionary(aruco::DICT_7X7_250), detectorParams);
aruco::CharucoBoard board(Size(8, 5), 0.03455f, 0.02164f, detector.getDictionary());
vector<Mat> corners, rejectedPoints;
vector<int> ids;
detector.detectMarkers(img, corners, ids, rejectedPoints);
ASSERT_EQ(corners.size(), 19ull);
EXPECT_EQ(Size(4, 1), corners[0].size()); // check dimension of detected corners
size_t numRejPoints = rejectedPoints.size();
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);
ASSERT_EQ(corners.size(), 20ull);
EXPECT_EQ(Size(4, 1), corners[0].size()); // check dimension of rejected corners after successfully refine
ASSERT_EQ(rejectedPoints.size() + 1, numRejPoints);
EXPECT_EQ(Size(4, 1), rejectedPoints[0].size()); // check dimension of rejected corners after successfully refine
}
TEST(Charuco, testmatchImagePoints)
{
aruco::CharucoBoard board(Size(2, 3), 1.f, 0.5f, aruco::getPredefinedDictionary(aruco::DICT_4X4_50));
auto chessboardPoints = board.getChessboardCorners();
vector<int> detectedIds;
vector<Point2f> detectedCharucoCorners;
for (const Point3f& point : chessboardPoints) {
detectedIds.push_back((int)detectedCharucoCorners.size());
detectedCharucoCorners.push_back({2.f*point.x, 2.f*point.y});
}
vector<Point3f> objPoints;
vector<Point2f> imagePoints;
board.matchImagePoints(detectedCharucoCorners, detectedIds, objPoints, imagePoints);
ASSERT_EQ(detectedCharucoCorners.size(), objPoints.size());
ASSERT_EQ(detectedCharucoCorners.size(), imagePoints.size());
for (size_t i = 0ull; i < detectedCharucoCorners.size(); i++) {
EXPECT_EQ(detectedCharucoCorners[i], imagePoints[i]);
EXPECT_EQ(chessboardPoints[i].x, objPoints[i].x);
EXPECT_EQ(chessboardPoints[i].y, objPoints[i].y);
}
}
TEST(Charuco, detectDiamondsClearsOutputsWithLessThanFourMarkers)
{
aruco::CharucoBoard board(Size(3, 3), 1.f, 0.5f, aruco::getPredefinedDictionary(aruco::DICT_4X4_50));
aruco::CharucoDetector detector(board);
vector<vector<Point2f>> markerCorners = {
{Point2f(10.f, 10.f), Point2f(20.f, 10.f), Point2f(20.f, 20.f), Point2f(10.f, 20.f)},
{Point2f(30.f, 10.f), Point2f(40.f, 10.f), Point2f(40.f, 20.f), Point2f(30.f, 20.f)},
{Point2f(10.f, 30.f), Point2f(20.f, 30.f), Point2f(20.f, 40.f), Point2f(10.f, 40.f)}
};
vector<int> markerIds = {0, 1, 2};
vector<vector<Point2f>> diamondCorners = {{Point2f(1.f, 1.f), Point2f(2.f, 1.f), Point2f(2.f, 2.f), Point2f(1.f, 2.f)}};
vector<Vec4i> diamondIds = {Vec4i(0, 1, 2, 3)};
detector.detectDiamonds(Mat(), diamondCorners, diamondIds, markerCorners, markerIds);
EXPECT_TRUE(diamondCorners.empty());
EXPECT_TRUE(diamondIds.empty());
}
typedef testing::TestWithParam<int> CharucoDraw;
INSTANTIATE_TEST_CASE_P(/**/, CharucoDraw, testing::Values(CV_8UC2, CV_8SC2, CV_16UC2, CV_16SC2, CV_32SC2, CV_32FC2, CV_64FC2));
TEST_P(CharucoDraw, testDrawDetected) {
vector<vector<Point>> detected_golds = {{Point(20, 20), Point(80, 20), Point(80, 80), Point2f(20, 80)}};
Point center_gold = (detected_golds[0][0] + detected_golds[0][1] + detected_golds[0][2] + detected_golds[0][3]) / 4;
int type = GetParam();
vector<Mat> detected(detected_golds[0].size(), Mat(4, 1, type));
// copy detected_golds to detected with any 2 channels type
for (size_t i = 0ull; i < detected_golds[0].size(); i++) {
detected[0].row((int)i) = Scalar(detected_golds[0][i].x, detected_golds[0][i].y);
}
vector<vector<Point>> contours;
Point detectedCenter;
Moments m;
Mat img;
// check drawDetectedMarkers
img = Mat::zeros(100, 100, CV_8UC1);
ASSERT_NO_THROW(aruco::drawDetectedMarkers(img, detected, noArray(), Scalar(255, 255, 255)));
// check that the marker borders are painted
findContours(img, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
ASSERT_EQ(contours.size(), 1ull);
m = moments(contours[0]);
detectedCenter = Point(cvRound(m.m10/m.m00), cvRound(m.m01/m.m00));
ASSERT_EQ(detectedCenter, center_gold);
// check drawDetectedCornersCharuco
img = Mat::zeros(100, 100, CV_8UC1);
ASSERT_NO_THROW(aruco::drawDetectedCornersCharuco(img, detected[0], noArray(), Scalar(255, 255, 255)));
// check that the 4 charuco corners are painted
findContours(img, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
ASSERT_EQ(contours.size(), 4ull);
for (size_t i = 0ull; i < 4ull; i++) {
m = moments(contours[i]);
detectedCenter = Point(cvRound(m.m10/m.m00), cvRound(m.m01/m.m00));
// detectedCenter must be in detected_golds
ASSERT_TRUE(find(detected_golds[0].begin(), detected_golds[0].end(), detectedCenter) != detected_golds[0].end());
}
// check drawDetectedDiamonds
img = Mat::zeros(100, 100, CV_8UC1);
ASSERT_NO_THROW(aruco::drawDetectedDiamonds(img, detected, noArray(), Scalar(255, 255, 255)));
// check that the diamonds borders are painted
findContours(img, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
ASSERT_EQ(contours.size(), 1ull);
m = moments(contours[0]);
detectedCenter = Point(cvRound(m.m10/m.m00), cvRound(m.m01/m.m00));
ASSERT_EQ(detectedCenter, center_gold);
}
typedef testing::TestWithParam<cv::Size> CharucoBoard;
INSTANTIATE_TEST_CASE_P(/**/, CharucoBoard, testing::Values(Size(3, 2), Size(3, 2), Size(6, 2), Size(2, 6),
Size(3, 4), Size(4, 3), Size(7, 3), Size(3, 7)));
TEST_P(CharucoBoard, testWrongSizeDetection)
{
cv::Size boardSize = GetParam();
ASSERT_FALSE(boardSize.width == boardSize.height);
aruco::CharucoBoard board(boardSize, 1.f, 0.5f, aruco::getPredefinedDictionary(aruco::DICT_4X4_50));
Mat boardImage;
board.generateImage(boardSize*40, boardImage);
swap(boardSize.width, boardSize.height);
aruco::CharucoDetector detector(aruco::CharucoBoard(boardSize, 1.f, 0.5f, aruco::getPredefinedDictionary(aruco::DICT_4X4_50)));
// try detect board with wrong size
for(int i: {0, 1}) {
vector<int> detectedCharucoIds, detectedArucoIds;
vector<Point2f> detectedCharucoCorners;
vector<vector<Point2f>> detectedArucoCorners;
if (i == 0) {
detector.detectBoard(boardImage, detectedCharucoCorners, detectedCharucoIds, detectedArucoCorners, detectedArucoIds);
// aruco markers must be found
ASSERT_EQ(detectedArucoIds.size(), board.getIds().size());
ASSERT_EQ(detectedArucoCorners.size(), board.getIds().size());
} else {
detector.detectBoard(boardImage, detectedCharucoCorners, detectedCharucoIds);
}
// charuco corners should not be found in board with wrong size
ASSERT_TRUE(detectedCharucoCorners.empty());
ASSERT_TRUE(detectedCharucoIds.empty());
}
}
typedef testing::TestWithParam<std::tuple<cv::Size, float, cv::Size, int>> CharucoBoardGenerate;
INSTANTIATE_TEST_CASE_P(/**/, CharucoBoardGenerate, testing::Values(make_tuple(Size(7, 4), 13.f, Size(400, 300), 24),
make_tuple(Size(12, 2), 13.f, Size(200, 150), 1),
make_tuple(Size(12, 2), 13.1f, Size(400, 300), 1)));
TEST_P(CharucoBoardGenerate, issue_24806)
{
aruco::Dictionary dict = aruco::getPredefinedDictionary(aruco::DICT_4X4_1000);
auto params = GetParam();
const Size boardSize = std::get<0>(params);
const float squareLength = std::get<1>(params), markerLength = 10.f;
Size imgSize = std::get<2>(params);
const aruco::CharucoBoard board(boardSize, squareLength, markerLength, dict);
const int marginSize = std::get<3>(params);
Mat boardImg;
// generate chessboard image
board.generateImage(imgSize, boardImg, marginSize);
// This condition checks that the width of the image determines the dimensions of the chessboard in this test
CV_Assert((float)(boardImg.cols) / (float)boardSize.width <=
(float)(boardImg.rows) / (float)boardSize.height);
// prepare data for chessboard image test
Mat noMarginsImg = boardImg(Range(marginSize, boardImg.rows - marginSize),
Range(marginSize, boardImg.cols - marginSize));
const float pixInSquare = (float)(noMarginsImg.cols) / (float)boardSize.width;
Size pixInChessboard(cvRound(pixInSquare*boardSize.width), cvRound(pixInSquare*boardSize.height));
const Point startChessboard((noMarginsImg.cols - pixInChessboard.width) / 2,
(noMarginsImg.rows - pixInChessboard.height) / 2);
Mat chessboardZoneImg = noMarginsImg(Rect(startChessboard, pixInChessboard));
// B - black pixel, W - white pixel
// chessboard corner 1:
// B W
// W B
Mat goldCorner1 = (Mat_<uint8_t>(2, 2) <<
0, 255,
255, 0);
// B - black pixel, W - white pixel
// chessboard corner 2:
// W B
// B W
Mat goldCorner2 = (Mat_<uint8_t>(2, 2) <<
255, 0,
0, 255);
// test chessboard corners in generated image
for (const Point3f& p: board.getChessboardCorners()) {
Point2f chessCorner(pixInSquare*(p.x/squareLength),
pixInSquare*(p.y/squareLength));
Mat winCorner = chessboardZoneImg(Rect(Point(cvRound(chessCorner.x) - 1, cvRound(chessCorner.y) - 1), Size(2, 2)));
bool eq = (cv::countNonZero(goldCorner1 != winCorner) == 0) || (cv::countNonZero(goldCorner2 != winCorner) == 0);
ASSERT_TRUE(eq);
}
// marker size in pixels
const float pixInMarker = markerLength/squareLength*pixInSquare;
// the size of the marker margin in pixels
const float pixInMarginMarker = 0.5f*(pixInSquare - pixInMarker);
// determine the zone where the aruco markers are located
int endArucoX = cvRound(pixInSquare*(boardSize.width-1)+pixInMarginMarker+pixInMarker);
int endArucoY = cvRound(pixInSquare*(boardSize.height-1)+pixInMarginMarker+pixInMarker);
Mat arucoZone = chessboardZoneImg(Range(cvRound(pixInMarginMarker), endArucoY), Range(cvRound(pixInMarginMarker), endArucoX));
const auto& markerCorners = board.getObjPoints();
float minX, maxX, minY, maxY;
minX = maxX = markerCorners[0][0].x;
minY = maxY = markerCorners[0][0].y;
for (const auto& marker : markerCorners) {
for (const Point3f& objCorner : marker) {
minX = min(minX, objCorner.x);
maxX = max(maxX, objCorner.x);
minY = min(minY, objCorner.y);
maxY = max(maxY, objCorner.y);
}
}
Point2f outCorners[3];
for (const auto& marker : markerCorners) {
for (int i = 0; i < 3; i++) {
outCorners[i] = Point2f(marker[i].x, marker[i].y) - Point2f(minX, minY);
outCorners[i].x = outCorners[i].x / (maxX - minX) * float(arucoZone.cols);
outCorners[i].y = outCorners[i].y / (maxY - minY) * float(arucoZone.rows);
}
Size dst_sz(outCorners[2] - outCorners[0]); // assuming CCW order
dst_sz.width = dst_sz.height = std::min(dst_sz.width, dst_sz.height);
Rect borderRect = Rect(outCorners[0], dst_sz);
//The test checks the inner and outer borders of the Aruco markers.
//In the inner border of Aruco marker, all pixels should be black.
//In the outer border of Aruco marker, all pixels should be white.
Mat markerImg = arucoZone(borderRect);
bool markerBorderIsBlack = borderPixelsHaveSameColor(markerImg, 0);
ASSERT_EQ(markerBorderIsBlack, true);
Mat markerOuterBorder = markerImg;
markerOuterBorder.adjustROI(1, 1, 1, 1);
bool markerOuterBorderIsWhite = borderPixelsHaveSameColor(markerOuterBorder, 255);
ASSERT_EQ(markerOuterBorderIsWhite, true);
}
}
TEST(Charuco, testSeveralBoardsWithCustomIds)
{
Size res{500, 500};
Mat K = (Mat_<double>(3,3) <<
0.5*res.width, 0, 0.5*res.width,
0, 0.5*res.height, 0.5*res.height,
0, 0, 1);
// Expected corner coordinates adjusted by -0.5px after fixing the systematic offset bug
// The fix removes the incorrect +0.5 offset that was added after cornerSubPix
Mat expected_corners = (Mat_<float>(9,2) <<
199.5, 199.5,
249.5, 199.5,
299.5, 199.5,
199.5, 249.5,
249.5, 249.5,
299.5, 249.5,
199.5, 299.5,
249.5, 299.5,
299.5, 299.5
);
aruco::Dictionary dict = cv::aruco::getPredefinedDictionary(aruco::DICT_4X4_50);
vector<int> ids1 = {0, 1, 33, 3, 4, 5, 6, 8}, ids2 = {7, 9, 44, 11, 12, 13, 14, 15};
aruco::CharucoBoard board1(Size(4, 4), 1.f, .8f, dict, ids1), board2(Size(4, 4), 1.f, .8f, dict, ids2);
// generate ChArUco board
Mat gray;
{
Mat gray1, gray2;
board1.generateImage(Size(res.width, res.height), gray1, 150);
board2.generateImage(Size(res.width, res.height), gray2, 150);
hconcat(gray1, gray2, gray);
}
aruco::CharucoParameters charucoParameters;
charucoParameters.cameraMatrix = K;
aruco::CharucoDetector detector1(board1, charucoParameters), detector2(board2, charucoParameters);
vector<int> ids;
vector<Mat> corners;
Mat c_ids1, c_ids2, c_corners1, c_corners2;
detector1.detectBoard(gray, c_corners1, c_ids1, corners, ids);
detector2.detectBoard(gray, c_corners2, c_ids2, corners, ids);
ASSERT_EQ(ids.size(), size_t(16));
// In 4.x detectBoard() returns the charuco corners in a 2D Mat with shape (N_corners, 1)
// In 5.x, after PR #23473, detectBoard() returns the charuco corners in a 1D Mat with shape (1, N_corners)
ASSERT_EQ(expected_corners.total(), c_corners1.total()*c_corners1.channels());
EXPECT_NEAR(0., cvtest::norm(expected_corners.reshape(1, 1), c_corners1.reshape(1, 1), NORM_INF), 0.1);
ASSERT_EQ(expected_corners.total(), c_corners2.total()*c_corners2.channels());
expected_corners.col(0) += 500;
EXPECT_NEAR(0., cvtest::norm(expected_corners.reshape(1, 1), c_corners2.reshape(1, 1), NORM_INF), 0.1);
}
}} // namespace
+216
View File
@@ -0,0 +1,216 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
namespace opencv_test { namespace {
// label format:
// image_name
// num_face
// face_1
// face_..
// face_num
std::map<std::string, Mat> blobFromTXT(const std::string& path, int numCoords)
{
std::ifstream ifs(path.c_str());
CV_Assert(ifs.is_open());
std::map<std::string, Mat> gt;
Mat faces;
int faceNum = -1;
int faceCount = 0;
for (std::string line, key; getline(ifs, line); )
{
std::istringstream iss(line);
if (line.find(".png") != std::string::npos)
{
// Get filename
iss >> key;
}
else if (line.find(" ") == std::string::npos)
{
// Get the number of faces
iss >> faceNum;
}
else
{
// Get faces
Mat face(1, numCoords, CV_32FC1);
for (int j = 0; j < numCoords; j++)
{
iss >> face.at<float>(0, j);
}
faces.push_back(face);
faceCount++;
}
if (faceCount == faceNum)
{
// Store faces
gt[key] = faces;
faces.release();
faceNum = -1;
faceCount = 0;
}
}
return gt;
}
TEST(Objdetect_face_detection, regression)
{
// Pre-set params
float scoreThreshold = 0.7f;
float matchThreshold = 0.7f;
float l2disThreshold = 15.0f;
int numLM = 5;
int numCoords = 4 + 2 * numLM;
// Load ground truth labels
std::map<std::string, Mat> gt = blobFromTXT(findDataFile("dnn_face/detection/cascades_labels.txt"), numCoords);
// Initialize detector
std::string model = findDataFile("dnn/onnx/models/yunet-202303.onnx", false);
Ptr<FaceDetectorYN> faceDetector = FaceDetectorYN::create(model, "", Size(300, 300));
faceDetector->setScoreThreshold(0.7f);
// Detect and match
for (auto item: gt)
{
std::string imagePath = findDataFile("cascadeandhog/images/" + item.first);
Mat image = imread(imagePath);
// Set input size
faceDetector->setInputSize(image.size());
// Run detection
Mat faces;
faceDetector->detect(image, faces);
// std::cout << item.first << " " << item.second.rows << " " << faces.rows << std::endl;
// Match bboxes and landmarks
std::vector<bool> matchedItem(item.second.rows, false);
for (int i = 0; i < faces.rows; i++)
{
if (faces.at<float>(i, numCoords) < scoreThreshold)
continue;
bool boxMatched = false;
std::vector<bool> lmMatched(numLM, false);
cv::Rect2f resBox(faces.at<float>(i, 0), faces.at<float>(i, 1), faces.at<float>(i, 2), faces.at<float>(i, 3));
for (int j = 0; j < item.second.rows && !boxMatched; j++)
{
if (matchedItem[j])
continue;
// Retrieve bbox and compare IoU
cv::Rect2f gtBox(item.second.at<float>(j, 0), item.second.at<float>(j, 1), item.second.at<float>(j, 2), item.second.at<float>(j, 3));
double interArea = (resBox & gtBox).area();
double iou = interArea / (resBox.area() + gtBox.area() - interArea);
if (iou >= matchThreshold)
{
boxMatched = true;
matchedItem[j] = true;
}
// Match landmarks if bbox is matched
if (!boxMatched)
continue;
for (int lmIdx = 0; lmIdx < numLM; lmIdx++)
{
float gtX = item.second.at<float>(j, 4 + 2 * lmIdx);
float gtY = item.second.at<float>(j, 4 + 2 * lmIdx + 1);
float resX = faces.at<float>(i, 4 + 2 * lmIdx);
float resY = faces.at<float>(i, 4 + 2 * lmIdx + 1);
float l2dis = cv::sqrt((gtX - resX) * (gtX - resX) + (gtY - resY) * (gtY - resY));
if (l2dis <= l2disThreshold)
{
lmMatched[lmIdx] = true;
}
}
break;
}
EXPECT_TRUE(boxMatched) << "In image " << item.first << ", cannot match resBox " << resBox << " with any ground truth.";
if (boxMatched)
{
EXPECT_TRUE(std::all_of(lmMatched.begin(), lmMatched.end(), [](bool v) { return v; })) << "In image " << item.first << ", resBox " << resBox << " matched but its landmarks failed to match.";
}
}
}
}
TEST(Objdetect_face_recognition, regression)
{
// Pre-set params
float score_thresh = 0.9f;
float nms_thresh = 0.3f;
double cosine_similar_thresh = 0.363;
double l2norm_similar_thresh = 1.128;
// Load ground truth labels
std::ifstream ifs(findDataFile("dnn_face/recognition/cascades_label.txt").c_str());
CV_Assert(ifs.is_open());
std::set<std::string> fSet;
std::map<std::string, Mat> featureMap;
std::map<std::pair<std::string, std::string>, int> gtMap;
for (std::string line, key; getline(ifs, line);)
{
std::string fname1, fname2;
int label;
std::istringstream iss(line);
iss>>fname1>>fname2>>label;
// std::cout<<fname1<<" "<<fname2<<" "<<label<<std::endl;
fSet.insert(fname1);
fSet.insert(fname2);
gtMap[std::make_pair(fname1, fname2)] = label;
}
// Initialize detector
std::string detect_model = findDataFile("dnn/onnx/models/yunet-202303.onnx", false);
Ptr<FaceDetectorYN> faceDetector = FaceDetectorYN::create(detect_model, "", Size(150, 150), score_thresh, nms_thresh);
std::string recog_model = findDataFile("dnn/onnx/models/face_recognizer_fast.onnx", false);
Ptr<FaceRecognizerSF> faceRecognizer = FaceRecognizerSF::create(recog_model, "");
// Detect and match
for (auto fname: fSet)
{
std::string imagePath = findDataFile("dnn_face/recognition/" + fname);
Mat image = imread(imagePath);
Mat faces;
faceDetector->detect(image, faces);
Mat aligned_face;
faceRecognizer->alignCrop(image, faces.row(0), aligned_face);
Mat feature;
faceRecognizer->feature(aligned_face, feature);
featureMap[fname] = feature.clone();
}
for (auto item: gtMap)
{
Mat feature1 = featureMap[item.first.first];
Mat feature2 = featureMap[item.first.second];
int label = item.second;
double cos_score = faceRecognizer->match(feature1, feature2, FaceRecognizerSF::DisType::FR_COSINE);
double L2_score = faceRecognizer->match(feature1, feature2, FaceRecognizerSF::DisType::FR_NORM_L2);
EXPECT_TRUE(label == 0 ? cos_score <= cosine_similar_thresh : cos_score > cosine_similar_thresh) << "Cosine match result of images " << item.first.first << " and " << item.first.second << " is different from ground truth (score: "<< cos_score <<";Thresh: "<< cosine_similar_thresh <<").";
EXPECT_TRUE(label == 0 ? L2_score > l2norm_similar_thresh : L2_score <= l2norm_similar_thresh) << "L2norm match result of images " << item.first.first << " and " << item.first.second << " is different from ground truth (score: "<< L2_score <<";Thresh: "<< l2norm_similar_thresh <<").";
}
}
}} // namespace
+18
View File
@@ -0,0 +1,18 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#if defined(HAVE_HPX)
#include <hpx/hpx_main.hpp>
#endif
static
void initTests()
{
#ifdef HAVE_OPENCV_DNN
cvtest::addDataSearchEnv("OPENCV_DNN_TEST_DATA_PATH");
#endif // HAVE_OPENCV_DNN
}
CV_TEST_MAIN("cv", initTests())
+12
View File
@@ -0,0 +1,12 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#ifndef __OPENCV_TEST_PRECOMP_HPP__
#define __OPENCV_TEST_PRECOMP_HPP__
#include "opencv2/ts.hpp"
#include "opencv2/objdetect.hpp"
#include <random>
#endif
+78
View File
@@ -0,0 +1,78 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
namespace opencv_test {
static inline
void check_qr(const string& root, const string& name_current_image, const string& config_name,
const std::vector<Point>& corners,
const std::vector<string>& decoded_info, const int max_pixel_error,
bool isMulti = false) {
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;
FileNode images_list = file_config[config_name];
size_t images_count = static_cast<size_t>(images_list.size());
ASSERT_GT(images_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
for (size_t index = 0; index < images_count; index++) {
FileNode config = images_list[(int)index];
std::string name_test_image = config["image_name"];
if (name_test_image == name_current_image) {
if (isMulti) {
for(int j = 0; j < int(corners.size()); j += 4) {
bool ok = false;
for (int k = 0; k < int(corners.size() / 4); k++) {
int count_eq_points = 0;
for (int i = 0; i < 4; i++) {
int x = config["x"][k][i];
int y = config["y"][k][i];
if(((abs(corners[j + i].x - x)) <= max_pixel_error) && ((abs(corners[j + i].y - y)) <= max_pixel_error))
count_eq_points++;
}
if (count_eq_points == 4) {
ok = true;
break;
}
}
EXPECT_TRUE(ok);
}
}
else {
for (int i = 0; i < (int)corners.size(); i++) {
int x = config["x"][i];
int y = config["y"][i];
EXPECT_NEAR(x, corners[i].x, max_pixel_error);
EXPECT_NEAR(y, corners[i].y, max_pixel_error);
}
}
if (decoded_info.size() == 0ull)
return;
if (isMulti) {
size_t count_eq_info = 0;
for(int i = 0; i < int(decoded_info.size()); i++) {
for(int j = 0; j < int(decoded_info.size()); j++) {
std::string original_info = config["info"][j];
if(original_info == decoded_info[i]) {
count_eq_info++;
break;
}
}
}
EXPECT_EQ(decoded_info.size(), count_eq_info);
}
else {
std::string original_info = config["info"];
EXPECT_EQ(decoded_info[0], original_info);
}
return; // done
}
}
FAIL() << "Not found results for '" << name_current_image << "' image in config file:" << dataset_config <<
"Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data.\n";
}
}
+766
View File
@@ -0,0 +1,766 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include "test_qr_utils.hpp"
#include "opencv2/imgproc.hpp"
namespace opencv_test { namespace {
std::string qrcode_images_name[] = {
"version_1_down.jpg", "version_1_left.jpg", "version_1_right.jpg", "version_1_up.jpg", "version_1_top.jpg",
"version_2_down.jpg", "version_2_left.jpg", "version_2_right.jpg", "version_2_up.jpg", "version_2_top.jpg",
"version_3_down.jpg", "version_3_left.jpg", "version_3_right.jpg", "version_3_up.jpg", "version_3_top.jpg",
"version_4_down.jpg", "version_4_left.jpg", "version_4_right.jpg", "version_4_up.jpg", "version_4_top.jpg",
"version_5_down.jpg", "version_5_left.jpg", /*"version_5_right.jpg",*/ "version_5_up.jpg", "version_5_top.jpg",
"russian.jpg", "kanji.jpg", "link_github_ocv.jpg", "link_ocv.jpg", "link_wiki_cv.jpg"
// version_5_right.jpg DISABLED after tile fix, PR #22025
};
// Todo: fix corner align in big QRs to enable close_5.png
std::string qrcode_images_close[] = {
"close_1.png", "close_2.png", "close_3.png", "close_4.png"//, "close_5.png"
};
std::string qrcode_images_monitor[] = {
"monitor_1.png", "monitor_2.png", "monitor_3.png", "monitor_4.png", "monitor_5.png"
};
std::string qrcode_images_curved[] = {
"curved_1.jpg", "curved_2.jpg", "curved_3.jpg", /*"curved_4.jpg",*/ "curved_5.jpg", /*"curved_6.jpg",*/ "curved_7.jpg", "curved_8.jpg"
};
// curved_4.jpg, curved_6.jpg DISABLED after tile fix, PR #22025
std::string qrcode_images_multiple[] = {
"2_qrcodes.png", "3_close_qrcodes.png", "3_qrcodes.png", "4_qrcodes.png",
"5_qrcodes.png", "6_qrcodes.png", "7_qrcodes.png", "8_close_qrcodes.png"
};
static std::set<std::pair<std::string, std::string>> disabled_samples = {{"5_qrcodes.png", "aruco_based"}};
//#define UPDATE_QRCODE_TEST_DATA
#ifdef UPDATE_QRCODE_TEST_DATA
TEST(Objdetect_QRCode, generate_test_data)
{
const std::string root = "qrcode/";
const std::string dataset_config = findDataFile(root + "dataset_config.json");
FileStorage file_config(dataset_config, FileStorage::WRITE);
file_config << "test_images" << "[";
size_t images_count = sizeof(qrcode_images_name) / sizeof(qrcode_images_name[0]);
for (size_t i = 0; i < images_count; i++)
{
file_config << "{:" << "image_name" << qrcode_images_name[i];
std::string image_path = findDataFile(root + qrcode_images_name[i]);
std::vector<Point> corners;
Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
std::string decoded_info;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
EXPECT_TRUE(detectQRCode(src, corners));
EXPECT_TRUE(decodeQRCode(src, corners, decoded_info, straight_barcode));
file_config << "x" << "[:";
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
file_config << "]";
file_config << "y" << "[:";
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].y; }
file_config << "]";
file_config << "info" << decoded_info;
file_config << "}";
}
file_config << "]";
file_config.release();
}
TEST(Objdetect_QRCode_Close, generate_test_data)
{
const std::string root = "qrcode/close/";
const std::string dataset_config = findDataFile(root + "dataset_config.json");
FileStorage file_config(dataset_config, FileStorage::WRITE);
file_config << "close_images" << "[";
size_t close_count = sizeof(qrcode_images_close) / sizeof(qrcode_images_close[0]);
for (size_t i = 0; i < close_count; i++)
{
file_config << "{:" << "image_name" << qrcode_images_close[i];
std::string image_path = findDataFile(root + qrcode_images_close[i]);
std::vector<Point> corners;
Mat src = imread(image_path, IMREAD_GRAYSCALE), barcode, straight_barcode;
std::string decoded_info;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
const double min_side = std::min(src.size().width, src.size().height);
double coeff_expansion = 1024.0 / min_side;
const int width = cvRound(src.size().width * coeff_expansion);
const int height = cvRound(src.size().height * coeff_expansion);
Size new_size(width, height);
resize(src, barcode, new_size, 0, 0, INTER_LINEAR_EXACT);
EXPECT_TRUE(detectQRCode(barcode, corners));
EXPECT_TRUE(decodeQRCode(barcode, corners, decoded_info, straight_barcode));
file_config << "x" << "[:";
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
file_config << "]";
file_config << "y" << "[:";
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].y; }
file_config << "]";
file_config << "info" << decoded_info;
file_config << "}";
}
file_config << "]";
file_config.release();
}
TEST(Objdetect_QRCode_Monitor, generate_test_data)
{
const std::string root = "qrcode/monitor/";
const std::string dataset_config = findDataFile(root + "dataset_config.json");
FileStorage file_config(dataset_config, FileStorage::WRITE);
file_config << "monitor_images" << "[";
size_t monitor_count = sizeof(qrcode_images_monitor) / sizeof(qrcode_images_monitor[0]);
for (size_t i = 0; i < monitor_count; i++)
{
file_config << "{:" << "image_name" << qrcode_images_monitor[i];
std::string image_path = findDataFile(root + qrcode_images_monitor[i]);
std::vector<Point> corners;
Mat src = imread(image_path, IMREAD_GRAYSCALE), barcode, straight_barcode;
std::string decoded_info;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
const double min_side = std::min(src.size().width, src.size().height);
double coeff_expansion = 1024.0 / min_side;
const int width = cvRound(src.size().width * coeff_expansion);
const int height = cvRound(src.size().height * coeff_expansion);
Size new_size(width, height);
resize(src, barcode, new_size, 0, 0, INTER_LINEAR_EXACT);
EXPECT_TRUE(detectQRCode(barcode, corners));
EXPECT_TRUE(decodeQRCode(barcode, corners, decoded_info, straight_barcode));
file_config << "x" << "[:";
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
file_config << "]";
file_config << "y" << "[:";
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].y; }
file_config << "]";
file_config << "info" << decoded_info;
file_config << "}";
}
file_config << "]";
file_config.release();
}
TEST(Objdetect_QRCode_Curved, generate_test_data)
{
const std::string root = "qrcode/curved/";
const std::string dataset_config = findDataFile(root + "dataset_config.json");
FileStorage file_config(dataset_config, FileStorage::WRITE);
file_config << "test_images" << "[";
size_t images_count = sizeof(qrcode_images_curved) / sizeof(qrcode_images_curved[0]);
for (size_t i = 0; i < images_count; i++)
{
file_config << "{:" << "image_name" << qrcode_images_curved[i];
std::string image_path = findDataFile(root + qrcode_images_curved[i]);
std::vector<Point> corners;
Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
std::string decoded_info;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
EXPECT_TRUE(detectQRCode(src, corners));
EXPECT_TRUE(decodeCurvedQRCode(src, corners, decoded_info, straight_barcode));
file_config << "x" << "[:";
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }
file_config << "]";
file_config << "y" << "[:";
for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].y; }
file_config << "]";
file_config << "info" << decoded_info;
file_config << "}";
}
file_config << "]";
file_config.release();
}
TEST(Objdetect_QRCode_Multi, generate_test_data)
{
const std::string root = "qrcode/multiple/";
const std::string dataset_config = findDataFile(root + "dataset_config.json");
FileStorage file_config(dataset_config, FileStorage::WRITE);
file_config << "multiple_images" << "[:";
size_t multiple_count = sizeof(qrcode_images_multiple) / sizeof(qrcode_images_multiple[0]);
for (size_t i = 0; i < multiple_count; i++)
{
file_config << "{:" << "image_name" << qrcode_images_multiple[i];
std::string image_path = findDataFile(root + qrcode_images_multiple[i]);
Mat src = imread(image_path);
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
std::vector<Point> corners;
QRCodeDetector qrcode;
EXPECT_TRUE(qrcode.detectMulti(src, corners));
std::vector<cv::String> decoded_info;
std::vector<Mat> straight_barcode;
EXPECT_TRUE(qrcode.decodeMulti(src, corners, decoded_info, straight_barcode));
file_config << "x" << "[:";
for(size_t j = 0; j < corners.size(); j += 4)
{
file_config << "[:";
for (size_t k = 0; k < 4; k++)
{
file_config << corners[j + k].x;
}
file_config << "]";
}
file_config << "]";
file_config << "y" << "[:";
for(size_t j = 0; j < corners.size(); j += 4)
{
file_config << "[:";
for (size_t k = 0; k < 4; k++)
{
file_config << corners[j + k].y;
}
file_config << "]";
}
file_config << "]";
file_config << "info";
file_config << "[:";
for(size_t j = 0; j < decoded_info.size(); j++)
{
file_config << decoded_info[j];
}
file_config << "]";
file_config << "}";
}
file_config << "]";
file_config.release();
}
#else
typedef testing::TestWithParam< std::string > Objdetect_QRCode;
TEST_P(Objdetect_QRCode, regression)
{
const std::string name_current_image = GetParam();
const std::string root = "qrcode/";
const int pixels_error = 3;
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
std::vector<Point> corners;
std::string decoded_info;
QRCodeDetector qrcode;
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());
check_qr(root, name_current_image, "test_images", corners, {decoded_info}, pixels_error);
}
typedef testing::TestWithParam< std::string > Objdetect_QRCode_Close;
TEST_P(Objdetect_QRCode_Close, regression)
{
const std::string name_current_image = GetParam();
const std::string root = "qrcode/close/";
const int pixels_error = 3;
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path, IMREAD_GRAYSCALE), barcode, straight_barcode;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
const double min_side = std::min(src.size().width, src.size().height);
double coeff_expansion = 1024.0 / min_side;
const int width = cvRound(src.size().width * coeff_expansion);
const int height = cvRound(src.size().height * coeff_expansion);
Size new_size(width, height);
resize(src, barcode, new_size, 0, 0, INTER_LINEAR_EXACT);
std::vector<Point> corners;
std::string decoded_info;
QRCodeDetector qrcode;
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());
check_qr(root, name_current_image, "close_images", corners, {decoded_info}, pixels_error);
}
typedef testing::TestWithParam< std::string > Objdetect_QRCode_Monitor;
TEST_P(Objdetect_QRCode_Monitor, regression)
{
const std::string name_current_image = GetParam();
const std::string root = "qrcode/monitor/";
const int pixels_error = 3;
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path, IMREAD_GRAYSCALE), barcode, straight_barcode;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
const double min_side = std::min(src.size().width, src.size().height);
double coeff_expansion = 1024.0 / min_side;
const int width = cvRound(src.size().width * coeff_expansion);
const int height = cvRound(src.size().height * coeff_expansion);
Size new_size(width, height);
resize(src, barcode, new_size, 0, 0, INTER_LINEAR_EXACT);
std::vector<Point> corners;
std::string decoded_info;
QRCodeDetector qrcode;
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());
check_qr(root, name_current_image, "monitor_images", corners, {decoded_info}, pixels_error);
}
typedef testing::TestWithParam< std::string > Objdetect_QRCode_Curved;
TEST_P(Objdetect_QRCode_Curved, regression)
{
const std::string name_current_image = GetParam();
const std::string root = "qrcode/curved/";
const int pixels_error = 3;
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
std::vector<Point> corners;
std::string decoded_info;
QRCodeDetector qrcode;
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());
check_qr(root, name_current_image, "test_images", corners, {decoded_info}, pixels_error);
}
typedef testing::TestWithParam<std::tuple<std::string, std::string>> Objdetect_QRCode_Multi;
TEST_P(Objdetect_QRCode_Multi, regression)
{
const std::string name_current_image = get<0>(GetParam());
const std::string root = "qrcode/multiple/";
const std::string method = get<1>(GetParam());
const int pixels_error = 4;
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path);
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
if (disabled_samples.find({name_current_image, method}) != disabled_samples.end())
throw SkipTestException(name_current_image + " is disabled sample for method " + method);
GraphicalCodeDetector qrcode = QRCodeDetector();
if (method == "aruco_based") {
qrcode = QRCodeDetectorAruco();
}
std::vector<Point> corners;
std::vector<cv::String> decoded_info;
std::vector<Mat> straight_barcode;
EXPECT_TRUE(qrcode.detectAndDecodeMulti(src, decoded_info, corners, straight_barcode));
ASSERT_FALSE(corners.empty());
ASSERT_FALSE(decoded_info.empty());
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());
check_qr(root, name_current_image, "multiple_images", corners, decoded_info, pixels_error, true);
}
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode, testing::ValuesIn(qrcode_images_name));
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Close, testing::ValuesIn(qrcode_images_close));
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Monitor, testing::ValuesIn(qrcode_images_monitor));
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Curved, testing::ValuesIn(qrcode_images_curved));
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Multi, testing::Combine(testing::ValuesIn(qrcode_images_multiple),
testing::Values("contours_based", "aruco_based")));
TEST(Objdetect_QRCode_decodeMulti, decode_regression_16491)
{
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)};
std::vector<Point> vec_corners;
int array_size = 8;
vec_corners.assign(corners_, corners_ + array_size);
std::vector<cv::String> decoded_info;
std::vector<Mat> straight_barcode;
QRCodeDetector vec_qrcode;
EXPECT_NO_THROW(vec_qrcode.decodeMulti(zero_image, vec_corners, decoded_info, straight_barcode));
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));
}
typedef testing::TestWithParam<std::string> Objdetect_QRCode_detectMulti;
TEST_P(Objdetect_QRCode_detectMulti, detect_regression_16961)
{
const std::string method = GetParam();
const std::string name_current_image = "9_qrcodes.jpg";
const std::string root = "qrcode/multiple/";
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path);
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
GraphicalCodeDetector qrcode = QRCodeDetector();
if (method == "aruco_based") {
qrcode = QRCodeDetectorAruco();
}
std::vector<Point> corners;
EXPECT_TRUE(qrcode.detectMulti(src, corners));
ASSERT_FALSE(corners.empty());
size_t expect_corners_size = 36;
EXPECT_EQ(corners.size(), expect_corners_size);
}
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_detectMulti, testing::Values("contours_based", "aruco_based"));
typedef testing::TestWithParam<std::string> Objdetect_QRCode_detectAndDecodeMulti;
TEST_P(Objdetect_QRCode_detectAndDecodeMulti, check_output_parameters_type_19363)
{
const std::string name_current_image = "9_qrcodes.jpg";
const std::string root = "qrcode/multiple/";
const std::string method = GetParam();
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path);
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
GraphicalCodeDetector qrcode = QRCodeDetector();
if (method == "aruco_based") {
qrcode = QRCodeDetectorAruco();
}
std::vector<Point> corners;
std::vector<cv::String> decoded_info;
#if 0 // FIXIT: OutputArray::create() type check
std::vector<Mat2b> straight_barcode_nchannels;
EXPECT_ANY_THROW(qrcode->detectAndDecodeMulti(src, decoded_info, corners, straight_barcode_nchannels));
#endif
int expected_barcode_type = CV_8UC1;
std::vector<Mat1b> straight_barcode;
EXPECT_TRUE(qrcode.detectAndDecodeMulti(src, decoded_info, corners, straight_barcode));
ASSERT_FALSE(corners.empty());
for(size_t i = 0; i < straight_barcode.size(); i++)
EXPECT_EQ(expected_barcode_type, straight_barcode[i].type());
}
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_detectAndDecodeMulti, testing::Values("contours_based", "aruco_based"));
TEST(Objdetect_QRCode_detect, detect_regression_20882)
{
const std::string name_current_image = "qrcode_near_the_end.jpg";
const std::string root = "qrcode/";
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path);
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
QRCodeDetector qrcode;
std::vector<Point> corners;
Mat straight_barcode;
cv::String decoded_info;
EXPECT_TRUE(qrcode.detect(src, corners));
EXPECT_TRUE(!corners.empty());
EXPECT_NO_THROW(qrcode.decode(src, corners, straight_barcode));
}
TEST(Objdetect_QRCode_basic, not_found_qrcode)
{
std::vector<Point> corners;
Mat straight_barcode;
std::string decoded_info;
Mat zero_image = Mat::zeros(256, 256, CV_8UC1);
QRCodeDetector qrcode;
EXPECT_FALSE(qrcode.detect(zero_image, corners));
corners = std::vector<Point>(4);
EXPECT_ANY_THROW(qrcode.decode(zero_image, corners, straight_barcode));
}
TEST(Objdetect_QRCode_detect, detect_regression_21287)
{
const std::string name_current_image = "issue_21287.png";
const std::string root = "qrcode/";
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path);
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
QRCodeDetector qrcode;
std::vector<Point> corners;
Mat straight_barcode;
cv::String decoded_info;
EXPECT_TRUE(qrcode.detect(src, corners));
EXPECT_TRUE(!corners.empty());
EXPECT_NO_THROW(qrcode.decode(src, corners, straight_barcode));
}
TEST(Objdetect_QRCode_detect_flipped, regression_23249)
{
const std::vector<std::pair<std::string, std::string>> flipped_images =
// image name , expected result
{{"flipped_1.png", "The key is /qrcod_OMevpf"},
{"flipped_2.png", "A26"}};
const std::string root = "qrcode/flipped/";
for(const auto &flipped_image : flipped_images){
const std::string &image_name = flipped_image.first;
std::string image_path = findDataFile(root + image_name);
Mat src = imread(image_path);
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
QRCodeDetector qrcode;
std::vector<Point> corners;
Mat straight_barcode;
cv::String decoded_info;
EXPECT_TRUE(qrcode.detect(src, corners));
EXPECT_TRUE(!corners.empty());
std::string decoded_msg;
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);
}
}
// @author Kumataro, https://github.com/Kumataro
TEST(Objdetect_QRCode_decode, decode_regression_21929)
{
const cv::String expect_msg = "OpenCV";
Mat qrImg;
QRCodeEncoder::Params params;
params.version = 8; // 49x49
Ptr<QRCodeEncoder> qrcode_enc = cv::QRCodeEncoder::create(params);
qrcode_enc->encode(expect_msg, qrImg);
Mat src;
cv::resize(qrImg, src, Size(200,200), 1.0, 1.0, INTER_NEAREST);
QRCodeDetector qrcode;
std::vector<Point> corners;
Mat straight_barcode;
EXPECT_TRUE(qrcode.detect(src, corners));
EXPECT_TRUE(!corners.empty());
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);
}
TEST(Objdetect_QRCode_decode, decode_regression_version_25)
{
const cv::String expect_msg = "OpenCV";
Mat qrImg;
QRCodeEncoder::Params params;
params.version = 25; // 117x117
Ptr<QRCodeEncoder> qrcode_enc = cv::QRCodeEncoder::create(params);
qrcode_enc->encode(expect_msg, qrImg);
Mat src;
cv::resize(qrImg, src, qrImg.size()*3, 1.0, 1.0, INTER_NEAREST);
QRCodeDetector qrcode;
std::vector<Point> corners;
Mat straight_barcode;
EXPECT_TRUE(qrcode.detect(src, corners));
EXPECT_TRUE(!corners.empty());
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);
}
TEST(Objdetect_QRCode_decode, decode_alphanumeric_out_of_range)
{
// Version 1 QR with a valid Reed-Solomon block whose alphanumeric segment
// carries an 11-bit pair value of 2047. decodeAlpha computes 2047 / 45 == 45
// and used to read map[45], one past the 45-entry table, leaking an adjacent
// byte into the decoded string. The decoder must reject it and return an
// empty string instead.
static const char* modules[21] = {
"000000011011010000000",
"011111010011010111110",
"010001011011010100010",
"010001010011010100010",
"010001011011010100010",
"011111010011010111110",
"000000010101010000000",
"111111111011011111111",
"000001000011001010101",
"111111101001011011000",
"000111010001011011000",
"001001100111011011000",
"000001011011011011000",
"111111110101011011000",
"000000010111011011001",
"011111011101011011001",
"010001010001011011011",
"010001010001011011011",
"010001010001011011011",
"011111010001011011010",
"000000010001011011011"
};
Mat qr(21, 21, CV_8UC1);
for (int i = 0; i < 21; i++)
for (int j = 0; j < 21; j++)
qr.at<uchar>(i, j) = modules[i][j] == '1' ? 255 : 0;
Mat src;
cv::resize(qr, src, qr.size() * 10, 0, 0, INTER_NEAREST);
cv::copyMakeBorder(src, src, 40, 40, 40, 40, BORDER_CONSTANT, Scalar(255));
QRCodeDetector qrcode;
std::vector<Point> corners;
ASSERT_TRUE(qrcode.detect(src, corners));
Mat straight_barcode;
std::string decoded_info;
EXPECT_NO_THROW(decoded_info = qrcode.decode(src, corners, straight_barcode));
EXPECT_TRUE(decoded_info.empty());
}
TEST_P(Objdetect_QRCode_detectAndDecodeMulti, decode_9_qrcodes_version7)
{
const std::string name_current_image = "9_qrcodes_version7.jpg";
const std::string root = "qrcode/multiple/";
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path);
const std::string method = GetParam();
GraphicalCodeDetector qrcode = QRCodeDetector();
if (method == "aruco_based") {
qrcode = QRCodeDetectorAruco();
}
std::vector<Point> corners;
std::vector<cv::String> decoded_info;
std::vector<Mat1b> straight_barcode;
qrcode.detectAndDecodeMulti(src, decoded_info, corners, straight_barcode);
EXPECT_EQ(9ull, decoded_info.size());
const string gold_info = "I love OpenCV, QR Code version = 7, error correction = level Quartile";
for (const auto& info : decoded_info) {
EXPECT_EQ(info, gold_info);
}
}
#endif // UPDATE_QRCODE_TEST_DATA
TEST(Objdetect_QRCode_detectAndDecode, utf8_output)
{
const std::string name_current_image = "umlaut.png";
const std::string root = "qrcode/";
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path);
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
QRCodeDetector qrcode;
std::vector<Point> corners;
Mat straight;
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);
}
TEST_P(Objdetect_QRCode_detectAndDecodeMulti, detect_regression_24679)
{
const std::string name_current_image = "issue_24679.png";
const std::string root = "qrcode/";
std::string image_path = findDataFile(root + name_current_image);
Mat img = imread(image_path);
const std::string method = GetParam();
GraphicalCodeDetector qrcode = QRCodeDetector();
if (method == "aruco_based") {
qrcode = QRCodeDetectorAruco();
}
std::vector<cv::String> decoded_info;
ASSERT_TRUE(qrcode.detectAndDecodeMulti(img, decoded_info));
EXPECT_EQ(decoded_info.size(), 4U);
}
TEST_P(Objdetect_QRCode_detectAndDecodeMulti, detect_regression_24011)
{
const std::string name_current_image = "issue_24011.jpg";
const std::string root = "qrcode/";
std::string image_path = findDataFile(root + name_current_image);
Mat img = imread(image_path);
const std::string method = GetParam();
GraphicalCodeDetector qrcode = QRCodeDetector();
if (method == "aruco_based") {
qrcode = QRCodeDetectorAruco();
}
std::vector<cv::String> decoded_info;
ASSERT_TRUE(qrcode.detectAndDecodeMulti(img, decoded_info));
EXPECT_EQ(decoded_info.size(), 2U);
}
TEST(Objdetect_QRCode_detect, detect_regression_24450)
{
const std::string name_current_image = "issue_24450.png";
const std::string root = "qrcode/";
std::string image_path = findDataFile(root + name_current_image);
Mat img = imread(image_path);
GraphicalCodeDetector qrcode = QRCodeDetector();
std::vector<Point2f> points;
ASSERT_TRUE(qrcode.detect(img, points));
EXPECT_EQ(points.size(), 4U);
img.at<Vec3b>(img.rows - 1, 296) = {};
ASSERT_TRUE(qrcode.detect(img, points));
EXPECT_EQ(points.size(), 4U);
}
TEST(Objdetect_QRCode_detect, detect_regression_22892)
{
const std::string name_current_image = "issue_22892.png";
const std::string root = "qrcode/";
std::string image_path = findDataFile(root + name_current_image);
Mat img = imread(image_path);
QRCodeDetector qrcode;
std::vector<Point> corners;
Mat straight_code;
qrcode.detectAndDecodeCurved(img, corners, straight_code);
EXPECT_EQ(corners.size(), 4U);
}
// See https://github.com/opencv/opencv/issues/27783
TEST(Objdetect_QRCode_detect, detect_regression_27783)
{
const std::string name_current_image = "9_qrcodes.jpg";
const std::string root = "qrcode/multiple/";
std::string image_path = findDataFile(root + name_current_image);
Mat src = imread(image_path);
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
std::vector<std::string> info;
std::vector<Point> corners;
{
// If default, we can decode 9 QRs.
QRCodeDetector qrcode;
EXPECT_TRUE(qrcode.detectAndDecodeMulti(src, info, corners));
ASSERT_EQ(info.size(), 9UL);
ASSERT_EQ(corners.size(), 36UL);
}
{
// If setEpsX is too small, we can decode no QRs.
QRCodeDetector qrcode;
qrcode.setEpsX(0.01);
EXPECT_FALSE(qrcode.detectAndDecodeMulti(src, info, corners));
}
{
// If setEpsY is too small, we can decode no QRs.
QRCodeDetector qrcode;
qrcode.setEpsY(0.01);
EXPECT_FALSE(qrcode.detectAndDecodeMulti(src, info, corners));
}
}
}} // namespace
@@ -0,0 +1,630 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
namespace opencv_test { namespace {
std::string encode_qrcode_images_name[] = {
"version1_mode1.png", "version1_mode2.png", "version1_mode4.png",
"version2_mode1.png", "version2_mode2.png", "version2_mode4.png",
"version3_mode2.png", "version3_mode4.png",
"version4_mode4.png"
};
std::string encode_qrcode_eci_images_name[] = {
"version1_mode7.png",
"version2_mode7.png",
"version3_mode7.png",
"version4_mode7.png",
"version5_mode7.png"
};
const Size fixed_size = Size(200, 200);
const float border_width = 2.0;
int establishCapacity(QRCodeEncoder::EncodeMode mode, int version, int capacity)
{
int result = 0;
capacity *= 8;
capacity -= 4;
switch (mode)
{
case QRCodeEncoder::MODE_NUMERIC:
{
if (version >= 10)
capacity -= 12;
else
capacity -= 10;
int tmp = capacity / 10;
result = tmp * 3;
if (tmp * 10 + 7 <= capacity)
result += 2;
else if (tmp * 10 + 4 <= capacity)
result += 1;
break;
}
case QRCodeEncoder::MODE_ALPHANUMERIC:
{
if (version < 10)
capacity -= 9;
else
capacity -= 13;
int tmp = capacity / 11;
result = tmp * 2;
if (tmp * 11 + 6 <= capacity)
result++;
break;
}
case QRCodeEncoder::MODE_BYTE:
{
if (version > 9)
capacity -= 16;
else
capacity -= 8;
result = capacity / 8;
break;
}
default:
break;
}
return result;
}
// #define UPDATE_TEST_DATA
#ifdef UPDATE_TEST_DATA
TEST(Objdetect_QRCode_Encode, generate_test_data)
{
const std::string root = "qrcode/encode";
const std::string dataset_config = findDataFile(root + "/" + "dataset_config.json");
FileStorage file_config(dataset_config, FileStorage::WRITE);
file_config << "test_images" << "[";
size_t images_count = sizeof(encode_qrcode_images_name) / sizeof(encode_qrcode_images_name[0]);
for (size_t i = 0; i < images_count; i++)
{
file_config << "{:" << "image_name" << encode_qrcode_images_name[i];
std::string image_path = findDataFile(root + "/" + encode_qrcode_images_name[i]);
Mat src = imread(image_path, IMREAD_GRAYSCALE);
Mat straight_barcode;
EXPECT_TRUE(!src.empty()) << "Can't read image: " << image_path;
std::vector<Point2f> corners(4);
corners[0] = Point2f(border_width, border_width);
corners[1] = Point2f(qrcode.cols * 1.0f - border_width, border_width);
corners[2] = Point2f(qrcode.cols * 1.0f - border_width, qrcode.rows * 1.0f - border_width);
corners[3] = Point2f(border_width, qrcode.rows * 1.0f - border_width);
Mat resized_src;
resize(qrcode, resized_src, fixed_size, 0, 0, INTER_AREA);
float width_ratio = resized_src.cols * 1.0f / qrcode.cols;
float height_ratio = resized_src.rows * 1.0f / qrcode.rows;
for(size_t j = 0; j < corners.size(); j++)
{
corners[j].x = corners[j].x * width_ratio;
corners[j].y = corners[j].y * height_ratio;
}
std::string decoded_info = "";
EXPECT_TRUE(decodeQRCode(resized_src, corners, decoded_info, straight_barcode)) << "The QR code cannot be decoded: " << image_path;
file_config << "info" << decoded_info;
file_config << "}";
}
file_config << "]";
file_config.release();
}
#else
typedef testing::TestWithParam< std::string > Objdetect_QRCode_Encode;
TEST_P(Objdetect_QRCode_Encode, regression) {
const int pixels_error = 3;
const std::string name_current_image = GetParam();
const std::string root = "qrcode/encode";
std::string image_path = findDataFile(root + "/" + name_current_image);
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;
{
FileNode images_list = file_config["test_images"];
size_t images_count = static_cast<size_t>(images_list.size());
ASSERT_GT(images_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
for (size_t index = 0; index < images_count; index++)
{
FileNode config = images_list[(int)index];
std::string name_test_image = config["image_name"];
if (name_test_image == name_current_image)
{
std::string original_info = config["info"];
Ptr<QRCodeEncoder> encoder = QRCodeEncoder::create();
Mat result;
encoder->encode(original_info, result);
EXPECT_FALSE(result.empty()) << "Can't generate QR code image";
Mat src = imread(image_path, IMREAD_GRAYSCALE);
Mat straight_barcode;
EXPECT_TRUE(!src.empty()) << "Can't read image: " << image_path;
double diff_norm = cvtest::norm(result - src, NORM_L1);
EXPECT_NEAR(diff_norm, 0.0, pixels_error) << "The generated QRcode is not same as test data. The difference: " << diff_norm;
return; // done
}
}
FAIL() << "Not found results in config file:" << dataset_config
<< "\nRe-run tests with enabled UPDATE_ENCODE_TEST_DATA macro to update test data.";
}
}
typedef testing::TestWithParam< std::string > Objdetect_QRCode_Encode_ECI;
TEST_P(Objdetect_QRCode_Encode_ECI, regression) {
const int pixels_error = 3;
const std::string name_current_image = GetParam();
const std::string root = "qrcode/encode";
std::string image_path = findDataFile(root + "/" + name_current_image);
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;
{
FileNode images_list = file_config["test_images"];
size_t images_count = static_cast<size_t>(images_list.size());
ASSERT_GT(images_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
QRCodeEncoder::Params params;
params.mode = QRCodeEncoder::MODE_ECI;
for (size_t index = 0; index < images_count; index++)
{
FileNode config = images_list[(int)index];
std::string name_test_image = config["image_name"];
if (name_test_image == name_current_image)
{
std::string original_info = config["info"];
Mat result;
Ptr<QRCodeEncoder> encoder = QRCodeEncoder::create(params);
encoder->encode(original_info, result);
EXPECT_FALSE(result.empty()) << "Can't generate QR code image";
Mat src = imread(image_path, IMREAD_GRAYSCALE);
Mat straight_barcode;
EXPECT_TRUE(!src.empty()) << "Can't read image: " << image_path;
double diff_norm = cvtest::norm(result - src, NORM_L1);
EXPECT_NEAR(diff_norm, 0.0, pixels_error) << "The generated QRcode is not same as test data. The difference: " << diff_norm;
return; // done
}
}
FAIL() << "Not found results in config file:" << dataset_config
<< "\nRe-run tests with enabled UPDATE_ENCODE_TEST_DATA macro to update test data.";
}
}
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Encode, testing::ValuesIn(encode_qrcode_images_name));
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Encode_ECI, testing::ValuesIn(encode_qrcode_eci_images_name));
TEST(Objdetect_QRCode_Encode_Decode, regression)
{
const std::string root = "qrcode/decode_encode";
const int min_version = 1;
const int test_max_version = 5;
const int max_ec_level = 3;
const std::string dataset_config = findDataFile(root + "/" + "symbol_sets.json");
const std::string version_config = findDataFile(root + "/" + "capacity.json");
FileStorage file_config(dataset_config, FileStorage::READ);
FileStorage capacity_config(version_config, FileStorage::READ);
ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
ASSERT_TRUE(capacity_config.isOpened()) << "Can't read validation data: " << version_config;
FileNode mode_list = file_config["symbols_sets"];
FileNode capacity_list = capacity_config["version_ecc_capacity"];
size_t mode_count = static_cast<size_t>(mode_list.size());
ASSERT_GT(mode_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
const int testing_modes = 3;
QRCodeEncoder::EncodeMode modes[testing_modes] = {
QRCodeEncoder::MODE_NUMERIC,
QRCodeEncoder::MODE_ALPHANUMERIC,
QRCodeEncoder::MODE_BYTE
};
for (int i = 0; i < testing_modes; i++)
{
QRCodeEncoder::EncodeMode mode = modes[i];
FileNode config = mode_list[i];
std::string symbol_set = config["symbols_set"];
for(int version = min_version; version <= test_max_version; version++)
{
FileNode capa_config = capacity_list[version - 1];
for(int level = 0; level <= max_ec_level; level++)
{
const int cur_capacity = capa_config["ecc_level"][level];
int true_capacity = establishCapacity(mode, version, cur_capacity);
std::string input_info = symbol_set;
std::mt19937 rand_gen {1};
std::shuffle(input_info.begin(), input_info.end(), rand_gen);
int count = 0;
if((int)input_info.length() > true_capacity)
{
input_info = input_info.substr(0, true_capacity);
}
else
{
while ((int)input_info.length() != true_capacity)
{
input_info += input_info.substr(count%(int)input_info.length(), 1);
count++;
}
}
QRCodeEncoder::Params params;
params.version = version;
params.correction_level = static_cast<QRCodeEncoder::CorrectionLevel>(level);
params.mode = mode;
Ptr<QRCodeEncoder> encoder = QRCodeEncoder::create(params);
Mat qrcode;
encoder->encode(input_info, qrcode);
EXPECT_TRUE(!qrcode.empty()) << "Can't generate this QR image (" << "mode: " << (int)mode <<
" version: "<< version <<" error correction level: "<< (int)level <<")";
std::vector<Point2f> corners(4);
corners[0] = Point2f(border_width, border_width);
corners[1] = Point2f(qrcode.cols * 1.0f - border_width, border_width);
corners[2] = Point2f(qrcode.cols * 1.0f - border_width, qrcode.rows * 1.0f - border_width);
corners[3] = Point2f(border_width, qrcode.rows * 1.0f - border_width);
Mat resized_src;
resize(qrcode, resized_src, fixed_size, 0, 0, INTER_AREA);
float width_ratio = resized_src.cols * 1.0f / qrcode.cols;
float height_ratio = resized_src.rows * 1.0f / qrcode.rows;
for(size_t k = 0; k < corners.size(); k++)
{
corners[k].x = corners[k].x * width_ratio;
corners[k].y = corners[k].y * height_ratio;
}
Mat straight_barcode;
std::string output_info = QRCodeDetector().decode(resized_src, corners, straight_barcode);
EXPECT_FALSE(output_info.empty())
<< "The generated QRcode cannot be decoded." << " Mode: " << (int)mode
<< " 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;
}
}
}
}
TEST(Objdetect_QRCode_Encode_Kanji, regression)
{
QRCodeEncoder::Params params;
params.mode = QRCodeEncoder::MODE_KANJI;
Mat qrcode;
const int testing_versions = 3;
std::string input_infos[testing_versions] = {"\x82\xb1\x82\xf1\x82\xc9\x82\xbf\x82\xcd\x90\xa2\x8a\x45", // "Hello World" in Japanese
"\x82\xa8\x95\xa0\x82\xaa\x8b\xf3\x82\xa2\x82\xc4\x82\xa2\x82\xdc\x82\xb7", // "I am hungry" in Japanese
"\x82\xb1\x82\xf1\x82\xc9\x82\xbf\x82\xcd\x81\x41\x8e\x84\x82\xcd\x8f\xad\x82\xb5\x93\xfa\x96\x7b\x8c\xea\x82\xf0\x98\x62\x82\xb5\x82\xdc\x82\xb7" // "Hello, I speak a little Japanese" in Japanese
};
for (int i = 0; i < testing_versions; i++)
{
std::string input_info = input_infos[i];
Ptr<QRCodeEncoder> encoder = QRCodeEncoder::create(params);
encoder->encode(input_info, qrcode);
std::vector<Point2f> corners(4);
corners[0] = Point2f(border_width, border_width);
corners[1] = Point2f(qrcode.cols * 1.0f - border_width, border_width);
corners[2] = Point2f(qrcode.cols * 1.0f - border_width, qrcode.rows * 1.0f - border_width);
corners[3] = Point2f(border_width, qrcode.rows * 1.0f - border_width);
Mat resized_src;
resize(qrcode, resized_src, fixed_size, 0, 0, INTER_AREA);
float width_ratio = resized_src.cols * 1.0f / qrcode.cols;
float height_ratio = resized_src.rows * 1.0f / qrcode.rows;
for(size_t j = 0; j < corners.size(); j++)
{
corners[j].x = corners[j].x * width_ratio;
corners[j].y = corners[j].y * height_ratio;
}
Mat straight_barcode;
QRCodeDetector detector;
std::string decoded_info = detector.decode(resized_src, corners, straight_barcode);
EXPECT_FALSE(decoded_info.empty()) << "The generated QRcode cannot be decoded.";
EXPECT_EQ(input_info, decoded_info);
EXPECT_EQ(detector.getEncoding(), QRCodeEncoder::ECIEncodings::ECI_SHIFT_JIS);
}
}
TEST(Objdetect_QRCode_Encode_Decode_Structured_Append, regression)
{
// disabled since QR decoder probably doesn't support structured append mode qr codes
const std::string root = "qrcode/decode_encode";
const std::string dataset_config = findDataFile(root + "/" + "symbol_sets.json");
const std::string version_config = findDataFile(root + "/" + "capacity.json");
FileStorage file_config(dataset_config, FileStorage::READ);
ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;
FileNode mode_list = file_config["symbols_sets"];
size_t mode_count = static_cast<size_t>(mode_list.size());
ASSERT_GT(mode_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;
int modes[] = {1, 2, 4};
const int min_stuctures_num = 2;
const int max_stuctures_num = 5;
for (int i = 0; i < 3; i++)
{
int mode = modes[i];
FileNode config = mode_list[i];
std::string symbol_set = config["symbols_set"];
std::string input_info = symbol_set;
std::mt19937 rand_gen {1};
std::shuffle(input_info.begin(), input_info.end(), rand_gen);
for (int j = min_stuctures_num; j < max_stuctures_num; j++)
{
QRCodeEncoder::Params params;
params.structure_number = j;
Ptr<QRCodeEncoder> encoder = QRCodeEncoder::create(params);
vector<Mat> qrcodes;
encoder->encodeStructuredAppend(input_info, qrcodes);
EXPECT_TRUE(!qrcodes.empty()) << "Can't generate this QR images";
CV_CheckEQ(qrcodes.size(), (size_t)j, "Number of QR codes");
std::vector<Point2f> corners(4 * qrcodes.size());
for (size_t k = 0; k < qrcodes.size(); k++)
{
Mat qrcode = qrcodes[k];
corners[4 * k] = Point2f(border_width, border_width);
corners[4 * k + 1] = Point2f(qrcode.cols * 1.0f - border_width, border_width);
corners[4 * k + 2] = Point2f(qrcode.cols * 1.0f - border_width, qrcode.rows * 1.0f - border_width);
corners[4 * k + 3] = Point2f(border_width, qrcode.rows * 1.0f - border_width);
float width_ratio = fixed_size.width * 1.0f / qrcode.cols;
float height_ratio = fixed_size.height * 1.0f / qrcode.rows;
resize(qrcode, qrcodes[k], fixed_size, 0, 0, INTER_AREA);
for (size_t ki = 0; ki < 4; ki++)
{
corners[4 * k + ki].x = corners[4 * k + ki].x * width_ratio + fixed_size.width * k;
corners[4 * k + ki].y = corners[4 * k + ki].y * height_ratio;
}
}
Mat resized_src;
hconcat(qrcodes, resized_src);
std::vector<cv::String> decoded_info;
cv::String output_info;
EXPECT_TRUE(QRCodeDetector().decodeMulti(resized_src, corners, decoded_info));
for (size_t k = 0; k < decoded_info.size(); ++k)
{
if (!decoded_info[k].empty())
output_info = decoded_info[k];
}
EXPECT_FALSE(output_info.empty())
<< "The generated QRcode cannot be decoded." << " Mode: " << modes[i]
<< " structures number: " << j;
EXPECT_EQ(input_info, output_info) << "The generated QRcode is not same as test data." << " Mode: " << mode <<
" structures number: " << j;
}
}
}
#endif // UPDATE_QRCODE_TEST_DATA
CV_ENUM(EncodeModes, QRCodeEncoder::EncodeMode::MODE_NUMERIC,
QRCodeEncoder::EncodeMode::MODE_ALPHANUMERIC,
QRCodeEncoder::EncodeMode::MODE_BYTE)
typedef ::testing::TestWithParam<EncodeModes> Objdetect_QRCode_Encode_Decode_Structured_Append_Parameterized;
TEST_P(Objdetect_QRCode_Encode_Decode_Structured_Append_Parameterized, regression_22205)
{
const std::string input_data = "the quick brown fox jumps over the lazy dog";
std::vector<cv::Mat> result_qrcodes;
cv::QRCodeEncoder::Params params;
int encode_mode = GetParam();
params.mode = static_cast<cv::QRCodeEncoder::EncodeMode>(encode_mode);
for(size_t struct_num = 2; struct_num < 5; ++struct_num)
{
params.structure_number = static_cast<int>(struct_num);
cv::Ptr<cv::QRCodeEncoder> encoder = cv::QRCodeEncoder::create(params);
encoder->encodeStructuredAppend(input_data, result_qrcodes);
EXPECT_EQ(result_qrcodes.size(), struct_num) << "The number of QR Codes requested is not equal"<<
"to the one returned";
}
}
INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode_Encode_Decode_Structured_Append_Parameterized, EncodeModes::all());
TEST(Objdetect_QRCode_Encode_Decode, regression_issue22029)
{
const cv::String msg = "OpenCV";
const int min_version = 1;
const int max_version = 40;
for ( int v = min_version ; v <= max_version ; v++ )
{
SCOPED_TRACE(cv::format("version=%d",v));
Mat qrimg;
QRCodeEncoder::Params params;
params.version = v;
Ptr<QRCodeEncoder> qrcode_enc = cv::QRCodeEncoder::create(params);
qrcode_enc->encode(msg, qrimg);
const int white_margin = 2;
const int finder_width = 7;
const int timing_pos = white_margin + 6;
int i;
// Horizontal Check
// (1) White margin(Left)
for(i = 0; i < white_margin ; i++ )
{
ASSERT_EQ((uint8_t)255, qrimg.at<uint8_t>(i, timing_pos)) << "i=" << i;
}
// (2) Finder pattern(Left)
for( ; i < white_margin + finder_width ; i++ )
{
ASSERT_EQ((uint8_t)0, qrimg.at<uint8_t>(i, timing_pos)) << "i=" << i;
}
// (3) Timing pattern
for( ; i < qrimg.rows - finder_width - white_margin; i++ )
{
ASSERT_EQ((uint8_t)(i % 2 == 0)?0:255, qrimg.at<uint8_t>(i, timing_pos)) << "i=" << i;
}
// (4) Finder pattern(Right)
for( ; i < qrimg.rows - white_margin; i++ )
{
ASSERT_EQ((uint8_t)0, qrimg.at<uint8_t>(i, timing_pos)) << "i=" << i;
}
// (5) White margin(Right)
for( ; i < qrimg.rows ; i++ )
{
ASSERT_EQ((uint8_t)255, qrimg.at<uint8_t>(i, timing_pos)) << "i=" << i;
}
// Vertical Check
// (1) White margin(Top)
for(i = 0; i < white_margin ; i++ )
{
ASSERT_EQ((uint8_t)255, qrimg.at<uint8_t>(timing_pos, i)) << "i=" << i;
}
// (2) Finder pattern(Top)
for( ; i < white_margin + finder_width ; i++ )
{
ASSERT_EQ((uint8_t)0, qrimg.at<uint8_t>(timing_pos, i)) << "i=" << i;
}
// (3) Timing pattern
for( ; i < qrimg.rows - finder_width - white_margin; i++ )
{
ASSERT_EQ((uint8_t)(i % 2 == 0)?0:255, qrimg.at<uint8_t>(timing_pos, i)) << "i=" << i;
}
// (4) Finder pattern(Bottom)
for( ; i < qrimg.rows - white_margin; i++ )
{
ASSERT_EQ((uint8_t)0, qrimg.at<uint8_t>(timing_pos, i)) << "i=" << i;
}
// (5) White margin(Bottom)
for( ; i < qrimg.rows ; i++ )
{
ASSERT_EQ((uint8_t)255, qrimg.at<uint8_t>(timing_pos, i)) << "i=" << i;
}
}
}
// 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"},
}));
TEST(Objdetect_QRCode_Encode_Decode_Long_Text, regression_issue27183)
{
const int len = 135;
Ptr<QRCodeEncoder> encoder = QRCodeEncoder::create();
std::string input;
input.resize(len);
cv::randu(Mat(1, len, CV_8U, &input[0]), 'a', 'z' + 1);
Mat qrcode;
encoder->encode(input, qrcode);
std::vector<Point2f> corners(4);
corners[0] = Point2f(border_width, border_width);
corners[1] = Point2f(qrcode.cols * 1.0f - border_width, border_width);
corners[2] = Point2f(qrcode.cols * 1.0f - border_width, qrcode.rows * 1.0f - border_width);
corners[3] = Point2f(border_width, qrcode.rows * 1.0f - border_width);
Mat resized_src;
resize(qrcode, resized_src, fixed_size, 0, 0, INTER_AREA);
float width_ratio = resized_src.cols * 1.0f / qrcode.cols;
float height_ratio = resized_src.rows * 1.0f / qrcode.rows;
for(size_t j = 0; j < corners.size(); j++)
{
corners[j].x = corners[j].x * width_ratio;
corners[j].y = corners[j].y * height_ratio;
}
QRCodeDetector detector;
cv::String decoded_msg;
Mat straight_barcode;
EXPECT_NO_THROW(decoded_msg = detector.decode(resized_src, corners, straight_barcode));
ASSERT_FALSE(straight_barcode.empty());
EXPECT_EQ(input, decoded_msg);
}
}} // namespace