用不同的QR Code识别库实现二维码的识别(第二篇:zbar 、zxing和quirc)

上一篇介绍了使用opencv和boofcv再嵌入式平台上的识别效果。这一篇继续使用上面的方法,依然使用python编写代码测试zbar和zxing的效果。

1、zbar测试

首先按照pyzbar的教程安装完zbar。测试代码依然延续前面的。使用opencv读取视频流,使用zbar解码图片。操作比较简单。zbar除了qrcode还可以识别其它类型的条形码和其它类型二维码。我这里只测试qrcode的。代码如下:

import numpy as np
from  pyzbar.pyzbar import decode
from  pyzbar.pyzbar import ZBarSymbol
import cv2
import os
import time


video_path = '~/Downloads/'

# VideoCapture
cap = cv2.VideoCapture(0, cv2.CAP_V4L2)
cap.set(3, 1280)
cap.set(4, 720)
cap.set(5, 30)

# VideoWriter
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
outVideo = cv2.VideoWriter()
outVideo.open('output.mp4',fourcc,10.0,(1280,720), True )

# QRCodeDetector
findQR = False
qrResult = ''



print('Demo will work')
cnt = 0

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        #frame = cv2.flip(frame,-1)
        outVideo.write(frame)
        
        # QR Code Detector
        if not findQR :
            frame_mono = cv2.cvtColor(np.uint8(frame), cv2.COLOR_BGR2GRAY)
            start = time.time_ns()
            barcodes = decode(frame_mono, symbols=[ZBarSymbol.QRCODE])
            if len(barcodes)>0:
                end = time.time_ns()
                print("running time is ", str(end - start))
                print("find QR Code " )
                findQR = True
                qrResult = barcodes[0].data.decode('utf-8')

    else:
        print('fail to open camera')
        break
    cnt += 1
    if cnt > 100 :
        print('Have created output.mp4')
        break

if findQR:
    print("QR Code  is: {}".format(qrResult))
else:
    print("Didn't find QR Code!")

cap.release()
outVideo.release()
cv2.destroyAllWindows()


    

单独用手机展示第一篇中的两张二维码测试,效果如下:

可以看出zbar的识别速度只有130ms左右,再现在这款嵌入式平台上碾压pyboof和opencv。识别效果也很不错。在测试过程中没有发现错误识别和识别不到的情况。(我的测试比较常规,没有进行遮挡。但手机测试应该一定的扭曲、颠倒。)

2、zxing

这里使用比较容易安装的python版本的zxing,可能性能会有所损失

https://github.com/dlenski/python-zxing 确保java安装成功之后,直接使用pip3 install zxing

import numpy as np
import zxing
import cv2
import os
import time


video_path = '~/Downloads/'

# VideoCapture
cap = cv2.VideoCapture(0, cv2.CAP_V4L2)
cap.set(3, 1280)
cap.set(4, 720)
cap.set(5, 30)

# VideoWriter
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
outVideo = cv2.VideoWriter()
outVideo.open('output.mp4',fourcc,10.0,(1280,720), True )

# QRCodeDetector
findQR = False
qrResult = ''
reader = zxing.BarCodeReader()


print('Demo will work')
cnt = 0

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        #frame = cv2.flip(frame,-1)
        outVideo.write(frame)
        
        # QR Code Detector
        if not findQR :
            frame_mono = cv2.cvtColor(np.uint8(frame), cv2.COLOR_BGR2GRAY)
            cv2.imwrite("temp.bmp", frame_mono)
            start = time.time_ns()
            barcode = reader.decode("temp.bmp", possible_formats = 'QR_CODE')
            if barcode is not None:
                end = time.time_ns()
                print("running time is ", str(end - start))
                print("find QR Code " )
                findQR = True
                qrResult = barcode.parsed

    else:
        print('fail to open camera')
        break
    cnt += 1
    if cnt > 100 :
        print('Have created output.mp4')
        break

if findQR:
    print("QR Code  is: {}".format(qrResult))
else:
    print("Didn't find QR Code!")

cap.release()
outVideo.release()
cv2.destroyAllWindows()


    

这个版本的zxing非常不好用,只能读取图片。所以视频流需要先存为照片然后再读入内存。这应该会耗费很多时间。

测试结果也非常令人遗憾:

是已经测试的几个中最差的,用时已经几乎是1.5秒左右了。而且识别效果似乎也不太好。

 

3、quirc

最初觉得比较幸运的是quirc也有python版本:https://github.com/svartalf/python-quirc。但灯测试时发现这个项目年久失修,无论python3还是python都不可用。考虑到opencv实际上是借助quirc来实现二维码识别的。暂时认为效果差不多。

既然开始了这个项目,不妨用c语言继续测试quirc。

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
要在C++中实现高精度的二维码识别,您可以使用一些开源的图像处理二维码识别。以下是一般的步骤: 1. 安装并配置图像处理二维码识别:选择适合您需求的,如OpenCV用于图像处理,ZBarZXing用于二维码识别。请按照它们的安装指南进行安装和配置。 2. 加载图像并进行预处理:使用图像处理加载图像,并进行预处理来增强二维码的边缘和特征。这可能包括调整图像大小、转换为灰度图像、二值化、去噪等操作。 3. 进行二维码识别:使用二维码识别对预处理后的图像进行识别。这通常涉及在图像中检测和解码二维码。 以下是一个简单的C++示例,使用OpenCV进行图像处理和ZBar进行二维码识别: ```cpp #include <iostream> #include <opencv2/opencv.hpp> #include <zbar.h> int main() { // 加载图像 cv::Mat image = cv::imread(&quot;path/to/qr_code.png&quot;, cv::IMREAD_GRAYSCALE); // 创建ZBar扫描器 zbar::ImageScanner scanner; // 配置ZBar扫描器 scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1); // 将图像数据传递给ZBar zbar::Image zbarImage(image.cols, image.rows, &quot;Y800&quot;, image.data, image.cols * image.rows); // 扫描二维码 scanner.scan(zbarImage); // 从扫描结果中获取识别结果 for (zbar::Image::SymbolIterator symbol = zbarImage.symbol_begin(); symbol != zbarImage.symbol_end(); ++symbol) { std::cout << &quot;解码结果:&quot; << symbol->get_data() << std::endl; } // 释放ZBar图像和扫描器 zbarImage.set_data(NULL, 0); return 0; } ``` 请注意,这只是一个基本的示例,并且可能需要根据您的具体需求进行调整。您可能还需要处理异常情况、优化图像处理和识别算法等。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值