ZXing C++ + opencv 识别二维码二维码

ZXing-C++源码编译(linux环境)

        ZXing-CPP是一个用C++实现的开源、多格式一维与二维条形码图像处理库。它最初是从Java ZXing库移植而来的,但经过进一步开发,现在在运行时和检测性能方面有了许多改进。它可以读取和写入多种格式的条形码。包括工业DM码、RQ码、以及其他常见的各种一维条形码。zbar也是一个和zxing-c++一样的c++解码库,但是zbar在解码性能,种类上都远远没有zxing好,在检测多个二维码的情况下zbar只要几帧但是zxing可以跑到60帧以上,下面说一下zxing c++源码获取和库的使用。

源码路径:
zxing-cpp/zxing-cpp: C++ port of ZXing (github.com)https://github.com/zxing-cpp/zxing-cpp下载解压完
进入目录

mkdir build install && cd build

cmake -DCMAKE_INSTALL_PREFIX=../install ../

make -j8

make install

生成库文件在install下面,可以运行build/example目录下的测试代码,查看是否编译成功。

opencv下运行代码

        在使用opencv 把图片格式转换成灰度图,解码速度比直接传入BGR图片快十多帧。

    auto zimage = ZXing::ImageView(gray.data, width, height, ZXing::ImageFormat::Lum);   //设置输入图片类型 GRAY
    auto options = ZXing::ReaderOptions().setFormats(ZXing::BarcodeFormat::QRCode);     //设置解码类型 Any 全部
    auto barcodes = ZXing::ReadBarcodes(zimage,options);
    for (const auto& barcode : barcodes){
        auto pos = barcode.position();
        auto zx2cv = [](ZXing::PointI p) { return cv::Point(p.x, p.y); };
        auto contour = std::vector<cv::Point>{zx2cv(pos[0]), zx2cv(pos[1]), zx2cv(pos[2]), zx2cv(pos[3])};
        const auto* pts = contour.data();
        int npts = contour.size();

        cv::polylines(ori_img, &pts, &npts, 1, true, CV_RGB(0, 255, 0),4);
        cv::putText(ori_img, barcode.text(), zx2cv(pos[3]) + cv::Point(0, 20), cv::FONT_HERSHEY_DUPLEX, 0.5, CV_RGB(255, 0, 0));
    }

解码当张图片速度是zbar的十倍

下面是使用多线程解码二维码视频的完整代码。

#include "zbar.h"
#include "ThreadPool.hpp"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <queue>
#include <string>
#include <vector>
#include <sys/time.h>

#include "ZXing/ReadBarcode.h"

using namespace std;
using namespace cv;
using std::queue;
using std::time;
using std::time_t;

vector<string> Data_Array;
Scalar color;


class zbar_lite
{
private:
    int ret;
    int width = 0;
    int height = 0;
public:
    Mat ori_img;
    int Read_Decode_Pic();
    zbar_lite();
    ~zbar_lite();
};

zbar_lite::zbar_lite()
{
    /* Create the neural network */
    printf("Loading mode...\n");
}

zbar_lite::~zbar_lite()
{
    printf("Loading delete...\n");
}

int zbar_lite::Read_Decode_Pic()
{	
	cv::Mat gray;
	if (ori_img.channels() == 1) gray = ori_img;
	else cv::cvtColor(ori_img, gray, COLOR_BGR2GRAY);
	int width 	= gray.cols;
	int height 	= gray.rows;

    auto zimage = ZXing::ImageView(gray.data, width, height, ZXing::ImageFormat::Lum);   //设置输入图片类型 GRAY
    auto options = ZXing::ReaderOptions().setFormats(ZXing::BarcodeFormat::QRCode);     //设置解码类型 Any 全部
    auto barcodes = ZXing::ReadBarcodes(zimage,options);
    for (const auto& barcode : barcodes){
        auto pos = barcode.position();
        auto zx2cv = [](ZXing::PointI p) { return cv::Point(p.x, p.y); };
        auto contour = std::vector<cv::Point>{zx2cv(pos[0]), zx2cv(pos[1]), zx2cv(pos[2]), zx2cv(pos[3])};
        const auto* pts = contour.data();
        int npts = contour.size();

        cv::polylines(ori_img, &pts, &npts, 1, true, CV_RGB(0, 255, 0),4);
        cv::putText(ori_img, barcode.text(), zx2cv(pos[3]) + cv::Point(0, 20), cv::FONT_HERSHEY_DUPLEX, 0.5, CV_RGB(255, 0, 0));
    }  

    return 0;
}

int main(int argc, char** argv)
{
  if(argc < 2){
		
		std::cout << "Usage: ./test_zbar barcode.png" << std::endl;
		return -1;
	}
     char *image_name = argv[1];
    cv::VideoCapture capture;
    capture.open(image_name);
 
    int n = 12, frames = 0;
    printf("线程数:\t%d\n", n);
    vector<zbar_lite *> rkpool;
    // 线程池
    dpool::ThreadPool pool(n);
    // 线程队列
    queue<std::future<int>> futs;

    for (int i = 0; i < n; i++)
    {
        zbar_lite *ptr = new zbar_lite();
        rkpool.push_back(ptr);
        capture >> ptr->ori_img;
        futs.push(pool.submit(&zbar_lite::Read_Decode_Pic, &(*ptr)));
        // futs.push(pool.submit(Read_Decode_Pic, image));
    }

    struct timeval time;
    gettimeofday(&time, nullptr);
    auto initTime = time.tv_sec * 1000 + time.tv_usec / 1000;

    gettimeofday(&time, nullptr);
    long tmpTime, lopTime = time.tv_sec * 1000 + time.tv_usec / 1000;

    while (capture.isOpened())
    {
        if (futs.front().get() != 0) break;
        futs.pop();
        imshow("Camera FPS", rkpool[frames % n]->ori_img);
        if (cv::waitKey(1) == 'q') // 延时1毫秒,按q键退出
            break;
        if(!capture.read(rkpool[frames % n]->ori_img)) break;
        
        // 检测图像中的码(解码)
        futs.push(pool.submit(&zbar_lite::Read_Decode_Pic, &(*rkpool[frames++ % n])));

        if(frames % 60 == 0){
            gettimeofday(&time, nullptr);
            tmpTime = time.tv_sec * 1000 + time.tv_usec / 1000;
            printf("60帧平均帧率:\t%f帧\n", 60000.0 / (float)(tmpTime - lopTime));
            lopTime = tmpTime;
        }

    }
    gettimeofday(&time, nullptr);
    printf("\n平均帧率:\t%f帧\n", float(frames) / (float)(time.tv_sec * 1000 + time.tv_usec / 1000 - initTime + 0.0001) * 1000.0);

    while (!futs.empty())
    {
        if (futs.front().get())
        break;
        futs.pop();
    }
    for (int i = 0; i < n; i++)
        delete rkpool[i];
    capture.release();
    cv::destroyAllWindows();
    return 0;
}

运行帧率

60帧平均帧率:	133.037694帧
60帧平均帧率:	151.515152帧
60帧平均帧率:	136.986301帧
60帧平均帧率:	135.440181帧
60帧平均帧率:	145.278450帧
60帧平均帧率:	142.180095帧
60帧平均帧率:	126.582278帧
60帧平均帧率:	129.589633帧
60帧平均帧率:	121.212121帧
60帧平均帧率:	98.360656帧
60帧平均帧率:	70.671378帧
60帧平均帧率:	58.765916帧
60帧平均帧率:	55.970149帧
60帧平均帧率:	75.662043帧
60帧平均帧率:	56.818182帧
60帧平均帧率:	49.627792帧
60帧平均帧率:	51.194539帧
60帧平均帧率:	49.140049帧
60帧平均帧率:	59.464817帧
60帧平均帧率:	63.559322帧
60帧平均帧率:	53.908356帧
60帧平均帧率:	54.298643帧

平均帧率:	88.158511帧

在多二维码的情况下可以到60多帧

后续使用 yolov5+zxing可实现在多二维码情况下120帧

完整工程

tickley/QRcode (github.com)https://github.com/tickley/QRcode

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值