Distance Estimation Using Opencv and ArUco Marker
by Shreyas M Iliger in Circuits > Cameras
4248 Views, 1 Favorites, 0 Comments
Distance Estimation Using Opencv and ArUco Marker
The project aims to estimate the distance of an ArUco marker using an OpenCV Python program and a camera. ArUco markers are square markers with a unique pattern that can be detected by a camera, making them useful for various applications such as augmented reality, robotics, and object tracking. The program will use the camera to capture an image of the marker and then apply image processing techniques to identify the marker's position and orientation. From this information, the distance of the marker from the camera can be estimated using known parameters such as the size of the marker and the camera's focal length. The project will leverage the OpenCV library, which provides a wide range of image processing functions and algorithms. The outcome of this project will be a useful tool for distance estimation in various applications.
Supplies
The project will use IP Webcam to capture the video of the ArUco marker and send it wirelessly to the OpenCV program running on a computer. The OpenCV program will then process the image to detect the position and orientation of the marker, which will allow for the estimation of its distance from the camera.
App link-https://play.google.com/store/apps/details?id=com.pas.webcam
Installing Required Libraries for Python Code
Here is a step-by-step guide to installing the required libraries:
- First, ensure that you have Python 3 installed on your computer. You can check this by running the command python --version in your terminal or command prompt.
- To install the requests library, open your terminal or command prompt and run the command pip install requests.
- To install OpenCV version 4.6, run the following command in your terminal or command prompt: "pip install opencv-python==4.6.0.66" (NOTE- version 4.6 only should be installed for running this code)
- Next, we need to install the aruco module from OpenCV. This can be done by running the following command in your terminal or command prompt: "pip install opencv-contrib-python==4.6.0.66"
- The numpy library can be installed by running the command "pip install numpy".
Python Code
Download the python code file from below and save it to your computer. Open Visual Studio Code on your computer. If you do not have it installed, you can download it from the official website. In VS Code, go to File > Open and select the folder where you saved the code file.
Downloads
Npz File
An NPZ file is a file format used in NumPy, a popular Python library for numerical computing. The NPZ file format is used to store one or more arrays in a compressed format. The format allows for efficient storage of large arrays and facilitates fast access to the data when it is needed.
An NPZ file is actually a ZIP archive that contains one or more NumPy arrays. Each array is stored as a separate file within the archive. When you load an NPZ file into your Python program using NumPy, all the arrays contained within the file are loaded into memory as NumPy arrays.
Download the MultiMatrix.npz file from the drive link and specify the path to the file in line 5 of the python code.
https://tinyurl.com/mpvvmvb2
Mobile Camera As WebCam
Since we don't have a camera module, we will be using an IP webcam app to stream video to our OpenCV program. To use the IP webcam app, you will need to install it on your mobile device. Once the app is installed, open it and click on the three dots in the top right corner. From the menu that appears, click on "Start server" to start the video stream.
To connect to the video stream from your OpenCV program, you will need to find the IPv4 address of your mobile device. You can usually find this by going to the settings of the IP webcam app and looking for the "IP address" or "Camera URL" field. Once you have the IPv4 address, you can enter it on line 27 of your Python program.
Important: Both your computer and mobile device must be connected to the same Wi-Fi network for this to work. If you do not have a Wi-Fi network available, you can also connect your computer to the mobile device's hotspot. Also this will not consume any mobile data.
ArUco Marker
To use the attached ArUco marker with the OpenCV program, you will need to download the image and print it.
Alternatively, ArUco markers can be generated online using a variety of free tools available on the internet. Simply search for "ArUco marker generator" and choose one that suits your needs. Be sure to generate a 4x4 marker, as this is the type of marker that the code is designed to detect.
Downloads
Run the Code
Now that you have installed the necessary libraries, configured the IP webcam app, and downloaded the ArUco marker, you are ready to run the Python program.
To do this, open the Python file in your preferred IDE (such as VS Code) and run it. The program should detect the ArUco marker in the video stream from the IP webcam app and estimate the distance between the marker and the camera.
To see the distance measurement, simply place the ArUco marker in front of the mobile camera and watch the program output in the console. Have fun experimenting with different markers and seeing how the program performs in different lighting conditions and distances.