Skip to content

Commit e673aa4

Browse files
CertseedsdarkliangWangberlinT
authored
Merge pull request #2757 from Certseeds:Barcode-Support
1D Barcode support * init barcode interface. * barcode: update readme. * now it is just interface, do not need CMakeLists to compiler it. Signed-off-by: Killer_Quinn <51754303+Certseeds@users.noreply.github.com> * fix-trailing whitespace of docs-build. Signed-off-by: Killer_Quinn <51754303+Certseeds@users.noreply.github.com> * Branch: Barcode-Support,replace vector<RotateRect> to vector<vector<Point2f>>, barcodeDirectly now just output one string, this commit is still only contain interface, barcode module will not be compile. Signed-off-by: Killer_Quinn <51754303+Certseeds@users.noreply.github.com> * add implementation details * fix doc bug * not generate python bindings temporarily * add barcode group for doxygen * generate python bindings and improve performance * remove win10 build warnings in detect stage * remove win10 build warnings on decode stage * add samples and accuracy tests * Update README.md * add tutorial, part of content is to be done. * add decode and EAN part in tutorial * refactor imports * delete decodeDirectly api for simplicity * add super resolution and optimize code format * Use @snippet / @include doxygen statements for embedding code from .cpp files * improve decoding performance * optimize code and slightly improve the performance * add ean8 support * add references and use uint type for some non-negative variables * support java bindings * optimize wording in source code and documentation * refine code * whitespace * bugfix: forget to clear list Co-authored-by: darkliang <11710911@mail.sustech.edu.cn> Co-authored-by: WangberlinT <11711613@mail.sustech.edu.cn> Co-authored-by: Junhao Liang <43094337+darkliang@users.noreply.github.com>
1 parent 201cd5b commit e673aa4

36 files changed

+2686
-0
lines changed

modules/barcode/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
set(the_description "Barcode Detection and Decoding")
2+
ocv_define_module(barcode opencv_core opencv_imgproc opencv_dnn WRAP python java)

modules/barcode/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
1D Barcode Detect and Decode
2+
======================
3+
4+
This module is focused on detecting and decoding barcode from image. It is mainly designed for scanning the images, locating barcode, decoding barcode and outputting its decoded result.
5+
6+
1. Support 1-dimension bar code detection of any angle tilting.
7+
2. Support multi-scale detection.
8+
3. Support EAN-13, EAN-8 and UPC-A decode yet.
9+
4. With x86 CPU, it achieves 50FPS averagely.

modules/barcode/doc/barcode.bib

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@mastersthesis{Xiangmin2015research,
2+
title={Research on Barcode Recognition Technology In a Complex Background},
3+
author={Xiangmin, Wang},
4+
year={2015},
5+
school={Huazhong University of Science and Technology}
6+
}
7+
8+
@article{bazen2002systematic,
9+
title={Systematic methods for the computation of the directional fields and singular points of fingerprints},
10+
author={Bazen, Asker M and Gerez, Sabih H},
11+
journal={IEEE transactions on pattern analysis and machine intelligence},
12+
volume={24},
13+
number={7},
14+
pages={905--919},
15+
year={2002},
16+
publisher={IEEE}
17+
}
18+
19+
@article{kass1987analyzing,
20+
title={Analyzing oriented patterns},
21+
author={Kass, Michael and Witkin, Andrew},
22+
journal={Computer vision, graphics, and image processing},
23+
volume={37},
24+
number={3},
25+
pages={362--385},
26+
year={1987},
27+
publisher={Elsevier}
28+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
// Copyright (c) 2020-2021 darkliang wangberlinT Certseeds
5+
6+
#ifndef __OPENCV_BARCODE_HPP__
7+
#define __OPENCV_BARCODE_HPP__
8+
9+
#include <opencv2/core.hpp>
10+
#include <ostream>
11+
12+
/** @defgroup barcode Barcode detecting and decoding methods
13+
*/
14+
15+
namespace cv {
16+
namespace barcode {
17+
18+
//! @addtogroup barcode
19+
//! @{
20+
21+
enum BarcodeType
22+
{
23+
NONE, EAN_8, EAN_13, UPC_A, UPC_E, UPC_EAN_EXTENSION
24+
};
25+
26+
static inline std::ostream &operator<<(std::ostream &out, const BarcodeType &barcode_type)
27+
{
28+
switch (barcode_type)
29+
{
30+
case BarcodeType::EAN_8:
31+
out << "EAN_8";
32+
break;
33+
case BarcodeType::EAN_13:
34+
out << "EAN_13";
35+
break;
36+
case BarcodeType::UPC_E:
37+
out << "UPC_E";
38+
break;
39+
case BarcodeType::UPC_A:
40+
out << "UPC_A";
41+
break;
42+
case BarcodeType::UPC_EAN_EXTENSION:
43+
out << "UPC_EAN_EXTENSION";
44+
break;
45+
default:
46+
out << "NONE";
47+
}
48+
return out;
49+
}
50+
51+
class CV_EXPORTS_W BarcodeDetector
52+
{
53+
public:
54+
/**
55+
* @brief Initialize the BarcodeDetector.
56+
* @param prototxt_path prototxt file path for the super resolution model
57+
* @param model_path model file path for the super resolution model
58+
*/
59+
CV_WRAP BarcodeDetector(const std::string &prototxt_path = "", const std::string &model_path = "");
60+
61+
~BarcodeDetector();
62+
63+
/** @brief Detects Barcode in image and returns the rectangle(s) containing the code.
64+
*
65+
* @param img grayscale or color (BGR) image containing (or not) Barcode.
66+
* @param points Output vector of vector of vertices of the minimum-area rotated rectangle containing the codes.
67+
* For N detected barcodes, the dimensions of this array should be [N][4].
68+
* Order of four points in vector< Point2f> is bottomLeft, topLeft, topRight, bottomRight.
69+
*/
70+
CV_WRAP bool detect(InputArray img, OutputArray points) const;
71+
72+
/** @brief Decodes barcode in image once it's found by the detect() method.
73+
*
74+
* @param img grayscale or color (BGR) image containing bar code.
75+
* @param points vector of rotated rectangle vertices found by detect() method (or some other algorithm).
76+
* For N detected barcodes, the dimensions of this array should be [N][4].
77+
* Order of four points in vector<Point2f> is bottomLeft, topLeft, topRight, bottomRight.
78+
* @param decoded_info UTF8-encoded output vector of string or empty vector of string if the codes cannot be decoded.
79+
* @param decoded_type vector of BarcodeType, specifies the type of these barcodes
80+
*/
81+
CV_WRAP bool decode(InputArray img, InputArray points, CV_OUT std::vector<std::string> &decoded_info, CV_OUT
82+
std::vector<BarcodeType> &decoded_type) const;
83+
84+
/** @brief Both detects and decodes barcode
85+
86+
* @param img grayscale or color (BGR) image containing barcode.
87+
* @param decoded_info UTF8-encoded output vector of string(s) or empty vector of string if the codes cannot be decoded.
88+
* @param decoded_type vector of BarcodeType, specifies the type of these barcodes
89+
* @param points optional output vector of vertices of the found barcode rectangle. Will be empty if not found.
90+
*/
91+
CV_WRAP bool detectAndDecode(InputArray img, CV_OUT std::vector<std::string> &decoded_info, CV_OUT
92+
std::vector<BarcodeType> &decoded_type, OutputArray points = noArray()) const;
93+
94+
protected:
95+
struct Impl;
96+
Ptr<Impl> p;
97+
};
98+
//! @}
99+
}
100+
} // cv::barcode::
101+
#endif //__OPENCV_BARCODE_HPP__
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
misc/java/src/cpp/barcode_converters.hpp
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type_dict": {
3+
"vector_BarcodeType": {
4+
"j_type": "List<Integer>",
5+
"jn_type": "List<Integer>",
6+
"jni_type": "jobject",
7+
"jni_var": "std::vector< cv::barcode::BarcodeType > %(n)s",
8+
"suffix": "Ljava_util_List",
9+
"v_type": "vector_BarcodeType"
10+
}
11+
}
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html
4+
5+
// Author: darkliang
6+
7+
#include "barcode_converters.hpp"
8+
9+
#define LOG_TAG "org.opencv.barcode"
10+
11+
12+
void Copy_vector_BarcodeType_to_List(JNIEnv* env, std::vector<cv::barcode::BarcodeType>& vs, jobject list)
13+
{
14+
static jclass juArrayList = ARRAYLIST(env);
15+
jmethodID m_add = LIST_ADD(env, juArrayList);
16+
jmethodID m_clear = LIST_CLEAR(env, juArrayList);
17+
env->CallVoidMethod(list, m_clear);
18+
19+
static jclass jInteger = env->FindClass("java/lang/Integer");
20+
static jmethodID m_create_Integer = env->GetMethodID(jInteger, "<init>", "(I)V");
21+
22+
for (size_t i = 0; i < vs.size(); ++i)
23+
{
24+
jobject element = env->NewObject(jInteger, m_create_Integer, vs[i]);
25+
env->CallBooleanMethod(list, m_add, element);
26+
env->DeleteLocalRef(element);
27+
}
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html
4+
5+
// Author: darkliang
6+
7+
#ifndef BARCODE_CONVERTERS_HPP
8+
#define BARCODE_CONVERTERS_HPP
9+
10+
#include <jni.h>
11+
#include "opencv_java.hpp"
12+
#include "opencv2/core.hpp"
13+
#include "opencv2/barcode.hpp"
14+
15+
16+
using namespace cv::barcode;
17+
18+
void Copy_vector_BarcodeType_to_List(JNIEnv* env, std::vector<cv::barcode::BarcodeType>& vs, jobject list);
19+
20+
#endif /* BARCODE_CONVERTERS_HPP */
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifdef HAVE_OPENCV_BARCODE
2+
typedef std::vector<cv::barcode::BarcodeType> vector_BarcodeType;
3+
4+
template<> struct pyopencvVecConverter<cv::barcode::BarcodeType>
5+
{
6+
static bool to(PyObject* obj, std::vector<cv::barcode::BarcodeType>& value, const ArgInfo& info)
7+
{
8+
return pyopencv_to_generic_vec(obj, value, info);
9+
}
10+
static PyObject* from(const std::vector<cv::barcode::BarcodeType>& value)
11+
{
12+
13+
return pyopencv_from_generic_vec(value);
14+
}
15+
};
16+
17+
template<>
18+
bool pyopencv_to(PyObject *o, std::vector<cv::barcode::BarcodeType>& types, const ArgInfo& info)
19+
{
20+
return pyopencvVecConverter<cv::barcode::BarcodeType>::to(o, types, info);
21+
}
22+
#endif // HAVE_OPENCV_BARCODE

0 commit comments

Comments
 (0)