Skip to content

Commit 0be18f5

Browse files
rayonnant14alalek
andauthored
Merge pull request opencv#19407 from rayonnant14:issue_19363
QRCodeDetector::decodeMulti() fixed invalid usage fixedType() * fixed invalid usage fixedType() changed default barcode type to CV_8UC1 added tests added assert in case multi channel straight barcode input * deleted extra wrap into OutputArray * fix warnings * objdetect(qr): remove unnecessary checks Co-authored-by: Alexander Alekhin <alexander.a.alekhin@gmail.com>
1 parent 4f08bb5 commit 0be18f5

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

modules/objdetect/src/qrcode.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,12 +2491,13 @@ cv::String QRCodeDetector::decode(InputArray in, InputArray points,
24912491
bool ok = qrdec.straightDecodingProcess();
24922492

24932493
std::string decoded_info = qrdec.getDecodeInformation();
2494-
2495-
if (ok && straight_qrcode.needed())
2494+
if (!ok && straight_qrcode.needed())
2495+
{
2496+
straight_qrcode.release();
2497+
}
2498+
else if (straight_qrcode.needed())
24962499
{
2497-
qrdec.getStraightBarcode().convertTo(straight_qrcode,
2498-
straight_qrcode.fixedType() ?
2499-
straight_qrcode.type() : CV_32FC2);
2500+
qrdec.getStraightBarcode().convertTo(straight_qrcode, CV_8UC1);
25002501
}
25012502

25022503
return ok ? decoded_info : std::string();
@@ -2520,11 +2521,13 @@ cv::String QRCodeDetector::decodeCurved(InputArray in, InputArray points,
25202521

25212522
std::string decoded_info = qrdec.getDecodeInformation();
25222523

2523-
if (ok && straight_qrcode.needed())
2524+
if (!ok && straight_qrcode.needed())
25242525
{
2525-
qrdec.getStraightBarcode().convertTo(straight_qrcode,
2526-
straight_qrcode.fixedType() ?
2527-
straight_qrcode.type() : CV_32FC2);
2526+
straight_qrcode.release();
2527+
}
2528+
else if (straight_qrcode.needed())
2529+
{
2530+
qrdec.getStraightBarcode().convertTo(straight_qrcode, CV_8UC1);
25282531
}
25292532

25302533
return ok ? decoded_info : std::string();
@@ -3615,18 +3618,18 @@ bool QRCodeDetector::decodeMulti(
36153618
for_copy.push_back(straight_barcode[i]);
36163619
}
36173620
straight_barcode = for_copy;
3618-
vector<Mat> tmp_straight_qrcodes;
3619-
if (straight_qrcode.needed())
3621+
if (straight_qrcode.needed() && straight_barcode.size() == 0)
3622+
{
3623+
straight_qrcode.release();
3624+
}
3625+
else if (straight_qrcode.needed())
36203626
{
3627+
straight_qrcode.create(Size((int)straight_barcode.size(), 1), CV_8UC1);
3628+
vector<Mat> tmp_straight_qrcodes(straight_barcode.size());
36213629
for (size_t i = 0; i < straight_barcode.size(); i++)
36223630
{
3623-
Mat tmp_straight_qrcode;
3624-
tmp_straight_qrcodes.push_back(tmp_straight_qrcode);
3625-
straight_barcode[i].convertTo(((OutputArray)tmp_straight_qrcodes[i]),
3626-
((OutputArray)tmp_straight_qrcodes[i]).fixedType() ?
3627-
((OutputArray)tmp_straight_qrcodes[i]).type() : CV_32FC2);
3631+
straight_barcode[i].convertTo(tmp_straight_qrcodes[i], CV_8UC1);
36283632
}
3629-
straight_qrcode.createSameSize(tmp_straight_qrcodes, CV_32FC2);
36303633
straight_qrcode.assign(tmp_straight_qrcodes);
36313634
}
36323635
decoded_info.clear();

modules/objdetect/test/test_qrcode.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ TEST_P(Objdetect_QRCode, regression)
252252
decoded_info = qrcode.detectAndDecode(src, corners, straight_barcode);
253253
ASSERT_FALSE(corners.empty());
254254
ASSERT_FALSE(decoded_info.empty());
255+
int expected_barcode_type = CV_8UC1;
256+
EXPECT_EQ(expected_barcode_type, straight_barcode.type());
255257
#else
256258
ASSERT_TRUE(qrcode.detect(src, corners));
257259
#endif
@@ -317,6 +319,8 @@ TEST_P(Objdetect_QRCode_Close, regression)
317319
decoded_info = qrcode.detectAndDecode(barcode, corners, straight_barcode);
318320
ASSERT_FALSE(corners.empty());
319321
ASSERT_FALSE(decoded_info.empty());
322+
int expected_barcode_type = CV_8UC1;
323+
EXPECT_EQ(expected_barcode_type, straight_barcode.type());
320324
#else
321325
ASSERT_TRUE(qrcode.detect(barcode, corners));
322326
#endif
@@ -382,6 +386,8 @@ TEST_P(Objdetect_QRCode_Monitor, regression)
382386
decoded_info = qrcode.detectAndDecode(barcode, corners, straight_barcode);
383387
ASSERT_FALSE(corners.empty());
384388
ASSERT_FALSE(decoded_info.empty());
389+
int expected_barcode_type = CV_8UC1;
390+
EXPECT_EQ(expected_barcode_type, straight_barcode.type());
385391
#else
386392
ASSERT_TRUE(qrcode.detect(barcode, corners));
387393
#endif
@@ -442,6 +448,8 @@ TEST_P(Objdetect_QRCode_Curved, regression)
442448
decoded_info = qrcode.detectAndDecodeCurved(src, corners, straight_barcode);
443449
ASSERT_FALSE(corners.empty());
444450
ASSERT_FALSE(decoded_info.empty());
451+
int expected_barcode_type = CV_8UC1;
452+
EXPECT_EQ(expected_barcode_type, straight_barcode.type());
445453
#else
446454
ASSERT_TRUE(qrcode.detect(src, corners));
447455
#endif
@@ -502,6 +510,9 @@ TEST_P(Objdetect_QRCode_Multi, regression)
502510
EXPECT_TRUE(qrcode.detectAndDecodeMulti(src, decoded_info, corners, straight_barcode));
503511
ASSERT_FALSE(corners.empty());
504512
ASSERT_FALSE(decoded_info.empty());
513+
int expected_barcode_type = CV_8UC1;
514+
for(size_t i = 0; i < straight_barcode.size(); i++)
515+
EXPECT_EQ(expected_barcode_type, straight_barcode[i].type());
505516
#else
506517
ASSERT_TRUE(qrcode.detectMulti(src, corners));
507518
#endif
@@ -612,6 +623,32 @@ TEST(Objdetect_QRCode_detectMulti, detect_regression_16961)
612623
EXPECT_EQ(corners.size(), expect_corners_size);
613624
}
614625

626+
TEST(Objdetect_QRCode_decodeMulti, check_output_parameters_type_19363)
627+
{
628+
const std::string name_current_image = "9_qrcodes.jpg";
629+
const std::string root = "qrcode/multiple/";
630+
631+
std::string image_path = findDataFile(root + name_current_image);
632+
Mat src = imread(image_path);
633+
ASSERT_FALSE(src.empty()) << "Can't read image: " << image_path;
634+
#ifdef HAVE_QUIRC
635+
QRCodeDetector qrcode;
636+
std::vector<Point> corners;
637+
std::vector<cv::String> decoded_info;
638+
#if 0 // FIXIT: OutputArray::create() type check
639+
std::vector<Mat2b> straight_barcode_nchannels;
640+
EXPECT_ANY_THROW(qrcode.detectAndDecodeMulti(src, decoded_info, corners, straight_barcode_nchannels));
641+
#endif
642+
643+
int expected_barcode_type = CV_8UC1;
644+
std::vector<Mat1b> straight_barcode;
645+
EXPECT_TRUE(qrcode.detectAndDecodeMulti(src, decoded_info, corners, straight_barcode));
646+
ASSERT_FALSE(corners.empty());
647+
for(size_t i = 0; i < straight_barcode.size(); i++)
648+
EXPECT_EQ(expected_barcode_type, straight_barcode[i].type());
649+
#endif
650+
}
651+
615652
TEST(Objdetect_QRCode_basic, not_found_qrcode)
616653
{
617654
std::vector<Point> corners;

0 commit comments

Comments
 (0)