Skip to content

Commit 43e6e6f

Browse files
committed
add references and use uint type for some non-negative variables
1 parent fa9fa31 commit 43e6e6f

File tree

16 files changed

+104
-86
lines changed

16 files changed

+104
-86
lines changed

modules/barcode/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
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.
55

6-
We provide two decoding methods, one-stage method and decode-after-detect method.
7-
8-
- One-stage method can decodes the barcodes in regular orientation. This module provides `decodeDirectly` as the port.
9-
- Decode-after-detect method can locate the barcode with irregular orientation, then decode the cropped regions. However this method is limited by location accuracy. This module provides `detectAndDecode` as the port, it also provides single step function `detect` and `decode`.
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+
}

modules/barcode/src/barcode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ bool BarDecode::decodeMultiplyProcess()
9898
{
9999
Mat bin_bar;
100100
Result max_res;
101-
float max_conf = -1;
101+
float max_conf = -1.f;
102102
bool decoded = false;
103103
for (const auto &decoder:getDecoders())
104104
{
@@ -161,7 +161,7 @@ vector<Mat> BarcodeDetector::Impl::initDecode(const Mat &src, const vector<Point
161161
CV_Assert(!points.empty());
162162
CV_Assert((points.size() % 4) == 0);
163163
src_points.clear();
164-
for (size_t i = 0; i < points.size(); i += 4)
164+
for (int i = 0; (uint) i < points.size(); i += 4)
165165
{
166166
vector<Point2f> tempMat{points.cbegin() + i, points.cbegin() + i + 4};
167167
if (contourArea(tempMat) > 0.0)

modules/barcode/src/decoder/abs_decoder.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ void fillCounter(const std::vector<uchar> &row, uint start, Counter &counter)
6868
}
6969
}
7070

71-
static inline int
72-
patternMatchVariance(const Counter &counter, const std::vector<int> &pattern, int maxIndividualVariance)
71+
static inline uint
72+
patternMatchVariance(const Counter &counter, const std::vector<int> &pattern, uint maxIndividualVariance)
7373
{
7474
size_t numCounters = counter.pattern.size();
75-
int total = counter.sum;
75+
int total = static_cast<int>(counter.sum);
7676
int patternLength = std::accumulate(pattern.cbegin(), pattern.cend(), 0);
7777
if (total < patternLength)
7878
{
@@ -87,12 +87,12 @@ patternMatchVariance(const Counter &counter, const std::vector<int> &pattern, in
8787

8888
int unitBarWidth = (total << INTEGER_MATH_SHIFT) / patternLength;
8989
maxIndividualVariance = (maxIndividualVariance * unitBarWidth) >> INTEGER_MATH_SHIFT;
90-
int totalVariance = 0;
90+
uint totalVariance = 0;
9191
for (uint x = 0; x < numCounters; x++)
9292
{
9393
int cnt = counter.pattern[x] << INTEGER_MATH_SHIFT;
9494
int scaledPattern = pattern[x] * unitBarWidth;
95-
int variance = std::abs(cnt - scaledPattern);
95+
uint variance = std::abs(cnt - scaledPattern);
9696
if (variance > maxIndividualVariance)
9797
{
9898
return std::numeric_limits<int32_t>::max();
@@ -115,7 +115,7 @@ patternMatchVariance(const Counter &counter, const std::vector<int> &pattern, in
115115
* the total variance between counters and patterns equals the pattern length, higher values mean
116116
* even more variance
117117
*/
118-
int patternMatch(const Counter &counters, const std::vector<int> &pattern, uint maxIndividual)
118+
uint patternMatch(const Counter &counters, const std::vector<int> &pattern, uint maxIndividual)
119119
{
120120
CV_Assert(counters.pattern.size() == pattern.size());
121121
return patternMatchVariance(counters, pattern, maxIndividual);

modules/barcode/src/decoder/abs_decoder.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#define __OPENCV_BARCODE_ABS_DECODER_HPP__
88

99
#include <opencv2/barcode.hpp>
10-
#include <utility>
1110

1211
namespace cv {
1312
namespace barcode {
@@ -65,9 +64,9 @@ void cropROI(const Mat &_src, Mat &_dst, const std::vector<Point2f> &rect);
6564
void fillCounter(const std::vector<uchar> &row, uint start, Counter &counter);
6665

6766
constexpr static uint INTEGER_MATH_SHIFT = 8;
68-
constexpr static int PATTERN_MATCH_RESULT_SCALE_FACTOR = 1 << INTEGER_MATH_SHIFT;
67+
constexpr static uint PATTERN_MATCH_RESULT_SCALE_FACTOR = 1 << INTEGER_MATH_SHIFT;
6968

70-
int patternMatch(const Counter &counters, const std::vector<int> &pattern, uint maxIndividual);
69+
uint patternMatch(const Counter &counters, const std::vector<int> &pattern, uint maxIndividual);
7170
}
7271
} // namespace cv
7372

modules/barcode/src/decoder/common/hybrid_binarizer.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ namespace cv {
1111
namespace barcode {
1212

1313

14-
#define CAP(x, x1, x2) x < x1 ? x1 : (x > x2 ? x2 : x)
14+
#define CAP(x, x1, x2) x < (x1) ? (x1) : ((x) > (x2) ? (x2) : (x))
15+
16+
// This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels.
17+
// So this is the smallest dimension in each axis we can accept.
18+
constexpr static int BLOCK_SIZE_POWER = 3;
19+
constexpr static int BLOCK_SIZE = 1 << BLOCK_SIZE_POWER; // ...0100...00
20+
constexpr static int BLOCK_SIZE_MASK = BLOCK_SIZE - 1; // ...0011...11
21+
constexpr static int MINIMUM_DIMENSION = BLOCK_SIZE * 5;
22+
constexpr static int MIN_DYNAMIC_RANGE = 24;
1523

1624
void
1725
calculateThresholdForBlock(const std::vector<uchar> &luminances, int sub_width, int sub_height, int width, int height,

modules/barcode/src/decoder/common/hybrid_binarizer.hpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010
namespace cv {
1111
namespace barcode {
1212

13-
// This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels.
14-
// So this is the smallest dimension in each axis we can accept.
15-
constexpr static int BLOCK_SIZE_POWER = 3;
16-
constexpr static int BLOCK_SIZE = 1 << BLOCK_SIZE_POWER; // ...0100...00
17-
constexpr static int BLOCK_SIZE_MASK = BLOCK_SIZE - 1; // ...0011...11
18-
constexpr static int MINIMUM_DIMENSION = BLOCK_SIZE * 5;
19-
constexpr static int MIN_DYNAMIC_RANGE = 24;
20-
21-
2213
void hybridBinarization(const Mat &src, Mat &dst);
2314

2415
void

modules/barcode/src/decoder/common/super_scale.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
// Modified by darkliang wangberlinT
88

99
#include "../../precomp.hpp"
10+
#include "opencv2/dnn.hpp"
1011
#include "super_scale.hpp"
1112

1213

13-
#define CLIP(x, x1, x2) max(x1, min(x, x2))
1414
namespace cv {
1515
namespace barcode {
16+
#define CLIP(x, x1, x2) max(x1, min(x, x2))
17+
constexpr static float MAX_SCALE = 4.0f;
18+
1619
int SuperScale::init(const std::string &proto_path, const std::string &model_path)
1720
{
1821
srnet_ = dnn::readNetFromCaffe(proto_path, model_path);
@@ -39,9 +42,7 @@ void SuperScale::processImageScale(const Mat &src, Mat &dst, float scale, const
3942
{
4043
if (use_sr && (int) sqrt(width * height * 1.0) < sr_max_size && net_loaded_)
4144
{
42-
int ret = superResolutionScale(src, dst);
43-
if (ret != 0)
44-
{ resize(src, dst, Size(), scale, scale, INTER_CUBIC); }
45+
superResolutionScale(src, dst);
4546
if (scale > 2.0)
4647
{
4748
processImageScale(dst, dst, scale / 2.0f, use_sr);
@@ -67,7 +68,7 @@ int SuperScale::superResolutionScale(const Mat &src, Mat &dst)
6768
const float *prob_score = prob.ptr<float>(0, 0, row);
6869
for (int col = 0; col < prob.size[3]; col++)
6970
{
70-
float pixel = prob_score[col] * 255.0;
71+
float pixel = prob_score[col] * 255.0f;
7172
dst.at<uint8_t>(row, col) = static_cast<uint8_t>(CLIP(pixel, 0.0f, 255.0f));
7273
}
7374
}

modules/barcode/src/decoder/common/super_scale.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#ifndef __OPENCV_BARCODE_SUPER_SCALE_HPP__
99
#define __OPENCV_BARCODE_SUPER_SCALE_HPP__
1010

11-
#include "opencv2/dnn.hpp"
1211

1312
namespace cv {
1413
namespace barcode {
@@ -27,7 +26,6 @@ class SuperScale
2726
private:
2827
dnn::Net srnet_;
2928
bool net_loaded_ = false;
30-
constexpr static float MAX_SCALE = 4.0f;
3129

3230
int superResolutionScale(const cv::Mat &src, cv::Mat &dst);
3331
};

modules/barcode/src/decoder/ean13_decoder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Result Ean13Decoder::decode(const vector<uchar> &data) const
2828
{
2929
return Result("Wrong Size", BarcodeType::NONE);
3030
}
31-
pair<int, int> pattern;
31+
pair<uint, uint> pattern;
3232
if (!findStartGuardPatterns(data, pattern))
3333
{
3434
return Result("Begin Pattern Not Found", BarcodeType::NONE);

0 commit comments

Comments
 (0)