I was working on a project where I have to detect ArUco markers in the images captured by the webcam. I was following the documentation until I found out that Java implementation of OpenCV ArUco module differs from what was described in the documentation, and deviated to finding my own way of detecting ArUco markers. Here is a minimal, reproducible example:
public class Main {
public static void main(String[] args) {
OpenCV.loadLocally();
// ---------------------------------------------------------------
// Adding the marker to detect in the dictionary.
byte[][] marker =
{{0, 1, 1, 0, 0, 1}, {0, 1, 1, 0, 1, 0}, {1, 1, 0, 0, 0, 0},
{1, 0, 0, 1, 1, 0}, {0, 0, 1, 1, 0, 0}, {0, 0, 1, 1, 0, 0}};
Mat mat = new Mat(6, 6, CvType.CV_8UC1);
for (int i = 0; i < 6; i++)
for (int j = 0; j < 6; j++)
mat.put(i, j, marker[i][j]);
Mat compressed = Dictionary.getByteListFromBits(mat);
Dictionary dict = new Dictionary(compressed, 6);
ArucoDetector detector = new ArucoDetector(dict);
// ---------------------------------------------------------------
// Initializing webcam (/dev/video0) and looping
VideoCapture cap = new VideoCapture("/dev/video0");
while (cap.isOpened()) {
Mat image = new Mat();
cap.read(image);
if (image.empty())
continue;
ArrayList<Mat> corners = new ArrayList<>();
Mat ids = new Mat();
try {
detector.detectMarkers(image, corners, ids);
if (!corners.isEmpty()) // If the marker is detected,
System.out.println(corners.size()); // this should print something.
} catch (CvException e) {
System.out.println(e.getMessage());
}
}
}
}
(OpenCV and ArUco detection library have been install by Gradle)
The above code was written to detect the marker at the center of the image.
Thanks for the help in advance, and I will provide more information appropriately if needed.
Nascity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.