使用Python和OpenCV侦测条形码区域

Detecting Barcodes in Images with Python and OpenCV

Detecting Barcodes in Images with Python and OpenCV

 

Detecting Barcodes in Images using Python and OpenCV

The goal of this blog post is to demonstrate a basic implementation of barcode detection using computer vision and image processing techniques. My implementation of the algorithm is originally based loosely on this StackOverflow question. I have gone through the code and provided some updates and improvements to the original algorithm.

It’s important to note that this algorithm will not work for all barcodes, but it should give you the basic intuition as to what types of techniques you should be applying.

For this example, we will be detecting the barcode in the following image:

Figure 1: Example image containing a barcode that we want to detect.

Figure 1: Example image containing a barcode that we want to detect.

Let’s go ahead and start writing some code. Open up a new file, name it detect_barcode.py , and let’s get coding:

The first thing we’ll do is import the packages we’ll need. We’ll utilize NumPy for numeric processing, argparse  for parsing command line arguments, and cv2  for our OpenCV bindings.

Then we’ll setup our command line arguments. We need just a single switch here, image , which is the path to our image that contains a barcode that we want to detect.

Now, time for some actual image processing:

On Lines 12 and 13 we load our image  off disk and convert it to grayscale.

Then, we use the Scharr operator (specified using ksize = 1 ) to construct the gradient magnitude representation of the grayscale image in the horizontal and vertical directions on Lines 17 and 18.

From there, we subtract the y-gradient of the Scharr operator from the x-gradient of the Scharr operator on Lines 21 and 22. By performing this subtraction we are left with regions of the image that have high horizontal gradients and low vertical gradients.

Our gradient  representation of our original image above looks like:

Figure 2: The gradient representation of our barcode image.

Figure 2: The gradient representation of our barcode image.

Notice how the barcoded region of the image has been detected by our gradient operations. The next steps will be to filter out the noise in the image and focus solely on the barcode region.

The first thing we’ll do is apply an average blur on Line 25 to the gradient image using a 9 x 9 kernel. This will help smooth out high frequency noise in the gradient representation of the image.

We’ll then threshold the blurred image on Line 26. Any pixel in the gradient image that is not greater than 225 is set to 0 (black). Otherwise, the pixel is set to 255 (white).

The output of the blurring and thresholding looks like this:

Figure 3: Thresholding the gradient image to obtain a rough approximation to the rectangular barcode region.

Figure 3: Thresholding the gradient image to obtain a rough approximation to the rectangular barcode region.

However, as you can see in the threshold image above, there are gaps between the vertical bars of the barcode. In order to close these gaps and make it easier for our algorithm to detect the “blob”-like region of the barcode, we’ll need to perform some basic morphological operations:

We’ll start by constructing a rectangular kernel using the cv2.getStructuringElement  on Line 29. This kernel has a width that is larger than the height, thus allowing us to close the gaps between vertical stripes of the barcode.

We then perform our morphological operation on Line 30 by applying our kernel to our thresholded image, thus attempting to close the the gaps between the bars.

You can now see that the gaps are substantially more closed, as compared to the thresholded image above:

Figure 4: Applying closing morphological operations to close the gap between barcode stripes.

Figure 4: Applying closing morphological operations to close the gap between barcode stripes.

Of course, now we have small blobs in the image that are not part of the actual barcode, but may interfere with our contour detection.

Let’s go ahead and try to remove these small blobs:

All we are doing here is performing 4 iterations of erosions, followed by 4 iterations of dilations. An erosion will “erode” the white pixels in the image, thus removing the small blobs, whereas a dilation will “dilate” the remaining white pixels and grow the white regions back out.

Provided that the small blobs were removed during the erosion, they will not reappear during the dilation.

After our series of erosions and dilations you can see that the small blobs have been successfully removed and we are left with the barcode region:

Figure 5: Removing small, irrelevant blobs by applying a series of erosions and dilations.

Figure 5: Removing small, irrelevant blobs by applying a series of erosions and dilations.

Finally, let’s find the contours of the barcoded region of the image:

Luckily, this is the easy part. On Lines 38-40 we simply find the largest contour in the image, which if we have done our image processing steps correctly, should correspond to the barcoded region.

We then determine the minimum bounding box for the largest contour on Lines 43 and 44 and finally display the detected barcode on Lines 48-50.

As you can see in the following image, we have successfully detected the barcode:

Figure 6: Successfully detecting the barcode in our example image.

Figure 6: Successfully detecting the barcode in our example image.

In the next section we’ll try a few more images.

Successful Barcode Detections

To follow along with these results, use the form at the bottom of this post to download the source code and accompanying images for this blog post.

Once you have the code and images, open up a terminal and execute the following command:

Figure 7: Using OpenCV to detect a barcode in an image.

Figure 7: Using OpenCV to detect a barcode in an image.

No problem detecting the barcode on that jar of coconut oil!

Let’s try another image:

Figure 8: Using computer vision to detect a barcode in an image.

Figure 8: Using computer vision to detect a barcode in an image.

We were able to find the barcode in that image too!

But enough of the food products, what about the barcode on a book:

Figure 9: Detecting a barcode on a book using Python and OpenCV.

Figure 9: Detecting a barcode on a book using Python and OpenCV.

Again, no problem!

How about the tracking code on a package?

Figure 10: Detecting the barcode on a package using computer vision and image processing.

Figure 10: Detecting the barcode on a package using computer vision and image processing.

Again, our algorithm is able to successfully detect the barcode.

Finally, let’s try one more image This one is of my favorite pasta sauce, Rao’s Homemade Vodka Sauce:

Figure 12: Barcode detection is easy using Python and OpenCV!

Figure 11: Barcode detection is easy using Python and OpenCV!

We were once again able to detect the barcode!

Summary

In this blog post we reviewed the steps necessary to detect barcodes in images using computer vision techniques. We implemented our algorithm using the Python programming language and the OpenCV library.

The general outline of the algorithm is to:

  1. Compute the Scharr gradient magnitude representations in both the and y direction.
  2. Subtract the y-gradient from the x-gradient to reveal the barcoded region.
  3. Blur and threshold the image.
  4. Apply a closing kernel to the thresholded image.
  5. Perform a series of dilations and erosions.
  6. Find the largest contour in the image, which is now presumably the barcode.

It is important to note that since this method makes assumptions regarding the gradient representations of the image, and thus will only work for horizontal barcodes.

If you wanted to implement a more robust barcode detection algorithm, you would need to take the orientation of the image into consideration, or better yet, apply machine learning techniques such as Haar cascades or HOG + Linear SVM to “scan” the image for barcoded regions.

推荐文章

沪公网安备 31010702002009号