使用JavaCV生成ArUco码
添加依赖:
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.8</version>
</dependency>创建代码:
import org.bytedeco.opencv.global.opencv_aruco;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_aruco.Dictionary;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imwrite;
public class ArucoExample {
/**
* 加载一个图片做为一个 Mat 对象
* Mat 对象是矩阵表示
* @param imagePath 图片路径
* @return Mat 对象
*/
public static Mat loadImage(String imagePath) {
return imread(imagePath);
}
/**
* 将图像矩阵保存为图片
* @param imageMatrix 图像矩阵
* @param targetPath 保存路径
*/
public static void saveImage(Mat imageMatrix, String targetPath){
imwrite(targetPath, imageMatrix);
}
public static void main(String[] args) {
int markerId = 23; // The ID of the marker
int markerSize = 200; // The size of the marker in pixels
// Create a dictionary of markers
Dictionary dictionary = opencv_aruco.getPredefinedDictionary(opencv_aruco.DICT_6X6_250);
// Create a marker image
Mat markerImage = new Mat();
opencv_aruco.drawMarker(dictionary, markerId, markerSize, markerImage, 1);
// Save the marker image
saveImage(markerImage,"D:/Code/marker23.png");
}
}对于其中的Dictionary有疑问:
In the comments of the code "Create a dictionary of markers". What's mean about the "Dictionary" term?
In the context of ArUco markers, a "dictionary" refers to a specific set of markers. Each dictionary contains a certain number of markers, each with a unique ID, and each marker has a specific binary square pattern. The size of the markers (i.e., the number of squares along each edge) and the number of markers in the dictionary can vary.
The term "dictionary" is used because it's a collection of unique elements (the markers), each of which can be looked up using a unique key (the marker ID). This is similar to how a language dictionary is a collection of unique words, each of which can be looked up using its spelling.
In the code snippet I provided, opencv_aruco.DICT_6X6_250 refers to a specific dictionary that contains 250 unique 6x6 markers. The getPredefinedDictionary function is used to retrieve this dictionary.
When you're detecting markers in an image, you need to know which dictionary the markers belong to, so that you can correctly interpret the binary patterns. Similarly, when you're creating markers, you need to specify which dictionary they should belong to.
参考资料:http://bytedeco.org/javacpp-presets/opencv/apidocs/
Answer by ChatGPT-4
评论已关闭