- Basic Image Processing techniques using OpenCV & Python
- Generating and detecting an ArUco marker
- Finding position and orientation of an ArUco marker.
Write a Python script for detecting ArUco markers. The resulting image must have ArUco markers marked as shown in the end result. Hence the resulting image will have ArUco markers with:
- Gray dot indicating top-left
- Green dot indicating top-right
- Pink dot indicating bottom-right
- White dot indicating bottom-left
- Red dot indicating center of the ArUco
- Blue line joining center of ArUco marker and the mid-point between top-left and top-right
- ArUco id number in RED colour
- Orientation of the ArUco in degrees in GREEN colour
Install OpenCV library for Python:
pip3 install opencv-contrib-python
Note: If you already have opencv-python, remove it and install opencv-contrib-python as it can conflict with aruco library.
OpenCV resources are given below:
- Introduction to OpenCV
- GUI Features in OpenCV
- Core Operations - Basic and Arithmetic Operations on Images
- Image Processing in OpenCV - Changing Color spaces, Image Thresholding, Contours in OpenCV
ArUco is an easy to use Python library for detection of ArUco markers. To know about the library functions of AruCo, open the terminal and type Python . Then type the following commands and press Enter.
import cv2
help(cv2.aruco)
Then you can see all the available functions in ArUco library. These contents are self-explanatory of library functions in aruco module.
Step 1. Create a Python script and add the required libraries.
import numpy as np
import math
import cv2
import cv2.aruco
Step 2. Select a Dictionary provided by the aruco module.
aruco_dict = aruco.Dictionary_get(aruco.DICT_5x5_250)
Step 3. Generating markers of any id from the defined dictionary with a required output image size and marker size.
img = aruco.drawMarker(aruco_dist,11, 400)
Step 1. Create a Python script and add the required libraries
import numpy as np
import math
import cv2
import cv2.aruco
Step 2. Load the corresponding image matrix to a variable, for example img, using cv2 operation. Step 3. Convert the image matrix from RGB to grayscale using cv2 operation
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Step 4. Select a Dictionary provided by the aruco module.
aruco_dict = aruco.Dictionary_get(aruco.DICT_5x5_250)
Step 5. Create an object variable, for example parameters, which includes all the options that can be customized during the marker detection process.
parameters = aruco.DetectorParameters_create()
Step 6. Detect the markers of the corresponding dictionary from the grayscale image considering marker detection parameters.
corners, ids, _ = aruco.detectMarkers(gray, aruco_dict, parameters = parameters)
Run the following command
cd ~/catkin_ws/src/strawberry_stacker/task_1/scripts
python3 aruco_detection.py
task1_bonus.mp4
- Getting started with ROS
- Subscribing video frames from a ROS topic
- Publishing messages over a ROS topic
- Highly recommended to follow ROS tutorials (following till 1.1.13 will be enough for this task) before proceeding with the actual task as the learning curve might be steep in the start.
- This task will require the knowledge of rosnodes, rostopic, publisher & subscriber model
-
Create a rosnode named marker_detection in a python script, which will detect a moving ArUco marker from the video feed of camera and will publish the id, position and orientation of the marker on a rostopic /marker_info
-
You need to subscribe to a rostopic named /camera/camera/image_raw to read camera the video frames from camera
-
Apply the ArUco detection on these frames and publish the results on the rostopic /marker_info in the message type task_1/Marker
- Launch the Gazebo world by typing the following command
roslaunch task_1 task1_2.launch
Once the simulation window launches, you should see a static camera in air poiting downwards and there will be an ArUco marker moving in a pattern.
- Run your python script in a separate terminal to start detecting and publishing the ArUco details
rosrun task_1 marker_detection.py
Note: To avoid manually typing the rosrun command for every iteration, you can start the rosnode in the launch file itself, to do that add the following lines in the task_1.2.launch file in the launch folder. Make sure you add the line before the
</launch>
line.
<node name="marker_detection" type="marker_detection.py" pkg="task_1" />
To record ROS messages enable recording via rosbag utility:
roslaunch task_1 task1_2.launch record:="true" rec_name:="aruco_detection.bag"