6

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.

vin
  • 960
  • 2
  • 14
  • 28
  • Maybe this [https://stackoverflow.com/a/47955834/228965](https://stackoverflow.com/a/47955834/228965) is the reason. – zardosht Feb 10 '19 at 16:07

3 Answers3

3

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

SSteve
  • 10,550
  • 5
  • 46
  • 72
  • Yes, even I was able to detect codes with 4x4_50.. any ideas why doesn't work even in C++ too ? Also just another query: is the MIP_36h12 dict not available in the opencv-contrib as yet ? – vin Oct 16 '18 at 06:21
  • Like I said, I don’t know why it doesn’t work. I’d suggest printing and photographing some 6X6 markers yourself to test. I don’t know anything about the MIP_36h12 dictionary. – SSteve Oct 16 '18 at 11:45
1

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

Benyi
  • 13
  • 4
  • Going forward, when writing one-liners, please consider adding a comment instead of an answer. – nurchi Nov 19 '21 at 15:48
  • I had a very similar problem and was tearing my hair out. This one-liner solved. My eternal thanks – David Jul 08 '22 at 19:18
0

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

ganesh raj
  • 21
  • 1
  • that is not what the question is about. The markers were not getting detected correctly for the particular dictionary – vin Jun 15 '21 at 11:00
  • 1
    Also note: this was being observed a few years back.. The aruco dependency might have been rebuilt since, and this might not be observable. – vin Jun 15 '21 at 11:07