OpenCV每日函数之BarcodeDetector类条码检测器
一、概述
OpenCV在V4.5.3版本的contrib包中提供了一个barcode::BarcodeDetector类,用于条形码的识别。
二、类参考
1、函数原型
构造方法
1 2 3 | cv::barcode::BarcodeDetector::BarcodeDetector ( const std::string & prototxt_path = "", const std::string & model_path = "" ) |
decode方法
1 2 3 4 5 | bool cv::barcode::BarcodeDetector::decode ( InputArray img, InputArray points, std::vector< std::string > & decoded_info, std::vector< BarcodeType > & decoded_type ) |
detect方法
detectAndDecode方法
1 2 3 4 5 | 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 | 找到的条形码矩形的顶点的可选输出向量。 如果找不到,则为空。 |
支持的条形码类型如下。
1 2 3 4 5 6 7 8 | 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、源码路径
2、源码代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | 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++代码进行示例。
1 2 3 4 5 6 | 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类型的应该支持才对,暂时不进行深究。
到此这篇关于OpenCV每日函数BarcodeDetector条码检测器的文章就介绍到这了,更多相关OpenCV条码检测器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
Python内置的HTTP协议服务器SimpleHTTPServer使用指南
这篇文章主要介绍了Python内置的HTTP协议服务器SimpleHTTPServer使用指南,SimpleHTTPServer本身的功能十分简单,文中介绍了需要的朋友可以参考下2016-03-03django rest framework 实现用户登录认证详解
这篇文章主要介绍了django rest framework 实现用户登录认证详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-07-07
最新评论