稳定可靠的视觉基准系统
安装
git clone https://github.com/AprilRobotics/apriltag.git
cd apriltag
cmake -B build -DCMAKE_BUILD_TYPE=Release
sudo cmake --build build --target install
make -j
sudo make install
修改(重要)
sudo nano /usr/local/share/apriltag/cmake/apriltagTargets-release.cmake
- 在原文件的 set(CMAKE_IMPORT_FILE_VERSION 1) 后面添加
set(apriltag_INCLUDE_DIR "/usr/local/include/apriltag") find_library(apriltag_LIBRARY1 NAMES libapriltag.so PATHS "/usr/local/lib" ) find_library(apriltag_LIBRARY2 NAMES libapriltag.so.3 PATHS "/usr/local/lib" ) find_library(apriltag_LIBRARY3 NAMES libapriltag.so.3.1.0 PATHS "/usr/local/lib" ) set(apriltag_LIBS ${apriltag_LIBRARY1} ${apriltag_LIBRARY2} ${apriltag_LIBRARY3})
测试
- 新建一个目录,其结构如下:
. ├── CMakeLists.txt └── main.cpp
- CMakeLists.txt
cmake_minimum_required(VERSION 3.10) project(test_code CXX) add_executable(${PROJECT_NAME} "main.cpp" ) find_package(OpenCV 4 REQUIRED) find_package(apriltag REQUIRED) target_include_directories(${PROJECT_NAME} PRIVATE ${OpenCV_INCLUDE_DIRS} ${apriltag_INCLUDE_DIRS} ) target_link_libraries(${PROJECT_NAME} PRIVATE ${OpenCV_LIBS} apriltag )
- main.cpp
#include <iostream> #include <opencv2/opencv.hpp> #include <apriltag.h> #include <tag36h11.h> using namespace std; using namespace cv; int main() { // 初始化 AprilTag 检测器 apriltag_family_t *tf = tag36h11_create(); apriltag_detector_t *td = apriltag_detector_create(); apriltag_detector_add_family(td, tf); VideoCapture cap(0); cap.set(cv::CAP_PROP_FRAME_WIDTH, 640); cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480); cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G')); while (1) { Mat frame, frame_gray; bool isRead = cap.read(frame); if (!isRead) { break; } cvtColor(frame, frame_gray, COLOR_BGR2GRAY); // 检测标签 image_u8_t im = {.width = frame_gray.cols, .height = frame_gray.rows, .stride = frame_gray.cols, .buf = frame_gray.data}; zarray_t *detections = apriltag_detector_detect(td, &im); // 显示检测结果 for (int i = 0; i < zarray_size(detections); i++) { apriltag_detection_t *det; zarray_get(detections, i, &det); putText(frame, to_string(det->id), Point(det->c[0],det->c[1]), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 0), 1); for (int i = 0; i < 4; ++i) { putText(frame, to_string(i), Point(det->p[i][0],det->p[i][1]), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2); int next = (i + 1) % 4; cv::line(frame, Point(det->p[i][0],det->p[i][1]), Point(det->p[next][0],det->p[next][1]), cv::Scalar(0, 255, 0), 2); } apriltag_detection_destroy(det); } imshow("frame", frame); waitKey(30); zarray_destroy(detections); } // 循环结束后销毁检测器和家族 apriltag_detector_destroy(td); tag36h11_destroy(tf); return 0; }
- 在根目录下
mkdir build && cd build && cmake .. && make
,最后./test_code
即可