如何使用OpenCV,Python和zbar检测单个QR码

时间:2015-01-29 09:18:36

标签: python opencv zbar

我正在使用AR Drone的底部相机来检测QR码,以便让我知道棋盘上无人机的下落。无人机盘旋在棋盘上,其中每个单个方格是保持位置的QR码(例如A1,C5,E7等)。当我按某个键时,它会扫描QR码并返回给我这个位置。

现在,我希望能够检测到许多QR码。由于无人机可能有多个QR码。因为我需要知道无人机在哪个准确的方格上,或者至少是最接近的方格(例如:A1上方2/3和A2上方1/3应该产生A1)。这是我目前正在使用的代码:

#!/usr/bin/python
from sys import argv
import zbar
import Image
import cv2

class DetectQRCode(object):

    @classmethod
    def detect_qr(self, image):
        # create a reader
        scanner = zbar.ImageScanner()

        # configure the reader
        scanner.parse_config('enable')

        # obtain image data
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY,dstCn=0)
        pil = Image.fromarray(gray)
        width, height = pil.size
        raw = pil.tostring()


        # wrap image data
        image = zbar.Image(width, height, 'Y800', raw)

        # scan the image for barcodes
        scanner.scan(image)

        # extract results
        for symbol in image:
            # do something useful with results
            if symbol.data == "None":
                return "Drone bevindt zich buiten het raster"
            else:
                return symbol.data

可以使用OpenCV,Python来完成吗? zbar是否有我可以使用的东西?

2 个答案:

答案 0 :(得分:2)

我建议最好的办法是首先检测QR码周围的所有边界(最大的方块),这样就可以得到图像中QR码的列表。

然后,您可以在此列表中对最接近无人机位置的列表进行排序。然后在特定的二维码上运行你的二维码阅读器。

Here是关于如何使用opencv检测方块的教程。

&安培; here是一个堆栈溢出问题,显示如何检测其中包含其他方块的边界框。

答案 1 :(得分:1)

如果之后

for symbol in image:

你把print symbol.location放在一个像

这样的格式的坐标上 <(>(334,407),(497,424),(512,272),(340,251))

这给出了相机图像中QR码的四个角

然后,您可以使用类似

的内容获取中心坐标
loc = symbol.location
x = (loc[0][0]+loc[2][0])/2
y = (loc[0][1]+loc[2][1])/2

如果中心用

表示500,500,则距相机图像中心的距离
distance_from_centre_squared = (x-500)**2 + (y-500)**2

然后使用距中心最短距离的QR