vin
vin

Reputation: 1019

Aruco OpenCV example, all markers rejected

I am following this example.

OpenCV Aruco example with image

And following is the code snippet I am using to detect the markers. I am unable to understand why the example is not working for me.

import numpy as np
import cv2
import cv2.aruco as aruco
import os

im_names = filter(lambda x: x.endswith('.png'),
                  [f for f in os.listdir('local_vids_ims')])

for imn in im_names:
    image = cv2.imread('local_vids_ims/' + imn)
    # image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)
    parameters = aruco.DetectorParameters_create()
    corners, ids, rejectedImgPoints = aruco.detectMarkers(
        image, aruco_dict, parameters=parameters)
    print(corners, ids, rejectedImgPoints)
    # aruco.drawDetectedMarkers(image, corners)
    aruco.drawDetectedMarkers(image, rejectedImgPoints)
    cv2.imshow('gray_im', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Local Run of above code, all markers rejected.

Upvotes: 7

Views: 8687

Answers (3)

Benyi
Benyi

Reputation: 21

I encounter the same problem. I solve it by flipping the input image mat with function cv::flip.

Upvotes: 1

ganesh raj
ganesh raj

Reputation: 41

You have only created a method to detect the aruco markers and their respective ID. If you want to detect and augment the marker ID with image ID you have to do this

    def augment_marker(bbox , ids , img , img_aug , draw_id=True):
        tl = bbox[0][0][0], bbox[0][0][1]  # top left
        tr = bbox[0][1][0], bbox[0][1][1]
        br = bbox[0][2][0], bbox[0][2][1]  # bottom left
        bl = bbox[0][3][0], bbox[0][3][1]

        h , w , c = img_aug.shape 

       pts1 = np.array([tl,tr,br,bl])
       pts2 = np.float32([[0,0],[w,0],[w,h],[0,h]])

       matrix, _ = cv2.findHomography(pts2,pts1)
       imgout = cv2.warpPerspective(img_aug , matrix , 
       (img.shape[1],img.shape[0]))
       # here the above imgout will just wrapy the marker and make the 
       background full black
       # so now just want to overlay the marker part and make the 
       environment 
       real 
       # step 1 : making the marker area alone black 
       cv2.fillConvexPoly(img , pts1.astype(int),(0,0,0))
       imgout = img + imgout

where , bbox is what you get from the aruco.detectMarkers() img is the aruco marker img_aug is the what you want to augment over the marker draw_id = just I made to draw the id over detected things

Upvotes: 0

SSteve
SSteve

Reputation: 10728

Interesting. There's nothing wrong with your program. I tried the same thing in Python and C++ and got the same result as you. So I tried with a different image and was successful.

Here's my program. It's basically the same as yours but note that I'm using a different dictionary.

import numpy as np
import cv2
import cv2.aruco as aruco

image = cv2.imread("52814747.png")
aruco_dict = aruco.Dictionary_get(aruco.DICT_4X4_50)
parameters = aruco.DetectorParameters_create()
corners, ids, rejectedImgPoints = aruco.detectMarkers(
    image, aruco_dict, parameters=parameters)
print(corners, ids, rejectedImgPoints)
aruco.drawDetectedMarkers(image, corners, ids)
aruco.drawDetectedMarkers(image, rejectedImgPoints, borderColor=(100, 0, 240))

cv2.imshow('so52814747', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

I don't know if the problem is with the 6X6 dictionary or that the source image doesn't have enough resolution to work with the 6x6 dictionary. But there's definitely something wrong with that tutorial. I've reported the issue on GitHub.

Here's the image I used.

source image

And here's the result. (Found markers have green borders. Rejected candidates have red borders.)

result image

Upvotes: 3

Related Questions