1.获取条码位置
https://zhuanlan.zhihu.com/p/367162545?ivk_sa=1024320u
opencv_contirb包
#include "opencv2/barcode.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
Ptr<barcode::BarcodeDetector> bardet = makePtr<barcode::BarcodeDetector>("sr.prototxt", "sr.caffemodel"); //如果不使用超分辨率则可以不指定模型路径
Mat input = imread("your file path");
Mat corners; //返回的检测框的四个角点坐标,如果检测到N个条码,那么维度应该是[N][4][2]
std::vector<std::string> decoded_info; //返回的解码结果,如果解码失败,则为空string
std::vector<barcode::BarcodeType> decoded_format; //返回的条码类型,如果解码失败,则为BarcodeType::NONE
bool ok = bardet->detectAndDecode(input, decoded_info, decoded_format, corners);
获取条码位置
TEST(Barcode_BarcodeDetector_detect_multi, detect_regression)
{
const std::string root = "barcode/multiple/";
datasetType validation = initValidation(root + "result.csv");
auto bardet = barcode::BarcodeDetector();
datasetType::iterator iterator = validation.begin();
while (iterator != validation.end())
{
std::string img = iterator->first;
size_t expect_corners_size = std::stoi(iterator->second);
std::string image_path = findDataFile(root + img);
Mat src = imread(image_path);
EXPECT_FALSE(src.empty()) << "Can't read image: " << image_path;
std::vector<Point> corners;
bardet.detect(src, corners);
EXPECT_EQ(corners.size(), expect_corners_size) << "Can't detect all barcodes: " << img;
iterator++;
}
}
zba解码
#include "zbar.h"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace zbar; //添加zbar名称空间
using namespace cv;
int main(int argc,char*argv[])
{
ImageScanner scanner;
scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
//Mat image = imread("Camera10-48-20191128100029-553-index-31-1.bmp");//从本机读取图像
Mat image = imread("QQ截图20191223085520.jpg");//从本机读取图像
if (!image.data)
{
cout << "请确认图片" << endl;
system("pause");
return 0;
}
Mat imageGray;
cvtColor(image,imageGray,CV_RGB2GRAY);
int width = imageGray.cols;
int height = imageGray.rows;
uchar *raw = (uchar *)imageGray.data;
Image imageZbar(width, height, "Y800", raw, width * height);
scanner.scan(imageZbar); //扫描条码
Image::SymbolIterator symbol = imageZbar.symbol_begin();
if(imageZbar.symbol_begin()==imageZbar.symbol_end())
{
cout<<"查询条码失败,请检查图片!"<<endl;
}
//获取码的位置信息
std::vector<Point> pts;
for (int i = 0; i < symbol->get_location_size(); i++)
{
int x = symbol->get_location_x(i);
int y = symbol->get_location_y(i);
pts.push_back(Point(x, y));
}
int n = pts.size();
printf("%d\n", n);
for (int j = 0; j < n; j++)
{
line(image, pts[j], pts[(j + 1) % n], Scalar(0, 255, 255), 3);
}
//输出码内容
for(;symbol != imageZbar.symbol_end();++symbol)
{
cout<<"类型:"<<endl<<symbol->get_type_name()<<endl<<endl;
cout<<"条码:"<<endl<<symbol->get_data()<<endl<<endl;
}
namedWindow("Image", CV_WINDOW_NORMAL);
imshow("Image", image);
namedWindow("imageGray", CV_WINDOW_NORMAL);
imshow("imageGray", imageGray);
waitKey();
imageZbar.set_data(NULL,0);
return 0;
}