oepncv读取条码

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;    
}       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值