Skip to content

Commit ed9e79d

Browse files
committed
Merge branch 4.x
2 parents 68492c4 + 029c283 commit ed9e79d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3091
-359
lines changed

modules/aruco/samples/tutorial_charuco_create_detect.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static bool readCameraParameters(std::string filename, cv::Mat& camMatrix, cv::M
2121
return false;
2222
fs["camera_matrix"] >> camMatrix;
2323
fs["distortion_coefficients"] >> distCoeffs;
24-
return true;
24+
return (camMatrix.size() == cv::Size(3,3)) ;
2525
}
2626

2727
void createBoard()
@@ -161,4 +161,4 @@ int main(int argc, char* argv[])
161161
break;
162162
}
163163
return 0;
164-
}
164+
}

modules/barcode/CMakeLists.txt

+2
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

+9
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

+28
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+
}
+101
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__
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
misc/java/src/cpp/barcode_converters.hpp
+12
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+
}
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+
jclass jInteger = env->FindClass("java/lang/Integer");
20+
jmethodID m_create_Integer = CONSTRUCTOR(env, jInteger);
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+
}
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 */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.opencv.test.barcode;
2+
3+
import java.util.List;
4+
import org.opencv.core.Mat;
5+
import org.opencv.barcode.BarcodeDetector;
6+
import org.opencv.imgcodecs.Imgcodecs;
7+
import org.opencv.test.OpenCVTestCase;
8+
import java.util.ArrayList;
9+
import static org.opencv.barcode.Barcode.EAN_13;
10+
11+
public class BarcodeDetectorTest extends OpenCVTestCase {
12+
13+
private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
14+
private String testDataPath;
15+
16+
@Override
17+
protected void setUp() throws Exception {
18+
super.setUp();
19+
20+
testDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
21+
if (testDataPath == null)
22+
throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
23+
}
24+
25+
public void testDetectAndDecode() {
26+
Mat img = Imgcodecs.imread(testDataPath + "/cv/barcode/multiple/4_barcodes.jpg");
27+
assertFalse(img.empty());
28+
BarcodeDetector detector = new BarcodeDetector();
29+
assertNotNull(detector);
30+
List < String > infos = new ArrayList< String >();
31+
List < Integer > types = new ArrayList< Integer >();
32+
33+
boolean result = detector.detectAndDecode(img, infos, types);
34+
assertTrue(result);
35+
assertEquals(infos.size(), 4);
36+
assertEquals(types.size(), 4);
37+
final String[] correctResults = {"9787122276124", "9787118081473", "9787564350840", "9783319200064"};
38+
for (int i = 0; i < 4; i++) {
39+
assertEquals(types.get(i).intValue(), EAN_13);
40+
result = false;
41+
for (int j = 0; j < 4; j++) {
42+
if (correctResults[j].equals(infos.get(i))) {
43+
result = true;
44+
break;
45+
}
46+
}
47+
assertTrue(result);
48+
}
49+
50+
}
51+
}
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
'''
3+
===============================================================================
4+
Barcode detect and decode pipeline.
5+
===============================================================================
6+
'''
7+
import os
8+
import numpy as np
9+
import cv2 as cv
10+
11+
from tests_common import NewOpenCVTests
12+
13+
class barcode_detector_test(NewOpenCVTests):
14+
15+
def test_detect(self):
16+
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/barcode/multiple/4_barcodes.jpg'))
17+
self.assertFalse(img is None)
18+
detector = cv.barcode_BarcodeDetector()
19+
retval, corners = detector.detect(img)
20+
self.assertTrue(retval)
21+
self.assertEqual(corners.shape, (4, 4, 2))
22+
23+
def test_detect_and_decode(self):
24+
img = cv.imread(os.path.join(self.extraTestDataPath, 'cv/barcode/single/book.jpg'))
25+
self.assertFalse(img is None)
26+
detector = cv.barcode_BarcodeDetector()
27+
retval, decoded_info, decoded_type, corners = detector.detectAndDecode(img)
28+
self.assertEqual(decoded_info[0], "9787115279460")
29+
self.assertEqual(decoded_type[0], cv.barcode.EAN_13)
30+
self.assertEqual(corners.shape, (1, 4, 2))

0 commit comments

Comments
 (0)