OpenCV 条码识别

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

OpenCV 条码识别

坐望云起   2022-06-20 我要评论

一、概述

OpenCV在V4.5.3版本的contrib包中提供了一个barcode::BarcodeDetector类,用于条形码的识别。

二、类参考

1、函数原型

构造方法

cv::barcode::BarcodeDetector::BarcodeDetector	(	const std::string & 	prototxt_path = "",
const std::string & 	model_path = "" 
)	

decode方法

bool cv::barcode::BarcodeDetector::decode	(	InputArray 	img,
InputArray 	points,
std::vector< std::string > & 	decoded_info,
std::vector< BarcodeType > & 	decoded_type 
)	

detect方法

bool cv::barcode::BarcodeDetector::detect	(	InputArray 	img,
OutputArray 	points 
)	

detectAndDecode方法

bool cv::barcode::BarcodeDetector::detectAndDecode	(	InputArray 	img,
std::vector< std::string > & 	decoded_info,
std::vector< BarcodeType > & 	decoded_type,
OutputArray 	points = noArray() 
)

2、参数详解

img包含条形码的灰度或彩色 (BGR) 图像。
decoded_infoUTF8 编码的字符串输出向量或字符串的空向量(如果代码无法解码)。
decoded_typeBarcodeType 的向量,指定这些条形码的类型
points找到的条形码矩形的顶点的可选输出向量。 如果找不到,则为空。

支持的条形码类型如下。

enum  	cv::barcode::BarcodeType {
  cv::barcode::NONE,
  cv::barcode::EAN_8,
  cv::barcode::EAN_13,
  cv::barcode::UPC_A,
  cv::barcode::UPC_E,
  cv::barcode::UPC_EAN_EXTENSION
}

三、OpenCV源码

1、源码路径

opencv_contrib\modules\barcode\src\barcode.cpp

2、源码代码

bool BarcodeDetector::detect(InputArray img, OutputArray points) const
{
    Mat inarr;
    if (!checkBarInputImage(img, inarr))
    {
        points.release();
        return false;
    }
 
    Detect bardet;
    bardet.init(inarr);
    bardet.localization();
    if (!bardet.computeTransformationPoints())
    { return false; }
    vector<vector<Point2f>> pnts2f = bardet.getTransformationPoints();
    vector<Point2f> trans_points;
    for (auto &i : pnts2f)
    {
        for (const auto &j : i)
        {
            trans_points.push_back(j);
        }
    }
 
    updatePointsResult(points, trans_points);
    return true;
}
 
bool BarcodeDetector::decode(InputArray img, InputArray points, vector<std::string> &decoded_info,
                             vector<BarcodeType> &decoded_type) const
{
    Mat inarr;
    if (!checkBarInputImage(img, inarr))
    {
        return false;
    }
    CV_Assert(points.size().width > 0);
    CV_Assert((points.size().width % 4) == 0);
    vector<vector<Point2f>> src_points;
    Mat bar_points = points.getMat();
    bar_points = bar_points.reshape(2, 1);
    for (int i = 0; i < bar_points.size().width; i += 4)
    {
        vector<Point2f> tempMat = bar_points.colRange(i, i + 4);
        if (contourArea(tempMat) > 0.0)
        {
            src_points.push_back(tempMat);
        }
    }
    CV_Assert(!src_points.empty());
    vector<Mat> bar_imgs = p->initDecode(inarr, src_points);
    BarDecode bardec;
    bardec.init(bar_imgs);
    bardec.decodeMultiplyProcess();
    const vector<Result> info = bardec.getDecodeInformation();
    decoded_info.clear();
    decoded_type.clear();
    bool ok = false;
    for (const auto &res : info)
    {
        if (res.format != NONE)
        {
            ok = true;
        }
 
        decoded_info.emplace_back(res.result);
        decoded_type.emplace_back(res.format);
    }
    return ok;
}
 
bool
BarcodeDetector::detectAndDecode(InputArray img, vector<std::string> &decoded_info, vector<BarcodeType> &decoded_type,
                                 OutputArray points_) const
{
    Mat inarr;
    if (!checkBarInputImage(img, inarr))
    {
        points_.release();
        return false;
    }
    vector<Point2f> points;
    bool ok = this->detect(img, points);
    if (!ok)
    {
        points_.release();
        return false;
    }
    updatePointsResult(points_, points);
    decoded_info.clear();
    decoded_type.clear();
    ok = this->decode(inarr, points, decoded_info, decoded_type);
    return ok;
}

四、效果图像示例

示例图像

参考代码,opencvsharp版本的需要打开barcode并重新编译,所以使用c++代码进行示例。

cv::Mat mata = cv::imread("barcode.png");
cv::barcode::BarcodeDetector barcode;
std::vector<string> info;
std::vector<cv::barcode::BarcodeType> type;
Mat points;
barcode.detectAndDecode(mata, info, type, points);

识别结果,可以看到第一个和第三个识别结果正确,不知道是否是放在一起的原因,下面把另外两个裁剪出来识别看看。

 

最后一个没有识别出来

把最后一个单独裁剪出来在测试下也没有识别出来,不过UPCE类型的应该支持才对,暂时不进行深究。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们