From a3b9ae3c063c21a359cec4f1bac236edf62b7086 Mon Sep 17 00:00:00 2001 From: ackimixs Date: Wed, 24 Jan 2024 15:53:58 +0100 Subject: [PATCH] update (i'm sorry) --- CMakeLists.txt | 10 +++--- {utils => aruco}/ArucoDetector.cpp | 50 +++++++++++++++++++----------- {utils => aruco}/ArucoDetector.h | 14 +++++---- {utils => aruco}/ArucoTag.cpp | 0 {utils => aruco}/ArucoTag.h | 0 arucoDetector.cpp | 41 ++++++++++++++++-------- 6 files changed, 73 insertions(+), 42 deletions(-) rename {utils => aruco}/ArucoDetector.cpp (85%) rename {utils => aruco}/ArucoDetector.h (78%) rename {utils => aruco}/ArucoTag.cpp (100%) rename {utils => aruco}/ArucoTag.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e082a9..95fbaa2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(detection_pot VERSION 1.0.0) +project(detection_pot VERSION 1.0.1) set(CMAKE_CXX_STANDARD 17) @@ -24,10 +24,10 @@ target_link_libraries( calibration ${OpenCV_LIBS} ) set(arucoDetectionSources ${COMMON_SOURCES} arucoDetector.cpp - utils/ArucoTag.cpp - utils/ArucoTag.h - utils/ArucoDetector.cpp - utils/ArucoDetector.h + aruco/ArucoTag.cpp + aruco/ArucoTag.h + aruco/ArucoDetector.cpp + aruco/ArucoDetector.h ) add_executable(arucoDetector ${arucoDetectionSources}) diff --git a/utils/ArucoDetector.cpp b/aruco/ArucoDetector.cpp similarity index 85% rename from utils/ArucoDetector.cpp rename to aruco/ArucoDetector.cpp index 7d7f25b..f813dc8 100644 --- a/utils/ArucoDetector.cpp +++ b/aruco/ArucoDetector.cpp @@ -2,12 +2,13 @@ ArucoDetector::ArucoDetector(const Type::RobotPose& pose, const std::string& calibrationPath, const Team team, const int cameraId, const bool headless) : robotPose(pose), headless(headless), team(team) { - // this->detector = cv::aruco::ArucoDetector(getPredefinedDictionary(cv::aruco::DICT_4X4_50), cv::aruco::DetectorParameters()); - // this->dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50); - // this->parameters = cv::aruco::DetectorParameters(); + // opencv 4.8 + this->detector = cv::aruco::ArucoDetector(getPredefinedDictionary(cv::aruco::DICT_4X4_50), cv::aruco::DetectorParameters()); + this->dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50); + this->parameters = cv::aruco::DetectorParameters(); // 4.6 - this->dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50); + // this->dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50); this->transformationMatrix = (cv::Mat_(4, 4) << @@ -22,6 +23,9 @@ ArucoDetector::ArucoDetector(const Type::RobotPose& pose, const std::string& cal if (!cap.isOpened()) { std::cerr << "Error opening camera." << std::endl; + } else + { + started = true; } if (!headless) @@ -67,15 +71,26 @@ void ArucoDetector::addArucoTag(const ArucoTag& tag) this->arucoTags.push_back(tag); } -std::pair>>> ArucoDetector::detectArucoTags() +std::pair>>> ArucoDetector::detectArucoTags(std::vector tags) { + if (tags.empty()) + { + tags = this->arucoTags; + } + + if (!started) + { + std::pair>>> result; + result.first = -2; + return result; + } + cv::Mat frame; cap >> frame; // Capture frame from the camera std::pair>>> result; if (frame.empty()) { - std::cerr << "Error capturing frame." << std::endl; result.first = -2; return result; } @@ -86,14 +101,14 @@ std::pair>>> Ar std::vector markerIds; std::vector> markerCorners; - cv::aruco::detectMarkers(frame, this->dictionary, markerCorners, markerIds); + // 4.6 + // cv::aruco::detectMarkers(frame, this->dictionary, markerCorners, markerIds); // opencv 4.8 - // detector.detectMarkers(frame, markerCorners, markerIds); + detector.detectMarkers(frame, markerCorners, markerIds); if (!markerIds.empty()) { - std::cout << "Detected " << markerIds.size() << " markers." << std::endl; if (!headless) { cv::aruco::drawDetectedMarkers(frame, markerCorners, markerIds); @@ -103,12 +118,12 @@ std::pair>>> Ar { int id = markerIds[i]; - if (std::find_if(arucoTags.begin(), arucoTags.end(), [id](const ArucoTag& tag) { return tag.id == id; }) == arucoTags.end()) + if (std::find_if(tags.begin(), tags.end(), [id](const ArucoTag& tag) { return tag.id == id; }) == tags.end()) { continue; } - ArucoTag tag = *std::find_if(arucoTags.begin(), arucoTags.end(), [id](const ArucoTag& tag) { return tag.id == id; }); + ArucoTag tag = *std::find_if(tags.begin(), tags.end(), [id](const ArucoTag& tag) { return tag.id == id; }); cv::Mat rvec, tvec; @@ -150,13 +165,12 @@ std::pair>>> Ar if (!headless) { cv::imshow("ArUco Detection", frame); - } - - if (cv::waitKey(10) == 27) - { - // Press 'Esc' to exit - result.first = 1; - return result; + if (cv::waitKey(10) == 27) + { + // Press 'Esc' to exit + result.first = 1; + return result; + } } result.first = 0; diff --git a/utils/ArucoDetector.h b/aruco/ArucoDetector.h similarity index 78% rename from utils/ArucoDetector.h rename to aruco/ArucoDetector.h index 30181b2..32aebc3 100644 --- a/utils/ArucoDetector.h +++ b/aruco/ArucoDetector.h @@ -1,6 +1,6 @@ #pragma once -#include "utils.h" +#include "../utils/utils.h" #include "ArucoTag.h" class ArucoDetector { @@ -14,12 +14,12 @@ class ArucoDetector { cv::VideoCapture cap; // 4.6 - cv::Ptr dictionary; + // cv::Ptr dictionary; // 4.8 - // cv::aruco::Dictionary dictionary; - // cv::aruco::DetectorParameters parameters; - // cv::aruco::ArucoDetector detector; + cv::aruco::Dictionary dictionary; + cv::aruco::DetectorParameters parameters; + cv::aruco::ArucoDetector detector; bool headless; @@ -27,6 +27,8 @@ class ArucoDetector { Team team; + bool started = false; + public: ArucoDetector(const Type::RobotPose& pose, const std::string& calibrationPath, Team team, int cameraId = 0, bool headless = false); @@ -34,7 +36,7 @@ public: ~ArucoDetector(); - std::pair>>> detectArucoTags(); + std::pair>>> detectArucoTags(std::vector tags = {}); void readCameraParameters(const std::string& path); diff --git a/utils/ArucoTag.cpp b/aruco/ArucoTag.cpp similarity index 100% rename from utils/ArucoTag.cpp rename to aruco/ArucoTag.cpp diff --git a/utils/ArucoTag.h b/aruco/ArucoTag.h similarity index 100% rename from utils/ArucoTag.h rename to aruco/ArucoTag.h diff --git a/arucoDetector.cpp b/arucoDetector.cpp index 92c8d47..cbd0e9b 100644 --- a/arucoDetector.cpp +++ b/arucoDetector.cpp @@ -1,8 +1,9 @@ -#include "utils/ArucoDetector.h" +#include "aruco/ArucoDetector.h" #include #include #include +#include std::atomic stopRequested(false); @@ -44,35 +45,46 @@ int main(int argc, char *argv[]) const std::string calibrationPath = argv[2]; // End argument parser + std::optional userInput; - std::thread userInput(userInputThread); + if (headless) + { + userInput = std::thread(userInputThread); + } const auto robotPose = Type::RobotPose{cv::Point3f(0, 0, 0), CV_PI/2}; ArucoDetector detector(robotPose, calibrationPath, BLUE, cameraId, headless); - while (true) { - const auto res = detector.detectArucoTags(); + auto whiteFlower = ArucoTag(36, "White flower", 20, FLOWER); + whiteFlower.setFlowerObjectRepresentation(); + auto purpleFlower = ArucoTag(13, "Purple flower", 20, FLOWER); + purpleFlower.setFlowerObjectRepresentation(); - if (res.first == -2) + auto solarPanel = ArucoTag(47, "Solar panel", 50, SOLAR_PANEL); + + while (true) { + const auto [code, res] = detector.detectArucoTags({solarPanel}); + + if (code == -2) { std::cerr << "Error: Could not capture frame." << std::endl; return -2; } - if (res.first == 1) + if (code == 1) { - break; + stopRequested = true; } - for (auto p : res.second) + for (auto [tags, matrix] : res) { - if (p.first.type == FLOWER) + if (tags.type == FLOWER) { - ArucoDetector::flowerDetector(p.first, p.second.first, p.second.first); - } else if (p.first.type == SOLAR_PANEL) + ArucoDetector::flowerDetector(tags, matrix.first, matrix.first); + } else if (tags.type == SOLAR_PANEL) { - ArucoDetector::solarPanelDetector(p.first, p.second.first, p.second.first, robotPose); + ArucoDetector::solarPanelDetector(tags, matrix.first, matrix.first, robotPose); } } @@ -83,7 +95,10 @@ int main(int argc, char *argv[]) } // Wait for the user input thread to finish - userInput.join(); + if (userInput.has_value()) + { + userInput.value().join(); + } return 0; } \ No newline at end of file