forked from opencv/opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request opencv#23666 from mshabunin:barcode-move
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
Showing
32 changed files
with
2,904 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
modules/objdetect/misc/python/test/test_barcode_detector.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Oops, something went wrong.