OpenCV 4.10.0-dev
Open Source Computer Vision
Loading...
Searching...
No Matches
Barcode Recognition

Prev Tutorial: Cascade Classifier Training
Next Tutorial: Introduction to Support Vector Machines

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: [304] , [144] , [21]

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).

try
{
app.bardet = makePtr<barcode::BarcodeDetector>(sr_prototxt, sr_model);
}
catch (const std::exception& e)
{
cout <<
"\n---------------------------------------------------------------\n"
"Failed to initialize super resolution.\n"
"Please, download 'sr.*' from\n"
"https://github.com/WeChatCV/opencv_3rdparty/tree/wechat_qrcode\n"
"and put them into the current directory.\n"
"Or you can leave sr_prototxt and sr_model unspecified.\n"
"---------------------------------------------------------------\n";
cout << e.what() << endl;
return -1;
}

We need to create variables to store the outputs.

vector<Point> corners;
vector<string> decode_info;
vector<string> decode_type;

Detecting

cv::barcode::BarcodeDetector::detect method uses an algorithm based on directional coherence. First, we compute the average squared gradients of every pixel, [21] . 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:

bardet->detectMulti(frame, corners);

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:

bardet->detectAndDecodeWithType(frame, decode_info, decode_type, corners);

Visualize the results:

for (size_t i = 0; i < corners.size(); i += 4)
{
const size_t idx = i / 4;
const bool isDecodable = idx < decode_info.size()
&& idx < decode_type.size()
&& !decode_type[idx].empty();
const Scalar lineColor = isDecodable ? greenColor : redColor;
// draw barcode rectangle
vector<Point> contour(corners.begin() + i, corners.begin() + i + 4);
const vector< vector<Point> > contours {contour};
drawContours(frame, contours, 0, lineColor, 1);
// draw vertices
for (size_t j = 0; j < 4; j++)
circle(frame, contour[j], 2, randColor(), -1);
// write decoded text
if (isDecodable)
{
ostringstream buf;
buf << "[" << decode_type[idx] << "] " << decode_info[idx];
putText(frame, buf.str(), contour[1], FONT_HERSHEY_COMPLEX, 0.8, yellowColor, 1);
}
}

Results

Original image:

image

After detection:

image