Detect barcodes on images with Python and OpenCV

Source: Internet
Author: User

Use Python and OpenCV to detect barcodes on a picture

The purpose of this blog post is to use computer vision and image processing techniques to demonstrate a basic implementation of barcode detection. The algorithm I implemented was essentially based on the problem on StackOverflow, and after I browsed through the code, I provided some updates and improvements to the original algorithm.

The first thing to note is that this algorithm is not valid for all barcodes, but will give you a basic intuition about what type of technology to apply.

Let's say we want to detect barcodes in:

Figure 1: Sample picture with barcode

Now let's start writing some code, create a new file, name it detect_barcode.py, open and encode:

1 # Import the necessary packages2 import NumPy as np3 import argparse4 Import cv256 # construct the argument parse and PA RSE the ARGUMENTS7 ap = Argparse. Argumentparser () 8 ap.add_argument ("-I", "--image", required = True, help = "path to the image file") 9 args = VARs (ap.parse _args ())

The first thing we do is import the required packages, we will use NumPy to do numerical calculations, argparse to parse the command line arguments, CV2 is OpenCV binding.

Then we set the command line arguments, we need a simple choice here, –image refers to the path of the image file that contains the barcode to be detected.

Now start the real image processing:

# Load the image and convert it to grayscale12 image = Cv2.imread (args["image"]) Gray = Cv2.cvtcolor (image, Cv2. Color_bgr2gray) 1415 # Compute the ScHARR gradient magnitude representation of the IMAGES16 # in both the X and Y direction GRADX = Cv2. Sobel (Gray, ddepth = cv2.cv.CV_32F, dx = 1, dy = 0, ksize =-1) GradY = Cv2. Sobel (Gray, ddepth = cv2.cv.CV_32F, dx = 0, dy = 1, ksize =-1) 1920x1080 # Subtract the y-gradient from the x-gradient21 Gradie NT = Cv2.subtract (GRADX, GradY)-gradient = cv2.convertscaleabs (gradient)

12~13: Loads an image from disk and converts it to a grayscale image.

17~18: Use the ScHARR action (specify using Ksize =-1) to construct a gradient amplitude representation of the grayscale in both horizontal and vertical directions.

21~22: After the ScHARR operation, we subtract the y-gradient from the x-gradient, and with this step subtraction, we end up with an image area with a high level gradient and a low vertical gradient.

The original image shown in the gradient above looks like this:

Figure: 2: Gradient representation of barcode images

Notice how the barcode area is detected by the gradient operation. the next step is to focus only on the barcode area by removing noise.

Blur and threshold the image25 blurred = Cv2.blur (gradient, (9, 9)) (_, Thresh) = Cv2.threshold (blurred, 225, 255, Cv2. Thresh_binary)

25 Line: The first thing we want to do is to use the 9*9 kernel to mean blurring the gradient graph, which will help smooth the high-frequency noise in the graph of the gradient characterization.

26 Rows: Then we will make the blurred graph binary, any pixels less than or equal to 255 in the gradient graph is set to 0 (black), the rest is set to 255 (white).

The fuzzy and binary output looks like this:

Figure 3: A two-valued gradient plot to obtain a rough approximation of the rectangular barcode area

However, as you can see, in the above two-valued image, there is a gap between the bars of the bar code, in order to eliminate these gaps, and make our algorithm easier to detect the "spot" area in the barcode, we need to do some basic morphological operations:

Construct a closing kernel and apply it to the thresholded image29 kernel = cv2.getstructuringelement (cv2. Morph_rect, (7)) closed = Cv2.morphologyex (Thresh, Cv2. Morph_close, Kernel)

29 Line: We first use cv2.getstructuringelement to construct a rectangular kernel. The width of the kernel is greater than the length, so we can eliminate the gap between the vertical bars in the barcode.

30 Line: The morphological operation here, the previous step of the kernel is applied to our two value graph, in order to eliminate the gap between the vertical bar.

Now, you can see that these gaps are basically eliminated compared to the above two-valued Image:

Figure 4: Using closed operations in morphology to eliminate gaps between bar codes ' vertical bars

Of course, there are still some small spots in the image that are not part of the real barcode, but may affect our contour detection.

Let's eliminate these small spots:

Perform a series of erosions and dilations33 closed = Cv2.erode (closed, None, iterations = 4) closed = Cv2.dilate (c Losed, None, iterations = 4)

What we're doing here is first 4 corrosion (erosion) and then 4 expansion (dilation). The corrosion operation will corrode the white pixels in the image to eliminate the small spots, and the expansion operation will expand the remaining white pixels and grow back again.

If the small spots are removed during the corrosion operation, they will not appear in the expansion operation.

Through our series of corrosion and expansion operations, we have been able to see that we have successfully removed small spots and obtained the barcode area.

Figure 5: Application of a series of corrosion and expansion to remove irrelevant small spots

Finally, let's find the outline of the barcode in the image:

# Find the contours in the thresholded image, then sort the contours37 # by their area, keeping only the largest one38 (CNTs, _) = Cv2.findcontours (Closed.copy (), Cv2. Retr_external,39cv2. chain_approx_simple) + c = sorted (CNTs, key = cv2.contourarea, reverse = True) [0]41] compute the rotated bounding box Of the largest contour43 rect = Cv2.minarearect (c) cv2.cv.BoxPoints box = np.int0 (bounding (rect)) Nded the detected barcode and display the47 # image48 cv2.drawcontours (image, [box],-1, (0, 255, 0), 3) cv2.imshow ("Ima GE ", image) Cv2.waitkey (0)

38~40: Fortunately this part is easier, we simply find the largest contour in the image, and if we do the image processing step correctly, this should correspond to the barcode area.

43~44: Then we determine the minimum bounding rectangle for the maximum contour.

48~50: Final display of detected barcodes

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

Figure 6: Barcode successfully detected in the sample image

In the next section, we'll try more images.

Successful barcode detection

To follow these results, use the form below in this article to download the source code and the accompanying images.

Once you have the code and the image, open a terminal to execute the following command:

$ python detect_barcode.py--image images/barcode_02.jpg

Figure 7: Using OPENCV to detect a barcode in an image

There is no problem checking the barcode on the coconut oil bottle.

Let's try another picture:

$ python detect_barcode.py--image images/barcode_03.jpg

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

We can also find barcodes in the images above.

The bar code test for food is enough, what about the barcode on the book:

$ python detect_barcode.py--image images/barcode_04.jpg

Figure 9: Using Python and OPENCV to detect barcodes on books

No problem, pass it again.

What about the tracking code on that package?

$ python detect_barcode.py--image images/barcode_05.jpg

Figure 10: Detecting barcodes on packages using computer vision and image processing

Our algorithm successfully detects the barcode again.

Finally, let's try another picture, this is my favorite spaghetti sauce-Raosi homemade vodka sauce (Rao's Homemade Vodka Sauce):

$ python detect_barcode.py--image images/barcode_06.jpg

Figure 11: Barcodes are easy to detect using Python and OpenCV

Once again, our algorithm detects barcodes!

Summarize

In this blog post, we review the steps necessary to detect barcodes in images using computer vision, using the Python programming language and the OpenCV library to implement our algorithms.

The algorithm is summarized as follows:

    1. Calculates the ScHARR gradient amplitude representation in the X and Y directions
    2. X-gradient minus y-gradient to display the barcode area
    3. Fuzzy and binary image
    4. Applying closed computing cores to two-valued images
    5. A series of corrosion, expansion
    6. Find the largest profile in the image, probably the barcode

It is important to note that this method makes assumptions about the image gradient representation and is therefore only valid for horizontal barcodes.

If you want to implement a more robust barcode detection algorithm, you need to consider the direction of the image, or better, apply machine learning techniques such as haar cascade or hog + Linear SVM to scan the image barcode area.

SOURCE Download: http://pan.baidu.com/s/1ntys565

Detect barcodes on images with Python and OpenCV

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.