Skip to content

Commit

Permalink
Add script to generate aruco markers
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNoobInventor committed Apr 27, 2024
1 parent c4cfcde commit f38ebd7
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
Binary file added docs/images/generated_aruco_marker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions lidarbot_aruco/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Lidarbot Aruco package

## Generate ArUco marker

The `opencv-contrib-python` module needs to be installed and not `opencv-python`:

```
pip uninstall opencv-python
pip install opencv-contrib-python
```
The computer might need to be restarted for the install to be effected.

Next navigate to the path in the `lidarbot_aruco` directory:

```
cd ~/dev_ws/lidarbot_aruco/lidarbot_aruco
```

Then run the following script:

```python
python generate_aruco_marker.py --id 24 --type DICT_4X4_50 \
--output ../tags/DICT_4X4_50_id24.png
```

The script arguments:

`--id` : The unique identifier of the ArUco tag — this is a required argument and ID must be a valid ID in the ArUco dictionary used to generate the tag

`--type` : The name of the ArUco dictionary used to generate the tag; the default type is `DICT_4X4_50`

`--output` : The path to the output image where the generated ArUco tag will be saved; this is a required argument

Running the previous script opens a window with the generated ArUco tag displayed,

<p align='center'>
<img src=../docs/images/generated_aruco_marker.png width="400">
</p>

To close the window, press the **q** key on the keyboard on the opened window.

89 changes: 89 additions & 0 deletions lidarbot_aruco/lidarbot_aruco/generate_aruco_marker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env python

# This script generates ArUco markers using OpenCV and Python

# Adapted from https://automaticaddison.com/how-to-create-an-aruco-marker-using-opencv-python/
# and https://pyimagesearch.com/2020/12/14/generating-aruco-markers-with-opencv-and-python/

import cv2
import argparse
import numpy as np
import sys

# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument(
"-o", "--output", required=True, help="path to output image containing ArUCo tag"
)
ap.add_argument(
"-i",
"--id",
type=int,
required=True,
help="ID of ArUCo tag to generate",
)
ap.add_argument(
"-t",
"--type",
type=str,
default="DICT_4X4_50",
help="type of ArUCo tag to generate",
)
args = vars(ap.parse_args())

# The different ArUco dictinaries built into OpenCV
ARUCO_DICT = {
"DICT_4X4_50": cv2.aruco.DICT_4X4_50,
"DICT_4X4_100": cv2.aruco.DICT_4X4_100,
"DICT_4X4_250": cv2.aruco.DICT_4X4_250,
"DICT_4X4_1000": cv2.aruco.DICT_4X4_1000,
"DICT_5X5_50": cv2.aruco.DICT_5X5_50,
"DICT_5X5_100": cv2.aruco.DICT_5X5_100,
"DICT_5X5_250": cv2.aruco.DICT_5X5_250,
"DICT_5X5_1000": cv2.aruco.DICT_5X5_1000,
"DICT_6X6_50": cv2.aruco.DICT_6X6_50,
"DICT_6X6_100": cv2.aruco.DICT_6X6_100,
"DICT_6X6_250": cv2.aruco.DICT_6X6_250,
"DICT_6X6_1000": cv2.aruco.DICT_6X6_1000,
"DICT_7X7_50": cv2.aruco.DICT_7X7_50,
"DICT_7X7_100": cv2.aruco.DICT_7X7_100,
"DICT_7X7_250": cv2.aruco.DICT_7X7_250,
"DICT_7X7_1000": cv2.aruco.DICT_7X7_1000,
"DICT_ARUCO_ORIGINAL": cv2.aruco.DICT_ARUCO_ORIGINAL,
}


# Main method
def main():

# Check that we have a valid ArUco marker
if ARUCO_DICT.get(args["type"], None) is None:
print("[INFO] ArUco tag of '{}' is not supported".format(args["type"]))
sys.exit(0)

# Load the ArUco dictionary
arucoDict = cv2.aruco.getPredefinedDictionary(ARUCO_DICT[args["type"]])

# Allocate memory for the output ArUCo tag and then draw the ArUCo
# tag on the output image
print(
"[INFO] generating ArUCo tag type '{}' with ID '{}'".format(
args["type"], args["id"]
)
)
tag = np.zeros((300, 300, 1), dtype="uint8")
cv2.aruco.generateImageMarker(arucoDict, args["id"], 300, tag, 1)

# Write the generated ArUCo tag to disk and then display it to our
# screen
cv2.imwrite(args["output"], tag)
cv2.imshow("ArUCo Tag", tag)

# If "q" is pressed on the keyboard on the opened window with the aruco tag
# close the window
if cv2.waitKey(0) & 0xFF == ord("q"):
cv2.destroyAllWindows


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions lidarbot_aruco/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>

<depend>usb-cam</depend>

<export>
<build_type>ament_python</build_type>
</export>
Expand Down
Binary file added lidarbot_aruco/tags/DICT_4X4_50_id24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f38ebd7

Please sign in to comment.