OpenCV每日函數(shù)之BarcodeDetector類條碼檢測器
一、概述
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 編碼的字符串輸出向量或字符串的空向量(如果代碼無法解碼)。 |
| 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版本的需要打開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è)裁剪出來識(shí)別看看。


最后一個(gè)沒有識(shí)別出來

把最后一個(gè)單獨(dú)裁剪出來在測試下也沒有識(shí)別出來,不過UPCE類型的應(yīng)該支持才對(duì),暫時(shí)不進(jìn)行深究。
到此這篇關(guān)于OpenCV每日函數(shù)BarcodeDetector條碼檢測器的文章就介紹到這了,更多相關(guān)OpenCV條碼檢測器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
GitHub上值得推薦的8個(gè)python 項(xiàng)目
GitHub 無疑是代碼托管領(lǐng)域的先行者,Python 作為一種通用編程語言,已經(jīng)被千千萬萬的開發(fā)人員用來構(gòu)建各種有意思或有用的項(xiàng)目。以下我們會(huì)介紹一些使用 Python 構(gòu)建的GitHub上優(yōu)秀的項(xiàng)目。2020-10-10
Python常見庫matplotlib學(xué)習(xí)筆記之畫圖文字的中文顯示
在Python中使用matplotlib或者plotnine模塊繪圖時(shí),常常出現(xiàn)圖表中無法正常顯示中文的問題,下面這篇文章主要給大家介紹了關(guān)于Python常見庫matplotlib學(xué)習(xí)筆記之畫圖文字的中文顯示的相關(guān)資料,需要的朋友可以參考下2023-05-05
使用Django Form解決表單數(shù)據(jù)無法動(dòng)態(tài)刷新的兩種方法
這篇文章主要介紹了使用Django Form解決表單數(shù)據(jù)無法動(dòng)態(tài)刷新的兩種方法,需要的朋友可以參考下2017-07-07
將字典轉(zhuǎn)換為DataFrame并進(jìn)行頻次統(tǒng)計(jì)的方法
下面小編就為大家分享一篇將字典轉(zhuǎn)換為DataFrame并進(jìn)行頻次統(tǒng)計(jì)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04

