Aruco markers in OpenCV with Python3

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

References

https://mecaruco2.readthedocs.io/en/latest/notebooks_rst/Aruco/aruco_basics.html

Install Libraries

pip3 install cython
pip3 install pandas

Make the Aruco Codes

Create the following Python3 code…

import cv2, PIL
from cv2 import aruco
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd

aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)

fig = plt.figure()
nx = 4
ny = 3
for i in range(1, nx*ny+1):
    ax = fig.add_subplot(ny,nx, i)
    img = aruco.drawMarker(aruco_dict,i, 700)
    plt.imshow(img, cmap = mpl.cm.gray, interpolation = "nearest")
    ax.axis("off")

plt.savefig("markers.pdf")
plt.show()

A pdf file name ‘markers.pdf’ will appear in your home directory.

For the next exercise, there is no need to print the document, you can display it on your terminal screen and take a photo of the aruco’s on your phone. Take the photo at an angle to create some real persepctive.

The next exercise will show how are Aruco’s are read in the real world from different angles.

Transfer the Aruco photo from your phone onto your computers home directory and call it ‘aruco1.jpg’.

Copy and paste the following code into Python3 and run it from terminal.

import numpy as np
import cv2 as cv2
from cv2 import aruco
import matplotlib.pyplot as plt
import pandas as pd

'''
http://brisbaneroboticsclub.id.au/aruco-markers-in-opencv-with-python3/
'''

frame = cv2.imread("aruco1.jpg")

#percent by which the image is resized
scale_percent = 25

#calculate the 50 percent of original dimensions
width = int(frame.shape[1] * scale_percent / 100)
height = int(frame.shape[0] * scale_percent / 100)

# dsize
dsize = (width, height)

# resize image
frame = cv2.resize(frame, dsize)

# show image
cv2.imshow('frame',frame)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Identify all arucos
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)
parameters =  aruco.DetectorParameters_create()
corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)
frame_markers = aruco.drawDetectedMarkers(frame.copy(), corners, ids)

# Show Aruco data
plt.figure()
plt.imshow(frame_markers)
for i in range(len(ids)):
    c = corners[i][0]
    plt.plot([c[:, 0].mean()], [c[:, 1].mean()], "o", label = "id={0}".format(ids[i]))
plt.legend()
plt.show()

def quad_area(data):
    l = data.shape[0]//2
    corners = data[["c1", "c2", "c3", "c4"]].values.reshape(l, 2,4)
    c1 = corners[:, :, 0]
    c2 = corners[:, :, 1]
    c3 = corners[:, :, 2]
    c4 = corners[:, :, 3]
    e1 = c2-c1
    e2 = c3-c2
    e3 = c4-c3
    e4 = c1-c4
    a = -.5 * (np.cross(-e1, e2, axis = 1) + np.cross(-e3, e4, axis = 1))
    return a

corners2 = np.array([c[0] for c in corners])

data = pd.DataFrame({"x": corners2[:,:,0].flatten(), "y": corners2[:,:,1].flatten()},
                   index = pd.MultiIndex.from_product(
                           [ids.flatten(), ["c{0}".format(i )for i in np.arange(4)+1]],
                       names = ["marker", ""] ))

data = data.unstack().swaplevel(0, 1, axis = 1).stack()
data["m1"] = data[["c1", "c2"]].mean(axis = 1)
data["m2"] = data[["c2", "c3"]].mean(axis = 1)
data["m3"] = data[["c3", "c4"]].mean(axis = 1)
data["m4"] = data[["c4", "c1"]].mean(axis = 1)
data["o"] = data[["m1", "m2", "m3", "m4"]].mean(axis = 1)
data

# Show Aruco data
print(data)
‘aruco1.jpg’
Aruco’s identified.
Aruco data.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.