其他分享
首页 > 其他分享> > 用opencv的aruco库生成二维码marker标记

用opencv的aruco库生成二维码marker标记

作者:互联网

用opencv的aruco库生成二维码marker标记

代码来源于官方提供的完整的工作实例create_marker.cpp。在opencv源码中的位置为opencv_contrib-4.4.0/modules/aruco/samples/create_marker.cpp。

#include <opencv2/highgui.hpp>
#include <opencv2/aruco.hpp>
#include <iostream>

using namespace cv;

namespace {
const char* about = "Create an ArUco marker image";
const char* keys  =
        "{@outfile |<none> | Output image }"
        "{d        |       | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,"
        "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, "
        "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
        "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}"
        "{id       |       | Marker id in the dictionary }"
        "{ms       | 200   | Marker size in pixels }"
        "{bb       | 1     | Number of bits in marker borders }"
        "{si       | false | show generated image }";
}


int main(int argc, char *argv[]) {
    CommandLineParser parser(argc, argv, keys); // 命令行参数解析类
    parser.about(about);

    if(argc < 4) {
        parser.printMessage();
        return 0;
    }

    int dictionaryId = parser.get<int>("d"); // 预定义的字典id
    int markerId = parser.get<int>("id"); // 字典里的marker id
    int borderBits = parser.get<int>("bb"); // marker黑色边界的大小
    int markerSize = parser.get<int>("ms"); // 输出marker图像的大小
    bool showImage = parser.get<bool>("si"); // 是否展示图像 

    String out = parser.get<String>(0);

    if(!parser.check()) {
        parser.printErrors();
        return 0;
    }

    Ptr<aruco::Dictionary> dictionary =
        aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));

    Mat markerImg;
    aruco::drawMarker(dictionary, markerId, markerSize, markerImg, borderBits);

    imwrite(out, markerImg);

    if(showImage) {
        imshow("marker", markerImg);
        waitKey(0);
    }

    return 0;
}

当命令行输入的参数数目不够时,会给如下提示,帮助用户正确输入参数。

Create an ArUco marker image
Usage: aruco_create_marker [params] outfile 

        --bb (value:1)
                Number of bits in marker borders
        -d
                dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16
        --id
                Marker id in the dictionary
        --ms (value:200)
                Marker size in pixels
        --si (value:false)
                show generated image

        outfile (value:<none>)
                Output image

正确的参数输入实例如下。
生成DICT_4X4_50字典中第一个marker,并保存在outfile指定的路径下。同时,在程序运行时,显示marker图像,按任意键后图像关闭,程序结束。

./aruco_create_marker -d=0 -id=0 -si=true -@outfile=/home/liangbeibei/m.jpg

生成的marker图片如下。
在这里插入图片描述
较为简单的生成marker图像的代码如下。

#include <opencv2/opencv.hpp>
#include <opencv2/aruco.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include "opencv2/core/core.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int main()
{
    cv::Mat markerImage;
    // 指定字典
    cv::Ptr<cv::aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
    cv::aruco::drawMarker(dictionary, 7, 200, markerImage, 1);
    imwrite("/home/liangbeibei/aruco_tag.jpg",markerImage);
    imshow("test", markerImage);
    waitKey();
    return 0;
}

标签:parser,50,opencv,DICT,marker,aruco,include
来源: https://blog.csdn.net/weixin_43996161/article/details/115024427