Path: blob/master/modules/objdetect/test/test_qrcode.cpp
16337 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.34#include "test_precomp.hpp"56namespace opencv_test { namespace {78std::string qrcode_images_name[] = {9"version_1_down.jpg", "version_1_left.jpg", "version_1_right.jpg", "version_1_up.jpg", "version_1_top.jpg",10"version_2_down.jpg", "version_2_left.jpg", "version_2_right.jpg", "version_2_up.jpg", "version_2_top.jpg",11"version_3_down.jpg", "version_3_left.jpg", "version_3_right.jpg", "version_3_up.jpg", "version_3_top.jpg",12"version_4_down.jpg", "version_4_left.jpg", "version_4_right.jpg", "version_4_up.jpg", "version_4_top.jpg",13"version_5_down.jpg", "version_5_left.jpg", "version_5_right.jpg", "version_5_up.jpg", "version_5_top.jpg",14"russian.jpg", "kanji.jpg", "link_github_ocv.jpg", "link_ocv.jpg", "link_wiki_cv.jpg"15};1617// #define UPDATE_QRCODE_TEST_DATA18#ifdef UPDATE_QRCODE_TEST_DATA1920TEST(Objdetect_QRCode, generate_test_data)21{22const std::string root = "qrcode/";23const std::string dataset_config = findDataFile(root + "dataset_config.json");24FileStorage file_config(dataset_config, FileStorage::WRITE);2526file_config << "test_images" << "[";27size_t images_count = sizeof(qrcode_images_name) / sizeof(qrcode_images_name[0]);28for (size_t i = 0; i < images_count; i++)29{30file_config << "{:" << "image_name" << qrcode_images_name[i];31std::string image_path = findDataFile(root + qrcode_images_name[i]);32std::vector<Point> corners;33Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;34std::string decoded_info;35ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;36EXPECT_TRUE(detectQRCode(src, corners));37#ifdef HAVE_QUIRC38EXPECT_TRUE(decodeQRCode(src, corners, decoded_info, straight_barcode));39#endif40file_config << "x" << "[:";41for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].x; }42file_config << "]";43file_config << "y" << "[:";44for (size_t j = 0; j < corners.size(); j++) { file_config << corners[j].y; }45file_config << "]";46file_config << "info" << decoded_info;47file_config << "}";48}49file_config << "]";50file_config.release();51}5253#else5455typedef testing::TestWithParam< std::string > Objdetect_QRCode;56TEST_P(Objdetect_QRCode, regression)57{58const std::string name_current_image = GetParam();59const std::string root = "qrcode/";60const int pixels_error = 3;6162std::string image_path = findDataFile(root + name_current_image);63Mat src = imread(image_path, IMREAD_GRAYSCALE), straight_barcode;64ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;6566std::vector<Point> corners;67std::string decoded_info;68ASSERT_TRUE(detectQRCode(src, corners));69#ifdef HAVE_QUIRC70ASSERT_TRUE(decodeQRCode(src, corners, decoded_info, straight_barcode));71#endif7273const std::string dataset_config = findDataFile(root + "dataset_config.json", false);74FileStorage file_config(dataset_config, FileStorage::READ);75ASSERT_TRUE(file_config.isOpened()) << "Can't read validation data: " << dataset_config;76{77FileNode images_list = file_config["test_images"];78size_t images_count = static_cast<size_t>(images_list.size());79ASSERT_GT(images_count, 0u) << "Can't find validation data entries in 'test_images': " << dataset_config;8081for (size_t index = 0; index < images_count; index++)82{83FileNode config = images_list[(int)index];84std::string name_test_image = config["image_name"];85if (name_test_image == name_current_image)86{87for (int i = 0; i < 4; i++)88{89int x = config["x"][i];90int y = config["y"][i];91EXPECT_NEAR(x, corners[i].x, pixels_error);92EXPECT_NEAR(y, corners[i].y, pixels_error);93}9495#ifdef HAVE_QUIRC96std::string original_info = config["info"];97EXPECT_EQ(decoded_info, original_info);98#endif99100return; // done101}102}103std::cerr104<< "Not found results for '" << name_current_image105<< "' image in config file:" << dataset_config << std::endl106<< "Re-run tests with enabled UPDATE_QRCODE_TEST_DATA macro to update test data."107<< std::endl;108}109}110111INSTANTIATE_TEST_CASE_P(/**/, Objdetect_QRCode, testing::ValuesIn(qrcode_images_name));112113114115TEST(Objdetect_QRCode_basic, not_found_qrcode)116{117std::vector<Point> corners;118Mat straight_barcode;119std::string decoded_info;120Mat zero_image = Mat::zeros(256, 256, CV_8UC1);121EXPECT_FALSE(detectQRCode(zero_image, corners));122#ifdef HAVE_QUIRC123corners = std::vector<Point>(4);124EXPECT_ANY_THROW(decodeQRCode(zero_image, corners, decoded_info, straight_barcode));125#endif126}127128129130#endif // UPDATE_QRCODE_TEST_DATA131132}} // namespace133134135