OpenCV每日函數(shù)之BarcodeDetector類條碼檢測(cè)器
一、概述
OpenCV在V4.5.3版本的contrib包中提供了一個(gè)barcode::BarcodeDetector類,用于條形碼的識(shí)別。
二、類參考
1、函數(shù)原型
構(gòu)造方法
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、參數(shù)詳解
| img | 包含條形碼的灰度或彩色 (BGR) 圖像。 |
| decoded_info | UTF8 編碼的字符串輸出向量或字符串的空向量(如果代碼無(wú)法解碼)。 |
| decoded_type | BarcodeType 的向量,指定這些條形碼的類型 |
| points | 找到的條形碼矩形的頂點(diǎn)的可選輸出向量。 如果找不到,則為空。 |
支持的條形碼類型如下。
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版本的需要打開(kāi)barcode并重新編譯,所以使用c++代碼進(jìn)行示例。
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);識(shí)別結(jié)果,可以看到第一個(gè)和第三個(gè)識(shí)別結(jié)果正確,不知道是否是放在一起的原因,下面把另外兩個(gè)裁剪出來(lái)識(shí)別看看。


最后一個(gè)沒(méi)有識(shí)別出來(lái)

把最后一個(gè)單獨(dú)裁剪出來(lái)在測(cè)試下也沒(méi)有識(shí)別出來(lái),不過(guò)UPCE類型的應(yīng)該支持才對(duì),暫時(shí)不進(jìn)行深究。
到此這篇關(guān)于OpenCV每日函數(shù)BarcodeDetector條碼檢測(cè)器的文章就介紹到這了,更多相關(guān)OpenCV條碼檢測(cè)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django的models中on_delete參數(shù)詳解
這篇文章主要介紹了Django的models中on_delete參數(shù)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python爬蟲(chóng)scrapy框架Cookie池(微博Cookie池)的使用
這篇文章主要介紹了Python爬蟲(chóng)scrapy框架Cookie池(微博Cookie池)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
在Django的form中使用CSS進(jìn)行設(shè)計(jì)的方法
這篇文章主要介紹了在Django的form中使用CSS進(jìn)行設(shè)計(jì)的方法,Django是Python重多人氣開(kāi)發(fā)框架中最為著名的一個(gè),需要的朋友可以參考下2015-07-07
Python實(shí)現(xiàn)購(gòu)物車功能的方法分析
這篇文章主要介紹了Python實(shí)現(xiàn)購(gòu)物車功能的方法,結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)購(gòu)物車功能的具體步驟、相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-11-11
Python與Matlab混合編程的實(shí)現(xiàn)案例
本文主要介紹了Python與Matlab混合編程的實(shí)現(xiàn)案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

