OpenCV每日函数之BarcodeDetector类条码检测器
作者:坐望云起 发布时间:2023-03-28 02:22:39
标签:OpenCV,条码,检测,识别
一、概述
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_info | UTF8 编码的字符串输出向量或字符串的空向量(如果代码无法解码)。 |
decoded_type | BarcodeType 的向量,指定这些条形码的类型 |
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类型的应该支持才对,暂时不进行深究。
来源:https://blog.csdn.net/bashendixie5/article/details/125294547
0
投稿
猜你喜欢
- 现在很多地方都需要用到关键词过滤功能。比如一般的服务器都不允许一些词出现在网页上,站长有时候会对在本网站发布信息的内容进行一个广告过滤等。雨
- 前言最近学习了 django 的一个 restframework 框架,对于里面的执行流程产生了兴趣,经过昨天一晚上初步搞清楚了执行流程(部
- 上一篇我们写了怎么将xmind转换成想要的excel格式,这篇再讲一下用Python自带的tkinter库设计一个简单的gui界面,让我们的
- 在今天网络开发方面,JavaScript起了很关键的作用;像jQuery, MooTools, Prototype等等JavaScript框
- 本文实例为大家分享了python分割一个文本为多个文本,供大家参考,具体内容如下# load file# for each row## if
- <body oncontextmenu="return false" onselectstart="re
- 本文实例讲述了php隐藏IP地址后两位显示为星号的方法。分享给大家供大家参考。具体实现方法如下:我们在很多的公共网站中都会有碰到显示用户的I
- 数据列类型与查询效率选用适当的数据列类型有助于提高查询命令的执行速度,下面是几点关于如何选择合适数据列类型的建议:尽量选用尺寸较小的数据列。
- 如果不是因为总监审查严格,一定要求这个细节解决掉,也许我也不会去深究根源性的解决办法,再此感谢MTIME负责而严格的同事。首先描述一下问题:
- 高级语言不能直接被机器所理解执行,所以都需要一个翻译的阶段,解释型语言用到的是解释器,编译型语言用到的是编译器。编译型语言通常的执行过程是:
- 过去一段时间人们似乎又非常热衷于探讨网络文档的印刷格式,涌现了很多与之相关的技术与理论资料,其中相当重要的一个领域就是关于印刷中字号和行高的
- 一、概述机器学习算法在近几年大数据点燃的热火熏陶下已经变得被人所“熟知”,就算不懂得其中各算法理论,叫你喊上一两个著名算法的名字,你也能昂首
- 使用tf.trian.NewCheckpointReader(model_dir)一个标准的模型文件有一下文件, model_dir就是My
- 围绕一门语言,学习它的文化精髓,能让你成为一名更优秀的程序员。如果你还没读过Python之禅(Zen of Python) ,那么打开Pyt
- 在使用柱状图时,经常遇到需要多组数据进行比较的情况。绘制单个数据系列的柱形图比较简单,多组数据柱状图绘制的关键有三点:多次调用bar()函数
- 本文实例讲述了PHP实现的线索二叉树及二叉树遍历方法。分享给大家供大家参考,具体如下:<?php require '
- 一、标识符何为标识符?标识符是用来标识某个实体的一个符号。在编程语言中,标识符是计算机语言中作为名字的有效字符串集合。标识符是用户编程时使用
- SQL Server数据库涉及到的数据仓库概念:1.多维数据集:多维数据集是联机分析处理 (OLAP) 中的主要对象,是一项可对数据仓库中的
- 使用穷举法求两个数的最大公约数for m in range (0,2): a = int(input("
- 通常我们在制作上图的时候,会分别给四个div加上不同的css属性,来实现中间间隔。但我们更希望的是不需要对html标签做标识,直接能通过cs