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
- 运行条形码和QR码扫描仪:
Dynamsoft条形码阅读器可以从一幅图像中检测多个条形码和QR码。然而,图像质量影响检测精度。正如你在上面的图像中看到的,为了捕捉所有的条形码和二维码,我们需要增加镜头的景深。这样,条形码和二维码可能会变得太小而无法读取。为了解决这一问题,我们将摄像头拉近以获得高质量的扫描图像,然后使用OpenCV拼接API将多个条形码和二维码图像拼接成一幅全景图。
将多个条形码和QR码图像拼接成全景图
OpenCV存储库包含一个stitching.py展示如何使用OpenCV缝合器API的文件。
要实现全景拼接:
- 初始化stitcher对象:
modes = (cv.Stitcher_PANORAMA, cv.Stitcher_SCANS)
stitcher = cv.Stitcher.create(modes[1])
stitcher.setPanoConfidenceThresh(0.5)
- 为拼接包含条形码和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