1

I'm using the bottom camera of an AR Drone to detect a QR code in order for me to know the whereabouts of the drone on a chessboard. The drone hovers over the chessboard, where each single square is a QR code holding the position(e.g. A1, C5, E7, etc.). When I press a certain key, it scans the QR code and returns to me the position.

Right now, I would like to be able to detect a single QR code out of many. Since it is possible that the drone has multiple QR codes in sight. Because I need to know which exact square the drone is on, or at least the closest one(for example: 2/3 above A1 and 1/3 above A2 should result in A1). Here's the code I'm currently using:

#!/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

Can this be done using OpenCV, Python? Does zbar have something I could use?

Redesign1991
  • 127
  • 1
  • 2
  • 12

2 Answers2

2

I would suggest the best way to do this would be to detect all the boundaries around the QR codes (the largest squares) first, this will give you a list of QR codes in an image.

You can then sort through this list for the one that is closest the drone position & then run your QR code reader on that specific QR code.

Here is a tutorial on how to detect squares using opencv.

& here is a stack overflow question showing how to detect the bounding box that has additional squares inside of it.

Community
  • 1
  • 1
GPPK
  • 6,019
  • 4
  • 29
  • 54
1

If after

for symbol in image:

you put print symbol.location it will give the coordinates in a format like

((334, 407), (497, 424), (512, 272), (340, 251))

Which gives the four corners of the QR code in your camera image

You can then get the centre coordinate with something like

loc = symbol.location
x = (loc[0][0]+loc[2][0])/2
y = (loc[0][1]+loc[2][1])/2

and the distance from the centre of your camera image if the centre is say 500,500 with

distance_from_centre_squared = (x-500)**2 + (y-500)**2

and then use the QR with the shortest distance to the centre

Tim 333
  • 902
  • 1
  • 8
  • 9