Detect Barcode in image using OpenCV

Tram Ho

The other day while reading miscellaneous on the blog and StackOverflow, I have read questions about how to detect barcodes on photos. Based on the answer to that question, I tried installing the code and got quite good results. Although this part of the code will not work for all barcodes, it will give us a basic understanding of the type of technique to be applied.

In this example, we will detect the barcode in the following image:

Code implementation

The first thing we need to do is import the necessary libraries. To receive barcodes in images, we use numpy libraries (used to process matrices), imutils and cv2. In the code implementation, we first use cv2 to load images as well as convert color images to grayscale images.

We then use the Sobel operator with ksize = -1 to construct the gradient intensity representation of the grayscale image in the horizontal and vertical directions. obtained horizontally of the Sobel operator. By doing this subtraction, we only have areas of the image with high horizontal gradient intensity and low vertical gradient intensity.

Next, we apply the average blur filter to the 11×11 nuclear gradient image. This will help reduce high frequency noise in the image intensity gradient representation of the image.

Then, we will threshold the blur image that has been blurred on. Any pixels in the image that are not larger than 150 are set to 0 (black) and the remaining pixels are set to 255 (white).

Next, we’ll start by building a rectangular kernel using the cv2.getSturationuringEuity function. We then perform the morphological operation by applying the kernel to the thresholded image, which can then fill in the gaps between barcodes.

Next we perform Closing to smooth the contours of the objects, fill the boundary gaps and remove small holes. This operation is performed by enlarging then eroding the image.

In the final step, using the function cv2.findContours we find the continuous region with the largest value and determine this is the barcode container. With the function cv2.drawContours we can easily mark this area by drawing a rectangular frame.

The results are as follows:

Although not achieving absolute accuracy, the blue frame is quite close to our barcode.

Several theories are used

This section lists some of the used algorithms implemented in openCV

Mask Filter

In fact, in image processing, filtering is used a lot and has many important roles. By using window / filter as odd-size matrices (e.g. 3×3 or 5×5) and using the convolution operator to apply the window / filter to the original image, we can obtain a new image with a lower noise level as well. as has been made more smooth than the original image.

Sobel

A simple way to determine if a pixel is an edge is to check the brightness value at that pixel minus the brightness value of the nearby pixel. If the difference is high, that means there is a sudden change in brightness at that point; and vice versa, if the result is a low value, then the high probability is not the edge. To do this, we will use convolution to solve this problem quickly. The way Sobel is performed is also the convolution calculation mentioned above with the kernel value (simply the matrix) used by this method as follows:

OpenCV provides built-in functions that can be used as follows:

Structuring Element

Dilation

As one of the basic operations in mathematical form. This operation works to make the original object in the image increase in size.

The enlargement of the image A with B is denoted as follows:

Erosion image

As one of the basic operations in mathematical form. This operation works to reduce the size of the object, separate objects close together, fragment and find the bones of the object.

The erosion of image A with B is denoted as follows:

Closing permission

is the operation to perform Dilation before performing the contraction (Erosion).

Closing operations are used in applications that smooth the boundaries of objects, fill boundary gaps, and eliminate small holes.

References

Share the news now

Source : Viblo