简体   繁体   English

我想使用OpenCV来检测静态图像中的2D条形码,但是文档似乎没有涵盖它

[英]I want to use OpenCV to detect 2D barcodes in a static image but the documentation doesn't seem to cover it

OpenCV has some tutorials that deal with trying to detect patterns in a live video stream. OpenCV的一些教程涉及尝试检测实时视频流中的模式。 Eg: 例如:

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

However, that is not really what I'm trying to do. 但是,这并不是我真正想做的。 I have static images, such as .jpgs, that include 2D barcodes. 我有包含2D条码的静态图像,例如.jpgs。

My goal is to isolate one or more 2D barcodes from the image. 我的目标是从图像中分离一个或多个2D条形码。 If the .jpg is 1000 pixels by 500 pixels, and the 2D barcode is just 200 pixels by 200 pixels, I just want to save the 200x200 pixel sample to an output file. 如果.jpg为1000像素乘500像素,而2D条形码仅为200像素乘200像素,我只想将200x200像素的样本保存到输出文件中。

I suspect that this requires either a Haar cascade or an LBP cascade. 我怀疑这需要Haar级联或LBP级联。 I suspect feature detection won't be able to do this. 我怀疑功能检测将无法执行此操作。

However, I can't find any tutorials that address this problem. 但是,我找不到解决此问题的任何教程。

Further, the opencv distribution automatically builds some executables that seem to be related, such as opencv_perf_objdetect and opencv_test_objdetect, but they don't seem to correspond to the tutorials, nor to anything else in the documentation. 此外,opencv发行版会自动构建一些似乎相关的可执行文件,例如opencv_perf_objdetect和opencv_test_objdetect,但它们似乎与教程以及文档中的其他内容都不对应。

Question: Is the problem of how to detect sub-images within a static image actually explained somewhere in the OpenCV documentation? 问题:在OpenCV文档中的某个地方是否真正解释了如何检测静态图像中的子图像的问题? If so, where? 如果是这样,在哪里?

Thanks. 谢谢。

I was recently working on a barcode detection project. 我最近正在从事条形码检测项目。 At the beginning I supposed that a simple machine learning algorithm combined with a texture-based descriptor can resolve the barcode detection. 一开始,我认为简单的机器学习算法与基于纹理的描述符相结合就可以解决条形码检测问题。 However, I have experienced several problems because in the case of my application I don't know if there is a barcode or not, it's size, it's type (UPC-A, EAN …), it's orientation… which supposes to try many combinations in order to localize the barcode. 但是,我遇到了几个问题,因为在我的应用程序中,我不知道是否有条形码,它的大小,它的类型(UPC-A,EAN…),它的方向……这想尝试许多组合为了定位条形码。

I also didn't dispose of or have the time to create a training dataset adequate to the type of images I have, so I didn't continue with this solution. 我也没有处理或没有时间创建适合我所拥有图像类型的训练数据集,因此我没有继续使用该解决方案。 Then I have read several articles. 然后,我读了几篇文章。 Many dedicated barcode detection methods start from the assumption that there is a barcode in the image and so they are trying to find it. 许多专用的条形码检测方法都是从图像中存在条形码的假设出发的,因此他们正在尝试找到它。 Moreover, some of the algorithms suppose that the barcode is horizontal, and use this hypothesis as an a priori information. 而且,一些算法假设条形码是水平的,并且使用该假设作为先验信息。

The best solution I have found is BLaDE ( http://www.ski.org/Rehab/Coughlan_lab/BLaDE/BLaDE_TechReport.pdf ). 我发现最好的解决方案是BLaDE( http://www.ski.org/Rehab/Coughlan_lab/BLaDE/BLaDE_TechReport.pdf )。 The code is also available on the web, so you can easily test it. 该代码也可以在网上获得,因此您可以轻松对其进行测试。 The only problem is that it was designed only for UPC-A barcodes. 唯一的问题是它仅适用于UPC-A条形码。

To resume, the best solution for you depends on several aspects: 要恢复,最适合您的解决方案取决于几个方面:

  • the type of barcode 条码类型

  • you know for sure that there is a barcode 您肯定知道有条形码

  • it's orientation: any/a given angle 它的方向:任意/给定角度

  • real time application/device to run it 实时应用程序/设备来运行它

  • you dispose of a training set 您处置训练集

Good luck! 祝好运!

honestly, the basic procedure to detect something with a cascade is simple. 老实说,使用级联检测事物的基本过程很简单。

it does not matter, if you had a haar, lbp or hog one, also no matter if you trained it on faces, bananas or barcodes, it will just try to localize the thing it was trained on. 没关系,如果您有一个haar,lbp或猪猪,也无论您是用面部,香蕉或条形码训练它,它都只会尝试定位它所训练的东西。

// 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] ...
    // i.e. filter for the largest boundingRect()
    // Mat subimg = img(rects[i]); // 'cropped' subimage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM