Skip to content

Commit

Permalink
Merge pull request opencv#23666 from mshabunin:barcode-move
Browse files Browse the repository at this point in the history
Moved barcode from opencv_contrib opencv#23666

Merge with opencv/opencv_contrib#3497

##### TODO
- [x] Documentation (bib)
- [x] Tutorial (references)
- [x] Sample app (refactored)
- [x] Java (test passes)
- [x] Python (test passes)
- [x] Build without DNN
  • Loading branch information
mshabunin authored and thewoz committed Jan 4, 2024
1 parent b1a7ee0 commit 2e44bf7
Show file tree
Hide file tree
Showing 32 changed files with 2,904 additions and 2 deletions.
76 changes: 76 additions & 0 deletions doc/tutorials/others/barcode_detect_and_decode.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
Barcode Recognition {#tutorial_barcode_detect_and_decode}
===================

@tableofcontents

@prev_tutorial{tutorial_traincascade}
@next_tutorial{tutorial_introduction_to_svm}

| | |
| -: | :- |
| Compatibility | OpenCV >= 4.8 |

Goal
----

In this chapter we will familiarize with the barcode detection and decoding methods available in OpenCV.

Basics
----

Barcode is major technique to identify commodity in real life. A common barcode is a pattern of parallel lines arranged by black bars and white bars with vastly different reflectivity. Barcode recognition is to scan the barcode in the horizontal direction to get a string of binary codes composed of bars of different widths and colors, that is, the code information of the barcode. The content of barcode can be decoded by matching with various barcode encoding methods. Currently, we support EAN-8, EAN-13, UPC-A and UPC-E standards.

See https://en.wikipedia.org/wiki/Universal_Product_Code and https://en.wikipedia.org/wiki/International_Article_Number

Related papers: @cite Xiangmin2015research , @cite kass1987analyzing , @cite bazen2002systematic

Code example
------------

### Main class
Several algorithms were introduced for barcode recognition.

While coding, we firstly need to create a cv::barcode::BarcodeDetector object. It has mainly three member functions, which will be introduced in the following.

#### Initialization

Optionally user can construct barcode detector with super resolution model which should be downloaded from https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode (`sr.caffemodel`, `sr.prototxt`).

@snippet cpp/barcode.cpp initialize

We need to create variables to store the outputs.

@snippet cpp/barcode.cpp output

#### Detecting

cv::barcode::BarcodeDetector::detect method uses an algorithm based on directional coherence. First, we compute the average squared gradients of every pixel, @cite bazen2002systematic . Then we divide an image into square patches and compute the **gradient orientation coherence** and **mean gradient direction** of each patch. Then, we connect all patches that have **high gradient orientation coherence** and **similar gradient direction**. At this stage we use multiscale patches to capture the gradient distribution of multi-size barcodes, and apply non-maximum suppression to filter duplicate proposals. At last, we use cv::minAreaRect to bound the ROI, and output the corners of the rectangles.

Detect codes in the input image, and output the corners of detected rectangles:

@snippet cpp/barcode.cpp detect

#### Decoding

cv::barcode::BarcodeDetector::decode method first super-scales the image (_optionally_) if it is smaller than threshold, sharpens the image and then binaries it by OTSU or local binarization. Then it reads the contents of the barcode by matching the similarity of the specified barcode pattern.

#### Detecting and decoding

cv::barcode::BarcodeDetector::detectAndDecode combines `detect` and `decode` in a single call. A simple example below shows how to use this function:

@snippet cpp/barcode.cpp detectAndDecode

Visualize the results:

@snippet cpp/barcode.cpp visualize

Results
-------

Original image:

![image](images/barcode_book.jpg)

After detection:

![image](images/barcode_book_res.jpg)
Binary file added doc/tutorials/others/images/barcode_book.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/tutorials/others/images/barcode_book_res.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion doc/tutorials/others/introduction_to_svm.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Introduction to Support Vector Machines {#tutorial_introduction_to_svm}

@tableofcontents

@prev_tutorial{tutorial_traincascade}
@prev_tutorial{tutorial_barcode_detect_and_decode}
@next_tutorial{tutorial_non_linear_svms}

| | |
Expand Down
1 change: 1 addition & 0 deletions doc/tutorials/others/table_of_content_other.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Other tutorials (ml, objdetect, photo, stitching, video) {#tutorial_table_of_con
- video. @subpage tutorial_optical_flow
- objdetect. @subpage tutorial_cascade_classifier
- objdetect. @subpage tutorial_traincascade
- objdetect. @subpage tutorial_barcode_detect_and_decode
- ml. @subpage tutorial_introduction_to_svm
- ml. @subpage tutorial_non_linear_svms
- ml. @subpage tutorial_introduction_to_pca
2 changes: 1 addition & 1 deletion doc/tutorials/others/traincascade.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Cascade Classifier Training {#tutorial_traincascade}
@tableofcontents

@prev_tutorial{tutorial_cascade_classifier}
@next_tutorial{tutorial_introduction_to_svm}
@next_tutorial{tutorial_barcode_detect_and_decode}

Introduction
------------
Expand Down
29 changes: 29 additions & 0 deletions modules/objdetect/doc/objdetect.bib
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,32 @@ @inproceedings{wang2016iros
year = {2016},
month = {October}
}

@mastersthesis{Xiangmin2015research,
title={Research on Barcode Recognition Technology In a Complex Background},
author={Xiangmin, Wang},
year={2015},
school={Huazhong University of Science and Technology}
}

@article{bazen2002systematic,
title={Systematic methods for the computation of the directional fields and singular points of fingerprints},
author={Bazen, Asker M and Gerez, Sabih H},
journal={IEEE transactions on pattern analysis and machine intelligence},
volume={24},
number={7},
pages={905--919},
year={2002},
publisher={IEEE}
}

@article{kass1987analyzing,
title={Analyzing oriented patterns},
author={Kass, Michael and Witkin, Andrew},
journal={Computer vision, graphics, and image processing},
volume={37},
number={3},
pages={362--385},
year={1987},
publisher={Elsevier}
}
2 changes: 2 additions & 0 deletions modules/objdetect/include/opencv2/objdetect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ using a Boosted Cascade of Simple Features. IEEE CVPR, 2001. The paper is availa
<https://github.com/SvHey/thesis/blob/master/Literature/ObjectDetection/violaJones_CVPR2001.pdf>
@defgroup objdetect_hog HOG (Histogram of Oriented Gradients) descriptor and object detector
@defgroup objdetect_barcode Barcode detection and decoding
@defgroup objdetect_qrcode QRCode detection and encoding
@defgroup objdetect_dnn_face DNN-based face detection and recognition
Check @ref tutorial_dnn_face "the corresponding tutorial" for more details.
Expand Down Expand Up @@ -863,5 +864,6 @@ class CV_EXPORTS_W_SIMPLE QRCodeDetectorAruco : public GraphicalCodeDetector {
#include "opencv2/objdetect/detection_based_tracker.hpp"
#include "opencv2/objdetect/face.hpp"
#include "opencv2/objdetect/charuco_detector.hpp"
#include "opencv2/objdetect/barcode.hpp"

#endif
65 changes: 65 additions & 0 deletions modules/objdetect/include/opencv2/objdetect/barcode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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.
// Copyright (c) 2020-2021 darkliang wangberlinT Certseeds

#ifndef OPENCV_OBJDETECT_BARCODE_HPP
#define OPENCV_OBJDETECT_BARCODE_HPP

#include <opencv2/core.hpp>
#include <opencv2/objdetect/graphical_code_detector.hpp>

namespace cv {
namespace barcode {

//! @addtogroup objdetect_barcode
//! @{

class CV_EXPORTS_W_SIMPLE BarcodeDetector : public cv::GraphicalCodeDetector
{
public:
/** @brief Initialize the BarcodeDetector.
*/
CV_WRAP BarcodeDetector();
/** @brief Initialize the BarcodeDetector.
*
* Parameters allow to load _optional_ Super Resolution DNN model for better quality.
* @param prototxt_path prototxt file path for the super resolution model
* @param model_path model file path for the super resolution model
*/
CV_WRAP BarcodeDetector(const std::string &prototxt_path, const std::string &model_path);
~BarcodeDetector();

/** @brief Decodes barcode in image once it's found by the detect() method.
*
* @param img grayscale or color (BGR) image containing bar code.
* @param points vector of rotated rectangle vertices found by detect() method (or some other algorithm).
* For N detected barcodes, the dimensions of this array should be [N][4].
* Order of four points in vector<Point2f> is bottomLeft, topLeft, topRight, bottomRight.
* @param decoded_info UTF8-encoded output vector of string or empty vector of string if the codes cannot be decoded.
* @param decoded_type vector strings, specifies the type of these barcodes
* @return true if at least one valid barcode have been found
*/
CV_WRAP bool decodeWithType(InputArray img,
InputArray points,
CV_OUT std::vector<std::string> &decoded_info,
CV_OUT std::vector<std::string> &decoded_type) const;

/** @brief Both detects and decodes barcode
* @param img grayscale or color (BGR) image containing barcode.
* @param decoded_info UTF8-encoded output vector of string(s) or empty vector of string if the codes cannot be decoded.
* @param decoded_type vector of strings, specifies the type of these barcodes
* @param points optional output vector of vertices of the found barcode rectangle. Will be empty if not found.
* @return true if at least one valid barcode have been found
*/
CV_WRAP bool detectAndDecodeWithType(InputArray img,
CV_OUT std::vector<std::string> &decoded_info,
CV_OUT std::vector<std::string> &decoded_type,
OutputArray points = noArray()) const;
};
//! @}

}} // cv::barcode::

#endif // OPENCV_OBJDETECT_BARCODE_HPP
50 changes: 50 additions & 0 deletions modules/objdetect/misc/java/test/BarcodeDetectorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.opencv.test.barcode;

import java.util.List;
import org.opencv.core.Mat;
import org.opencv.objdetect.BarcodeDetector;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.test.OpenCVTestCase;
import java.util.ArrayList;

public class BarcodeDetectorTest extends OpenCVTestCase {

private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
private String testDataPath;

@Override
protected void setUp() throws Exception {
super.setUp();

testDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
if (testDataPath == null)
throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
}

public void testDetectAndDecode() {
Mat img = Imgcodecs.imread(testDataPath + "/cv/barcode/multiple/4_barcodes.jpg");
assertFalse(img.empty());
BarcodeDetector detector = new BarcodeDetector();
assertNotNull(detector);
List < String > infos = new ArrayList< String >();
List < String > types = new ArrayList< String >();

boolean result = detector.detectAndDecodeWithType(img, infos, types);
assertTrue(result);
assertEquals(infos.size(), 4);
assertEquals(types.size(), 4);
final String[] correctResults = {"9787122276124", "9787118081473", "9787564350840", "9783319200064"};
for (int i = 0; i < 4; i++) {
assertEquals(types.get(i), "EAN_13");
result = false;
for (int j = 0; j < 4; j++) {
if (correctResults[j].equals(infos.get(i))) {
result = true;
break;
}
}
assertTrue(result);
}

}
}
33 changes: 33 additions & 0 deletions modules/objdetect/misc/python/test/test_barcode_detector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
'''
===============================================================================
Barcode detect and decode pipeline.
===============================================================================
'''
import os
import numpy as np
import cv2 as cv

from tests_common import NewOpenCVTests

class barcode_detector_test(NewOpenCVTests):

def test_detect(self):
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/barcode/multiple/4_barcodes.jpg'))
self.assertFalse(img is None)
detector = cv.barcode_BarcodeDetector()
retval, corners = detector.detect(img)
self.assertTrue(retval)
self.assertEqual(corners.shape, (4, 4, 2))

def test_detect_and_decode(self):
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/barcode/single/book.jpg'))
self.assertFalse(img is None)
detector = cv.barcode_BarcodeDetector()
retval, decoded_info, decoded_type, corners = detector.detectAndDecodeWithType(img)
self.assertTrue(retval)
self.assertTrue(len(decoded_info) > 0)
self.assertTrue(len(decoded_type) > 0)
self.assertEqual(decoded_info[0], "9787115279460")
self.assertEqual(decoded_type[0], "EAN_13")
self.assertEqual(corners.shape, (1, 4, 2))
Loading

0 comments on commit 2e44bf7

Please sign in to comment.