pupil-labs / apriltags

Python bindings for the apriltags3 library
https://pupil-apriltags.readthedocs.io/en/latest/index.html
Other
103 stars 29 forks source link

circular tags detection error #32

Closed walchko closed 3 years ago

walchko commented 3 years ago

This doesn't seem to detect circular apriltags:

import cv2
from pupil_apriltags import Detector
import numpy as np

im = cv2.imread("pics/tag21_07_00020.png", cv2.IMREAD_UNCHANGED)
d = Detector(families='tagCircle21h7',
        nthreads=1,
        quad_decimate=1.0,
        quad_sigma=0.0,
        refine_edges=1,
        decode_sharpening=0.25,
        debug=1)

tags = d.detect(im, estimate_tag_pose=False, camera_params=None, tag_size=None)
print(f"{len(tags)} tags found") 

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-9-86793bd9058c> in <module>
----> 1 tags = d.detect(im, estimate_tag_pose=False, camera_params=None, tag_size=None)
      2 print(f"{len(tags)} tags found")

~/venv/lib/python3.8/site-packages/pupil_apriltags/bindings.py in detect(self, img, estimate_tag_pose, camera_params, tag_size)
    403 image of type numpy.uint8."""
    404 
--> 405         assert len(img.shape) == 2
    406         assert img.dtype == numpy.uint8
    407 

AssertionError: 

Here is the tag:

tag21_07_00020

What am I doing wrong?

AlexVeuthey commented 3 years ago

The error you get is because you do not have 2 dimensions in your image, but likely 3 due to reading the image as RGB. Try reading/converting to grayscale 1-channel and it should work.

walchko commented 3 years ago

So that doesn't work. I pulled a circular tag from Apriltags pre-rendered tags here

tag49_12_00000

The image RGBA with the alpha layer giving the circular'ish aspect to the tag.

d = Detector(families='tagCircle49h12',
        nthreads=1,
        quad_decimate=1.0,
        quad_sigma=0.0,
        refine_edges=1,
        decode_sharpening=0.25,
        debug=1)

im = cv2.imread("pics/tag49_12_00000.png", cv2.IMREAD_UNCHANGED)
im = cv2.cvtColor(im, cv2.COLOR_BGRA2GRAY)

tags = d.detect(im, estimate_tag_pose=False, camera_params=None, tag_size=None)
print(f"{len(tags)} tags found")

Returns: 0 tags found

I also tried reading it in as grayscale with im = cv2.imread("pics/tag49_12_00000.png", 0) and that didn't help.

Am I somehow setting up the detector wrong?

AlexVeuthey commented 3 years ago

Ah yes I didn't think about it, I had the same issue not long ago. If you use the tags as input, the resolution is extremely small and nothing will be detected.

Try upscaling the tag image to 150x150 or something in this range. Beware of resampling that might make edges blurry.

walchko commented 3 years ago

Thanks! Yes, that can be a problem ... I forgot about it too and fixated on the alpha layer as the issue since other tags didn't have an alpha layer.

You can easily scale without blurriness by using cv2.INTER_AREA or cv2.INTER_NEAREST since they appear to give nice crisp edges compared to other methods I have tried.

def scale(im, s=1.0):
    (h,w) = im.shape
    return cv2.resize(im, (s*h, s*w,), interpolation=cv2.INTER_AREA)

download