### Stag Example Executable Definition Source: https://github.com/manfredstoiber/stag/blob/master/CMakeLists.txt Creates an executable target named 'stag_example' using 'main.cpp' as its source file. This executable is then linked against the previously defined 'staglib' library, making the Stag functionalities available to the example application. ```cmake add_executable( stag_example main.cpp ) target_link_libraries( stag_example staglib ) ``` -------------------------------- ### MSVC Runtime Output Directory Configuration Source: https://github.com/manfredstoiber/stag/blob/master/CMakeLists.txt Configures the runtime output directory for the 'stag_example' executable specifically for the Microsoft Visual C++ compiler (MSVC). It ensures that the executable is placed in the binary directory for debug and release builds, simplifying testing and deployment on Windows. ```cmake IF (MSVC) set_target_properties(stag_example PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}) set_target_properties(stag_example PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_BINARY_DIR}) set_target_properties(stag_example PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_BINARY_DIR}) ENDIF (MSVC) ``` -------------------------------- ### CMake Project Configuration and Build Type Source: https://github.com/manfredstoiber/stag/blob/master/CMakeLists.txt Initializes the CMake project and sets the default build type to Release if not already specified. It also prints the active build type to the console and configures platform-specific settings for Windows, such as adding a 'd' suffix to debug libraries and enabling symbol exports. ```cmake cmake_minimum_required(VERSION 3.16) project(Stag) IF(NOT CMAKE_BUILD_TYPE) SET(CMAKE_BUILD_TYPE Release) ENDIF() MESSAGE("Build type: " ${CMAKE_BUILD_TYPE}) IF (WIN32) set(CMAKE_DEBUG_POSTFIX "d") set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) ENDIF (WIN32) ``` -------------------------------- ### OpenCV Dependency and Include Directories Source: https://github.com/manfredstoiber/stag/blob/master/CMakeLists.txt Finds the required OpenCV version 4 library. It then defines the include directories for the project, including those from OpenCV and custom 'src' and 'src/ED' directories. This step is crucial for making OpenCV headers and custom headers available during compilation. ```cmake find_package(OpenCV 4 REQUIRED) file(GLOB SRC_FILE1 "src/*.c*") file(GLOB SRC_FILE2 "src/ED/*.c*") include_directories( ${OpenCV_INCLUDE_DIRS} src/ src/ED/ ) ``` -------------------------------- ### Library HD Configuration Options in C++ Source: https://context7.com/manfredstoiber/stag/llms.txt Demonstrates the impact of different library HD (High Density) configurations on marker detection. This code tests various HD settings, showing the trade-off between the number of unique markers supported and the error correction capability. It outputs the number of detected markers for each configuration. ```cpp #include "src/Stag.h" #include #include #include #include int main() { cv::Mat image = cv::imread("markers.jpg"); // Library HD options with their characteristics struct LibraryConfig { int hd; int librarySize; int maxErrorCorrection; }; std::vector libraries = { {11, 22309, 5}, // Many markers, low error correction {13, 2884, 6}, {15, 766, 7}, {17, 157, 8}, {19, 38, 9}, {21, 12, 10}, {23, 6, 11} // Few markers, high error correction }; std::cout << "Testing different library configurations:" << std::endl; for (const auto& config : libraries) { std::vector> corners; std::vector ids; // Detect with each library stag::detectMarkers(image, config.hd, corners, ids); std::cout << "HD " << config.hd << " (Library size: " << config.librarySize << ", Max EC: " << config.maxErrorCorrection << "): Found " << ids.size() << " markers" << std::endl; } return 0; } ``` -------------------------------- ### Compiler Flags and C++ Standard Configuration Source: https://github.com/manfredstoiber/stag/blob/master/CMakeLists.txt Sets compiler flags for C and C++ to enable all warnings ('-Wall') and suppress them ('-w'). It checks for C++11 or C++0x standard support and enforces C++11 standard for C++ compilation. Includes the 'CheckCXXCompilerFlag' module for this purpose. ```cmake set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -w ") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -w ") # Check C++11 or C++0x support include(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") ``` -------------------------------- ### Stag Library Definition Source: https://github.com/manfredstoiber/stag/blob/master/CMakeLists.txt Defines a static library named 'staglib'. It collects source files from the 'src/' and 'src/ED/' directories using globbing, along with specific files like 'Stag.cpp' and 'Stag.h'. The library is then linked against the found OpenCV libraries. ```cmake add_library( staglib ${SRC_FILE1} ${SRC_FILE2} src/Stag.cpp src/Stag.h ) target_link_libraries( staglib ${OpenCV_LIBS} ) ``` -------------------------------- ### Real-Time Marker Detection in C++ using STag and OpenCV Source: https://context7.com/manfredstoiber/stag/llms.txt Processes video frames from a camera in real-time to detect fiducial markers using the STag library. It includes performance monitoring for marker detection and draws the detected markers and information onto the video feed. Dependencies include OpenCV for video capture and image manipulation, and the STag library for marker detection. ```cpp #include "src/Stag.h" #include #include #include int main() { cv::VideoCapture cap(0); // Open default camera if (!cap.isOpened()) { std::cerr << "Error: Cannot open camera" << std::endl; return -1; } int libraryHD = 21; cv::Mat frame; std::cout << "Press 'q' to quit" << std::endl; while (true) { cap >> frame; if (frame.empty()) break; auto start = std::chrono::high_resolution_clock::now(); std::vector> corners; std::vector ids; // Detect markers in current frame stag::detectMarkers(frame, libraryHD, corners, ids); auto end = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast(end - start); // Draw results stag::drawDetectedMarkers(frame, corners, ids); // Overlay performance info std::string info = "Markers: " + std::to_string(ids.size()) + " | Time: " + std::to_string(duration.count()) + "ms"; cv::putText(frame, info, cv::Point(10, 30), cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 255, 0), 2); cv::imshow("STag Detection", frame); if (cv::waitKey(1) == 'q') break; } return 0; } ``` -------------------------------- ### Detect and Draw Fiducial Markers using C++ and OpenCV Source: https://github.com/manfredstoiber/stag/blob/master/README.md This C++ code snippet demonstrates how to load an image, specify the marker library (libraryHD), detect fiducial markers using the stag::detectMarkers function, and then draw the detected markers onto the image using stag::drawDetectedMarkers. Finally, it saves the resulting image. Dependencies include OpenCV and the STag library. ```c++ // load image cv::Mat image = cv::imread("example.jpg"); // set HD library int libraryHD = 21; auto corners = std::vector>(); auto ids = std::vector(); auto rejectedImgPoints = std::vector>(); // optional, helpful for debugging // detect markers stag::detectMarkers(image, libraryHD, corners, ids, rejectedImgPoints); // draw and save results stag::drawDetectedMarkers(image, corners, ids); cv::imwrite("example_result.jpg", image); ``` -------------------------------- ### Marker Detection with Debugging Output Source: https://context7.com/manfredstoiber/stag/llms.txt Detects markers and outputs rejected quadrilateral candidates, which is useful for debugging detection failures and understanding why certain markers might not be recognized. ```APIDOC ## POST /stag/detectMarkers/debug ### Description Detects markers and outputs rejected quadrilateral candidates, which is useful for debugging detection failures and understanding why certain markers might not be recognized. ### Method POST ### Endpoint `/stag/detectMarkers/debug` ### Parameters #### Request Body - **image** (cv::Mat) - Required - The input image to detect markers from. - **libraryHD** (int) - Required - Configures the Hamming Distance library. For `libraryHD = 17`, there are 157 markers. ### Request Example ```cpp #include "src/Stag.h" #include int main() { cv::Mat image = cv::imread("test_image.jpg"); int libraryHD = 17; // 157 markers std::vector> corners; std::vector ids; std::vector> rejectedImgPoints; // Detect with rejected candidates output stag::detectMarkers(image, libraryHD, corners, ids, rejectedImgPoints); std::cout << "Detected markers: " << ids.size() << std::endl; std::cout << "Rejected candidates: " << rejectedImgPoints.size() << std::endl; // Analyze rejected candidates for (size_t i = 0; i < rejectedImgPoints.size(); i++) { std::cout << " Rejected quad " << i << " at: "; for (const auto& pt : rejectedImgPoints[i]) { std::cout << "(" << pt.x << "," << pt.y << ") "; } std::cout << std::endl; } return 0; } ``` ### Response #### Success Response (200) - **ids** (std::vector) - A list of detected marker IDs. - **corners** (std::vector>) - A list of corner points for each detected marker. - **rejectedImgPoints** (std::vector>) - A list of rejected quadrilateral candidates, each represented by four corner points. #### Response Example ```json { "ids": [55], "corners": [ [{"x": 75.1, "y": 85.3}, {"x": 115.2, "y": 80.1}, {"x": 110.9, "y": 120.5}, {"x": 70.8, "y": 125.8}] ], "rejectedImgPoints": [ [{"x": 20.5, "y": 25.1}, {"x": 60.2, "y": 20.8}, {"x": 55.9, "y": 60.3}, {"x": 15.1, "y": 65.2}], [{"x": 100.1, "y": 110.5}, {"x": 140.2, "y": 105.2}, {"x": 135.9, "y": 145.8}, {"x": 95.1, "y": 150.3}] ] } ``` ``` -------------------------------- ### Multi-Image Batch Processing in C++ Source: https://context7.com/manfredstoiber/stag/llms.txt Efficiently processes a list of images for marker detection and accumulates statistics. This function iterates through a vector of image file paths, performs detection on each, and aggregates marker frequencies. It saves annotated images and reports overall marker statistics. ```cpp #include "src/Stag.h" #include #include #include #include #include int main() { std::vector imageFiles = { "frame_001.jpg", "frame_002.jpg", "frame_003.jpg" }; int libraryHD = 15; std::map markerFrequency; for (const auto& filename : imageFiles) { cv::Mat image = cv::imread(filename); if (image.empty()) continue; std::vector> corners; std::vector ids; stag::detectMarkers(image, libraryHD, corners, ids); // Accumulate marker statistics for (int id : ids) { markerFrequency[id]++; } // Save annotated result stag::drawDetectedMarkers(image, corners, ids); cv::imwrite("annotated_" + filename, image); } // Report statistics std::cout << "Marker detection statistics:" << std::endl; for (const auto& pair : markerFrequency) { std::cout << " Marker " << pair.first << ": detected in " << pair.second << " frames" << std::endl; } return 0; } ``` -------------------------------- ### Draw Detected Markers on Image in C++ Source: https://context7.com/manfredstoiber/stag/llms.txt Visualizes detected markers on an image by drawing borders, corners, and IDs. This function takes an OpenCV image matrix, detected marker corners, and their corresponding IDs. It supports custom color drawing and an option to omit drawing the marker IDs. ```cpp #include "src/Stag.h" #include #include int main() { cv::Mat image = cv::imread("example.jpg"); int libraryHD = 21; std::vector> corners; std::vector ids; stag::detectMarkers(image, libraryHD, corners, ids); // Draw with default green color (50, 255, 50) stag::drawDetectedMarkers(image, corners, ids); cv::imwrite("result_default.jpg", image); // Draw with custom color (red) cv::Mat image2 = cv::imread("example.jpg"); stag::detectMarkers(image2, libraryHD, corners, ids); cv::Scalar redColor(50, 50, 255); // BGR format stag::drawDetectedMarkers(image2, corners, ids, redColor); cv::imwrite("result_red.jpg", image2); // Draw without IDs (empty ids vector) cv::Mat image3 = cv::imread("example.jpg"); stag::detectMarkers(image3, libraryHD, corners, ids); stag::drawDetectedMarkers(image3, corners); cv::imwrite("result_no_ids.jpg", image3); return 0; } ``` -------------------------------- ### Detect STag Markers with Debugging Output (C++) Source: https://context7.com/manfredstoiber/stag/llms.txt Detects STag markers and also outputs a list of rejected quadrilateral candidates. This is useful for debugging detection failures and understanding why certain regions might have been missed. It requires OpenCV and the STag library, and returns detected marker information along with rejected candidates. ```cpp #include "src/Stag.h" #include int main() { cv::Mat image = cv::imread("test_image.jpg"); int libraryHD = 17; // 157 markers std::vector> corners; std::vector ids; std::vector> rejectedImgPoints; // Detect with rejected candidates output stag::detectMarkers(image, libraryHD, corners, ids, rejectedImgPoints); std::cout << "Detected markers: " << ids.size() << std::endl; std::cout << "Rejected candidates: " << rejectedImgPoints.size() << std::endl; // Analyze rejected candidates for (size_t i = 0; i < rejectedImgPoints.size(); i++) { std::cout << " Rejected quad " << i << " at: "; for (const auto& pt : rejectedImgPoints[i]) { std::cout << "(" << pt.x << "," << pt.y << ") "; } std::cout << std::endl; } return 0; } ``` -------------------------------- ### Detect STag Markers with Default Error Correction (C++) Source: https://context7.com/manfredstoiber/stag/llms.txt Detects STag markers in a grayscale image using the default maximum error correction for the specified library Hamming Distance (HD). It requires OpenCV for image loading and manipulation, and the STag library for marker detection. Outputs detected marker IDs and their corner coordinates. ```cpp #include "src/Stag.h" #include #include #include int main() { // Load image from file cv::Mat image = cv::imread("scene.jpg"); if (image.empty()) { std::cerr << "Error: Could not load image" << std::endl; return -1; } // Configure library HD (21 = 12 markers with HD of 21) int libraryHD = 21; // Prepare output containers std::vector> corners; std::vector ids; // Detect markers with automatic max error correction stag::detectMarkers(image, libraryHD, corners, ids); // Process results std::cout << "Detected " << ids.size() << " markers:" << std::endl; for (size_t i = 0; i < ids.size(); i++) { std::cout << " Marker ID: " << ids[i] << std::endl; std::cout << " Corners: "; for (const auto& corner : corners[i]) { std::cout << "(" << corner.x << "," << corner.y << ") "; } std::cout << std::endl; } return 0; } ``` -------------------------------- ### Detect Markers with Full Control in C++ Source: https://context7.com/manfredstoiber/stag/llms.txt Detects markers in an image with fine-grained control over error correction and access to debugging information. This function requires an OpenCV image matrix, library HD setting, and outputs marker corners, IDs, and rejected points. It allows for custom error correction levels, up to a maximum determined by the library HD. ```cpp #include "src/Stag.h" #include #include int main() { cv::Mat image = cv::imread("complex_scene.jpg"); int libraryHD = 13; // 2,884 markers int errorCorrection = 4; // (13-1)/2 = 6 is max std::vector> corners; std::vector ids; std::vector> rejectedImgPoints; // Full control detection stag::detectMarkers(image, libraryHD, corners, ids, errorCorrection, rejectedImgPoints); // Calculate success rate int totalCandidates = corners.size() + rejectedImgPoints.size(); double successRate = (totalCandidates > 0) ? (100.0 * corners.size() / totalCandidates) : 0.0; std::cout << "Detection success rate: " << successRate << "%" << std::endl; std::cout << " Accepted: " << corners.size() << std::endl; std::cout << " Rejected: " << rejectedImgPoints.size() << std::endl; return 0; } ``` -------------------------------- ### Detect STag Markers with Custom Error Correction (C++) Source: https://context7.com/manfredstoiber/stag/llms.txt Detects STag markers with a user-defined error correction level, offering fine-grained control over detection robustness. This function takes the image, library HD, and an optional custom error correction value. It outputs the detected marker IDs and corner coordinates. ```cpp #include "src/Stag.h" #include int main() { cv::Mat image = cv::imread("markers.jpg"); // Use HD 15 library (766 markers) int libraryHD = 15; // Set custom error correction (range: 0 to (HD-1)/2) // For HD 15: max is (15-1)/2 = 7 int errorCorrection = 5; std::vector> corners; std::vector ids; // Detect with custom error correction stag::detectMarkers(image, libraryHD, corners, ids, errorCorrection); std::cout << "Detected " << ids.size() << " markers with error correction = " << errorCorrection << std::endl; return 0; } ``` -------------------------------- ### Marker Detection with Default Error Correction Source: https://context7.com/manfredstoiber/stag/llms.txt Detects STag markers in an image using the maximum possible error correction for the specified library HD. This function automatically handles error correction based on the provided `libraryHD`. ```APIDOC ## POST /stag/detectMarkers ### Description Detects STag markers in an image using the maximum possible error correction for the specified library HD. This function automatically handles error correction based on the provided `libraryHD`. ### Method POST ### Endpoint `/stag/detectMarkers` ### Parameters #### Request Body - **image** (cv::Mat) - Required - The input image to detect markers from. Can be in various color spaces (grayscale, BGR, BGRA). - **libraryHD** (int) - Required - Configures the Hamming Distance library. Higher values allow for more markers but reduce error correction capability. For example, `libraryHD = 21` supports 12 markers with HD of 21. ### Request Example ```cpp #include "src/Stag.h" #include #include #include int main() { // Load image from file cv::Mat image = cv::imread("scene.jpg"); if (image.empty()) { std::cerr << "Error: Could not load image" << std::endl; return -1; } // Configure library HD (21 = 12 markers with HD of 21) int libraryHD = 21; // Prepare output containers std::vector> corners; std::vector ids; // Detect markers with automatic max error correction stag::detectMarkers(image, libraryHD, corners, ids); // Process results std::cout << "Detected " << ids.size() << " markers:" << std::endl; for (size_t i = 0; i < ids.size(); i++) { std::cout << " Marker ID: " << ids[i] << std::endl; std::cout << " Corners: "; for (const auto& corner : corners[i]) { std::cout << "(" << corner.x << "," << corner.y << ") "; } std::cout << std::endl; } return 0; } ``` ### Response #### Success Response (200) - **ids** (std::vector) - A list of detected marker IDs. - **corners** (std::vector>) - A list of corner points for each detected marker. Each inner vector contains four `cv::Point2f` objects. #### Response Example ```json { "ids": [101, 205, 310], "corners": [ [{"x": 10.5, "y": 20.2}, {"x": 50.1, "y": 15.8}, {"x": 45.9, "y": 55.3}, {"x": 5.2, "y": 60.0}], [{"x": 100.0, "y": 110.5}, {"x": 150.7, "y": 105.2}, {"x": 145.1, "y": 155.8}, {"x": 95.3, "y": 160.0}], [{"x": 200.3, "y": 210.1}, {"x": 250.9, "y": 205.5}, {"x": 245.2, "y": 255.9}, {"x": 195.8, "y": 260.4}] ] } ``` ``` -------------------------------- ### Marker Detection with Custom Error Correction Source: https://context7.com/manfredstoiber/stag/llms.txt Detects markers with a specified error correction level, allowing fine-tuned control over detection robustness versus false positive rate. This is useful when specific error tolerance is required. ```APIDOC ## POST /stag/detectMarkers/custom ### Description Detects markers with a specified error correction level, allowing fine-tuned control over detection robustness versus false positive rate. This is useful when specific error tolerance is required. ### Method POST ### Endpoint `/stag/detectMarkers/custom` ### Parameters #### Request Body - **image** (cv::Mat) - Required - The input image to detect markers from. - **libraryHD** (int) - Required - Configures the Hamming Distance library. For HD 15, there are 766 markers. - **errorCorrection** (int) - Required - The custom error correction level. This should be in the range of 0 to `(libraryHD - 1) / 2`. For `libraryHD = 15`, the maximum `errorCorrection` is 7. ### Request Example ```cpp #include "src/Stag.h" #include int main() { cv::Mat image = cv::imread("markers.jpg"); // Use HD 15 library (766 markers) int libraryHD = 15; // Set custom error correction (range: 0 to (HD-1)/2) // For HD 15: max is (15-1)/2 = 7 int errorCorrection = 5; std::vector> corners; std::vector ids; // Detect with custom error correction stag::detectMarkers(image, libraryHD, corners, ids, errorCorrection); std::cout << "Detected " << ids.size() << " markers with error correction = " << errorCorrection << std::endl; return 0; } ``` ### Response #### Success Response (200) - **ids** (std::vector) - A list of detected marker IDs. - **corners** (std::vector>) - A list of corner points for each detected marker. #### Response Example ```json { "ids": [42, 117], "corners": [ [{"x": 30.1, "y": 40.5}, {"x": 70.2, "y": 35.0}, {"x": 65.8, "y": 75.2}, {"x": 25.7, "y": 80.1}], [{"x": 130.9, "y": 140.2}, {"x": 180.1, "y": 135.5}, {"x": 175.5, "y": 185.8}, {"x": 125.2, "y": 190.3}] ] } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.