OpenCV每日函數(shù)之BarcodeDetector類條碼檢測器
一、概述
OpenCV在V4.5.3版本的contrib包中提供了一個barcode::BarcodeDetector類,用于條形碼的識別。
二、類參考
1、函數(shù)原型
構造方法
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 | 找到的條形碼矩形的頂點的可選輸出向量。 如果找不到,則為空。 |
支持的條形碼類型如下。
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類型的應該支持才對,暫時不進行深究。
到此這篇關于OpenCV每日函數(shù)BarcodeDetector條碼檢測器的文章就介紹到這了,更多相關OpenCV條碼檢測器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python常見庫matplotlib學習筆記之畫圖文字的中文顯示
在Python中使用matplotlib或者plotnine模塊繪圖時,常常出現(xiàn)圖表中無法正常顯示中文的問題,下面這篇文章主要給大家介紹了關于Python常見庫matplotlib學習筆記之畫圖文字的中文顯示的相關資料,需要的朋友可以參考下2023-05-05
使用Django Form解決表單數(shù)據(jù)無法動態(tài)刷新的兩種方法
這篇文章主要介紹了使用Django Form解決表單數(shù)據(jù)無法動態(tài)刷新的兩種方法,需要的朋友可以參考下2017-07-07
將字典轉換為DataFrame并進行頻次統(tǒng)計的方法
下面小編就為大家分享一篇將字典轉換為DataFrame并進行頻次統(tǒng)計的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04

