我正在做一个项目,我必须在网络摄像头捕获的图像中检测 ArUco 标记。我一直在关注文档,直到我发现 OpenCV ArUco 模块的 Java 实现与文档中描述的不同,并偏离了寻找我自己的检测 ArUco 标记的方法。这是一个最小的、可重现的示例:
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和ArUco检测库已通过Gradle安装)
上面的代码是为了检测图像中心的标记而编写的。
感谢您提前的帮助,如果需要,我将适当提供更多信息。
您可以使用Aruco.getPredefinedDictionary(Aruco.DICT_6X6_250)。除非您有特定需求,否则这是比创建自定义词典更标准的方法。字典 DICT_6X6_250 包含多种标记。如果您需要特定标记,您可以根据需要自定义词典。您可以使用“DetectorParameters.create()”进行更多可定制的检测。以下是一些可能对您有用的 OpenCV 相关链接: