I want to use OpenCV to detect 2D barcodes in a static image, but the documentation does not seem to cover it

OpenCV has several guides on trying to detect patterns in a live video stream. For instance:.

http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html

However, this is not exactly what I am trying to do. I have static images like .jpg that include 2D barcodes.

My goal is to isolate one or more 2D barcodes from the image. If the .jpg is 1000 pixels by 500 pixels and the 2D barcode is only 200 pixels by 200 pixels, I just want to save a 200x200 pixel sample to the output file.

I suspect that this requires either the Haar cascade or the LBP cascade. I suspect that detecting a function will not be able to do this.

However, I can not find textbooks on this issue.

Also, the opencv distribution automatically creates some executables that seem to be related to each other, such as opencv_perf_objdetect and opencv_test_objdetect, but they don't seem to match the tutorials and anyone else in the documentation.

Question: Is the problem of detecting sub-images inside a static image actually explained somewhere in the OpenCV documentation? If so, where?

Thanks.

+6
source share
2 answers

I recently worked on a barcode detection project. At first, I assumed that a simple machine learning algorithm combined with a texture-based descriptor could enable barcode detection. However, I had several problems, because in the case of my application, I do not know if there is a barcode or not, its size, its type (UPC-A, EAN ...), its orientation ... which suggests trying a lot of combinations in order to localize the barcode.

I also did not order or did not manage to create a training dataset corresponding to the type of images that I have, so I did not continue this decision. Then I read some articles. Many specialized barcode detection methods begin with the assumption that there is a barcode in the image, and so they try to find it. Moreover, some of the algorithms suggest that the barcode is horizontal, and use this hypothesis as a priori information.

The best solution I have found is BLaDE ( http://www.ski.org/Rehab/Coughlan_lab/BLaDE/BLaDE_TechReport.pdf ). The code is also available on the Internet, so you can easily test it. The only problem is that it was designed for UPC-A barcodes only.

To resume work, the best solution for you depends on several aspects:

  • barcode type

  • you know exactly what the barcode is

  • its orientation: any / given angle

  • application / real-time device to run it

  • you have chosen a training set

Good luck

+2
source

frankly, the basic procedure for finding something with a cascade is simple.

It doesn’t matter if you had haraam, lbp or hog alone, also regardless of whether you trained it on faces, bananas or barcodes, it will just try to localize the subject on which it was trained.

// first load the cascade string cascade_file = "my_barcode.xml"; CascadeClassifier cascade; if( !cascade.load( cascade_filee ) ){ printf("Error loading cascade\n"); return -1; }; // then we need a test image, should be grayscale (but will get converted internally if not) Mat img = imread("mybar.png", 0); // 0==>"load gray" if ( img.empty() ) { /* only fools don't check resource loading */ } // now we can check if it found something, we'll get a Rect for each found item: std::vector<Rect> rects; cascade.detectMultiScale( img, rects, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) ); for ( size_t i = 0; i < rects.size(); i++ ) // do something with rects[i] ... // ie filter for the largest boundingRect() // Mat subimg = img(rects[i]); // 'cropped' subimage 
0
source

All Articles