您能否将此解决方案改进为接口OpenCV 2.4+;至Zxing 1D条形码阅读器

您能否将此解决方案改进为接口OpenCV 2.4+;至Zxing 1D条形码阅读器,opencv,barcode,zxing,Opencv,Barcode,Zxing,我没有在网上找到这个解决方案,只能自己想办法。因此,为了其他人的利益,我将此作为一个“问题”: 您能将OpenCV 2.4 +的工作界面改进到Z邢2.2 1D条码阅读器的C++版本吗?p> 下面是我正在工作但可能可以改进的实现: /** * Gary Bradski, Reading 1D barcodes * License BSD, (c) 2013 * * Working example of how to call zxing using OpenCV 2.4+ cv::

我没有在网上找到这个解决方案,只能自己想办法。因此,为了其他人的利益,我将此作为一个“问题”:

您能将OpenCV 2.4 +的工作界面改进到Z邢2.2 1D条码阅读器的C++版本吗?p> 下面是我正在工作但可能可以改进的实现:

/**
 * Gary Bradski, Reading 1D barcodes
 *   License BSD, (c) 2013
 *
 *   Working example of how to call zxing using OpenCV 2.4+ cv::Mat
 *
 * Calling example, this one for 128 barcodes:
 *
 *   Code128Reader cr; //Instantiate a zxing barcode reader, int this case for 128 barcodes, 
 *                     //  but you can use any of their 1D or multi readers here
 *   ... by magic, I find, rectify and islotate a barcode into cv::Mat barcodeImage
 *   decode_image(&cr, barcodeImage);  //Decode the isolated rectified barcode or fail
 *
 */

#include <string>
#include <fstream>
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

//////////////ZXING BARCODE READER//////////////////////////////////////////
#include <zxing/LuminanceSource.h>
#include <zxing/MultiFormatReader.h>
#include <zxing/oned/OneDReader.h>
#include <zxing/oned/EAN8Reader.h>
#include <zxing/oned/EAN13Reader.h>
#include <zxing/oned/Code128Reader.h>
#include <zxing/datamatrix/DataMatrixReader.h>
#include <zxing/qrcode/QRCodeReader.h>
#include <zxing/aztec/AztecReader.h>
#include <zxing/common/GlobalHistogramBinarizer.h>
#include <zxing/Exception.h>

using namespace zxing;
using namespace oned;
using namespace datamatrix;
using namespace qrcode;
using namespace aztec;

class OpenCVBitmapSource : public LuminanceSource
{
private:
    cv::Mat m_pImage;

public:
    OpenCVBitmapSource(cv::Mat &image)
    : LuminanceSource(image.cols, image.rows)
    {
        m_pImage = image.clone();
    }

    ~OpenCVBitmapSource(){}

    int getWidth() const { return m_pImage.cols; }    
    int getHeight() const { return m_pImage.rows; }

    ArrayRef<char> getRow(int y, ArrayRef<char> row) const //See Zxing Array.h for ArrayRef def
    {
        int width_ = getWidth();
        if (!row)
            row = ArrayRef<char>(width_);
        const char *p = m_pImage.ptr<char>(y);
        for(int x = 0; x<width_; ++x, ++p)
            row[x] = *p;
        return row;
    }

    ArrayRef<char> getMatrix() const
    {
        int width_ = getWidth();
        int height_ =  getHeight();
        ArrayRef<char> matrix = ArrayRef<char>(width_*height_);
        for (int y = 0; y < height_; ++y)
        {
            const char *p = m_pImage.ptr<char>(y);
            int yoffset = y*width_;
            for(int x = 0; x < width_; ++x, ++p)
            {
                matrix[yoffset + x] = *p;
            }
        }
        return matrix;
    }
    /*
    // The following methods are not supported by this demo (the DataMatrix Reader doesn't call these methods)
    bool isCropSupported() const { return false; }
    Ref<LuminanceSource> crop(int left, int top, int width, int height) {}
    bool isRotateSupported() const { return false; }
    Ref<LuminanceSource> rotateCounterClockwise() {}
    */
};

void decode_image(Reader *reader, cv::Mat &image)
{
    try
    {
        Ref<OpenCVBitmapSource> source(new OpenCVBitmapSource(image));
        Ref<Binarizer> binarizer(new GlobalHistogramBinarizer(source));
        Ref<BinaryBitmap> bitmap(new BinaryBitmap(binarizer));
        Ref<Result> result(reader->decode(bitmap, DecodeHints(DecodeHints::TRYHARDER_HINT)));//+DecodeHints::DEFAULT_HINT)));
        cout << result->getText()->getText() << endl;
            //Export the read barcode here
    }
    catch (zxing::Exception& e)
    {
            //Export your failure to read the code here
        cerr << "Error: " << e.what() << endl;
    }
}
/**
*加里·布拉德斯基,正在读一维条形码
*许可证BSD,(c)2013
*
*如何使用OpenCV 2.4+cv::Mat调用zxing的工作示例
*
*调用示例,此示例用于128个条形码:
*
*code128cr//实例化一个zxing条形码阅读器,在本例中输入128个条形码,
*//但是您可以在这里使用他们的任何1D或多读卡器
*   ... 通过魔法,我找到、纠正并将条形码旋转到cv::Mat barcodeImage中
*解码图像(&cr,条形码图像)//解码隔离的已纠正条形码或失败
*
*/
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
//////////////ZXING条形码阅读器//////////////////////////////////////////
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间zxing;
使用名称空间;
使用命名空间数据矩阵;
使用名称空间编码;
使用名称空间aztec;
类OpenCVBitmapSource:公共亮度源
{
私人:
cv::Mat m_pImage;
公众:
OpenCVBitmapSource(cv::Mat和image)
:亮度源(image.cols、image.rows)
{
m_pImage=image.clone();
}
~OpenCVBitmapSource(){}
int getWidth()常量{return m_pImage.cols;}
int getHeight()常量{return m_pImage.rows;}
ArrayRef getRow(int y,ArrayRef row)常量//有关ArrayRef定义,请参见Zxing Array.h
{
int width=getWidth();
如果(!行)
行=ArrayRef(宽度);
常数char*p=m_pImage.ptr(y);

对于(int x=0;x我认为可以通过替换

Ref<OpenCVBitmapSource> source(new OpenCVBitmapSource(image));
Ref源代码(新的OpenCVBitmapSource(图像));

Ref-source(新的灰度标尺亮度源(image.data、image.step、image.rows、0、0、image.cols、image.rows));

这可能更适合于Oybe。我开始认为需要有一个更深的端口,以便OpenCV在整个过程中使用。我讨厌分散的分配,我喜欢一次将它们全部放在前端。我也不喜欢将检测器粘贴到读卡器中。我将检测到的校正图像传递给它。我希望能够模块化地改进该部分。读者是“meh”,在一个具有挑战性的场景中,它可以得到50%左右。我想它可以通过肉眼得到80%左右。我不确定我是否理解你所说的“更深”的意思。本质上,OpenCV在这里扮演的角色只是一个围绕原始字符缓冲区的数据结构。
Ref source(新的GreyscaleLuminanceSource(image.data、image.step、image.rows、0、0、image.cols、image.rows));
只需将cv::Mat转换为ZXing首选的数据结构,只需非常小的内存分配。如果您想使用自己的检测区域,您仍然可以传入ROI。此处未显示的是我的1D条形码检测器,所有这些都在OpenCV的几行中,可以在一个极具挑战性的数据集中查找所有条形码实例(从正面平行到超过60度透视+旋转的系统变化)。我现在看到了一种更好的方法,那就是你可以对多边形进行推理。因此,我希望检测器和阅读器能够更好地分离。对于阅读器,我想添加对多边形进行推理的方法,然后是使用模式识别的方法(Gallo&Manduchi)即使某些零交叉丢失,它也可以读取模式。感谢cxyzs7,这比idealsoftware.com使用的更清晰。但是直接提供指针对我来说不起作用(v2.4.8)。我必须提供ArrayRef:GreyscaleLuminanceSource(ArrayRef((char*)image.data,image.step*image.rows),image.step,image.rows,0,0,image.cols,image.rows)。请提供建议。
Ref<LuminanceSource> source(new GreyscaleLuminanceSource(image.data, image.step, image.rows, 0, 0, image.cols, image.rows));