Opencv最强案例——使用摄像头、OpenCV和Python扫描条形码和二维码。

try:

results = reader.decode_buffer(frame)

except BarcodeReaderError as bre:

print(bre)

return results

while True:

ret, frame = cap.read()

while len(barcodeTasks) > 0 and barcodeTasks[0].ready():

results = barcodeTasks.popleft().get()

if results != None:

for result in results:

points = result.localization_result.localization_points

cv.line(frame, points[0], points[1], (0,255,0), 2)

cv.line(frame, points[1], points[2], (0,255,0), 2)

cv.line(frame, points[2], points[3], (0,255,0), 2)

cv.line(frame, points[3], points[0], (0,255,0), 2)

cv.putText(frame, result.barcode_text, points[0], cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255))

if len(barcodeTasks) < threadn:

task = pool.apply_async(process_frame, (frame.copy(), ))

barcodeTasks.append(task)

cv.imshow(‘Barcode & QR Code Scanner’, frame)

ch = cv.waitKey(1)

if ch == 27:

break

  1. 运行条形码和QR码扫描仪:

Dynamsoft条形码阅读器可以从一幅图像中检测多个条形码和QR码。然而,图像质量影响检测精度。正如你在上面的图像中看到的,为了捕捉所有的条形码和二维码,我们需要增加镜头的景深。这样,条形码和二维码可能会变得太小而无法读取。为了解决这一问题,我们将摄像头拉近以获得高质量的扫描图像,然后使用OpenCV拼接API将多个条形码和二维码图像拼接成一幅全景图。

将多个条形码和QR码图像拼接成全景图

OpenCV存储库包含一个stitching.py展示如何使用OpenCV缝合器API的文件。

要实现全景拼接:

  1. 初始化stitcher对象:

modes = (cv.Stitcher_PANORAMA, cv.Stitcher_SCANS)

stitcher = cv.Stitcher.create(modes[1])

stitcher.setPanoConfidenceThresh(0.5)

  1. 为拼接包含条形码和QR码的图像创建新的任务功能:

panoramaPool = ThreadPool(processes = threadn)

panoramaTask = deque()

def stitch_frame(self, frame):

try:

results = self.reader.decode_buffer(frame)

if results != None:

for result in results:

points = result.localization_result.localization_points

cv.line(frame, points[0], points[1], (0,255,0), 2)

cv.line(frame, points[1], points[2], (0,255,0), 2)

cv.line(frame, points[2], points[3], (0,255,0), 2)

cv.line(frame, points[3], points[0], (0,255,0), 2)

cv.putText(frame, result.barcode_text, points[0], cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255))

self.panorama.append((frame, len(results)))

print(‘Stitching …’)

try:

all_images = [frame for frame, count in self.panorama]

status, image = self.stitcher.stitch(all_images)

if status != cv.Stitcher_OK:

print(“Can’t stitch images, error code = %d” % status)

return self.panorama[0][0]

else:

Stop stitching if the output image is out of control

if image.shape[0] >= frame.shape[0] * 1.5:

self.isPanoramaDone = True

self.save_frame(all_images[0])

print(‘Stitching is done…’)

return None

Drop the stitched image if its quality is not good enough

total = 0

for frame, count in self.panorama:

total += count

count_stitch = self.count_barcodes(image)

if count_stitch > total or count_stitch < self.panorama[0][1]:

return self.panorama[0][0]

Wait for the next stitching and return the current stitched image

self.panorama = [(image, count_stitch)]

return image

except Exception as e:

print(e)

return None

except BarcodeReaderError as e:

print(e)

return None

return None

while len(panoramaTask) > 0 and panoramaTask[0].ready():

image = panoramaTask.popleft().get()

if image is not None:

cv.imshow(‘panorama’, image)

if len(panoramaTask) < threadn:

task = panoramaPool.apply_async(self.stitch_frame, (frame_cp, ))

panoramaTask.append(task)

在这里插入图片描述

感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的:

① 2000多本Python电子书(主流和经典的书籍应该都有了)

② Python标准库资料(最全中文版)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)

加入社区:https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0

OpenCV+zbar开源库实现摄像头识别二维码,测试验证识别率非常高,已实现简单的应用。 打包源码在VS2013下可以完全编译成功,附加包含OpenCV库及zbar-0.10-setup.exe,zbar-0.10.tar.bz2 下载Demo后需要安装 zbar-0.10-setup.exe 以下代码可以可以完成整个流程的开发,也可以贡献积分下载资源包。 1、 环境准备 (1) OpenCV库2.49 (2) ZBar开源库 (3) VS2013 2、 VS2013环境配置 (1) 配置附加包含目录 C/C++ -- 附加包含目录 include\opencv\include\ include\opencv\include\opencv include\opencv\include\opencv2 include (2) 配置链接器 链接器 -- 附加库目录 lib32\opencv\lib lib32 (3) 配置链接器 链接器--输入--附加依赖项 opencv_core249d.lib opencv_highgui249d.lib opencv_imgproc249d.lib libzbar-0.lib 3、 代码开发 (1)包含头文件 include include include include include include using namespace std; using namespace zbar; using namespace cv; (2)实现函数 void MatToCImage(cv::Mat &mat, CImage &cImage) { //create new CImage int width = mat.cols; int height = mat.rows; int channels = mat.channels(); cImage.Destroy(); //clear cImage.Create(width, height, 8 * channels); //默认图像像素单通道占用1个字节 //copy values uchar* ps; uchar* pimg = (uchar*)cImage.GetBits(); //A pointer to the bitmap buffer int step = cImage.GetPitch(); for (int i = 0; i (i)); for (int j = 0; j GetDlgItem(IDC_STATIC_IMG)->GetClientRect(▭); cv::VideoCapture capture(0);//从摄像头读入图像 while (!m_bCloseCamera) { cv::Mat frame; capture >> frame; cv::Mat newframe; cv::Size ResImgSiz = cv::Size(rect.Width(), rect.Height()); cv::resize(frame, newframe, ResImgSiz, CV_INTER_CUBIC); MatToCImage(newframe, imgDst); imgDst.Draw(pThis->GetDlgItem(IDC_STATIC_IMG)->GetDC()->GetSafeHdc(), rect); ImageScanner scanner; scanner.set_config(ZBAR_NONE, ZBAR_CFG_ENABLE, 1); Mat imageGray; cvtColor(frame, 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()) { } else { iIndex++; if (iIndex > 999999) { iIndex = 0; } for (; symbol != imageZbar.symbol_end(); ++symbol) { char szInfo[1024]; memset(szInfo, 0, sizeof(szInfo)); sprintf(szInfo, "[d]类型:%s\r\n条码:%s\r\n", iIndex , symbol->get_type_name().c_str(), symbol->get_data().c_str()); pThis->GetDlgItem(IDC_EDIT1)->SetWindowText(szInfo); } } imageZbar.set_data(NULL, 0); } imgDst.Destroy(); capture.release(); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值