functional node using OpenCV

This commit is contained in:
Thomas BONNIER 2024-11-19 11:02:02 +01:00
parent e15a7a9579
commit 18cc0f789c
2 changed files with 63 additions and 49 deletions

View File

@ -4,30 +4,18 @@
<version>0.0.0</version> <version>0.0.0</version>
<description>The test package</description> <description>The test package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag --> <maintainer email="user@example.com">Your Name</maintainer>
<!-- Example: --> <license>BSD</license>
<!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
<maintainer email="thomas@todo.todo">thomas</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license>
<!-- Url tags are optional, but multiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository --> <!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: --> <!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/test</url> --> <!-- <url type="website">http://wiki.ros.org/test</url> -->
<!-- Author tags are optional, multiple are allowed, one per tag --> <!-- Author tags are optional, multiple are allowed, one per tag -->
<!-- Authors do not have to be maintainers, but could be --> <!-- Authors do not have to be maintainers, but could be -->
<!-- Example: --> <!-- Example: -->
<!-- <author email="jane.doe@example.com">Jane Doe</author> --> <!-- <author email="jane.doe@example.com">Jane Doe</author> -->
<!-- The *depend tags are used to specify dependencies --> <!-- The *depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies --> <!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: --> <!-- Examples: -->
@ -48,12 +36,25 @@
<!-- <test_depend>gtest</test_depend> --> <!-- <test_depend>gtest</test_depend> -->
<!-- Use doc_depend for packages you need only for building documentation: --> <!-- Use doc_depend for packages you need only for building documentation: -->
<!-- <doc_depend>doxygen</doc_depend> --> <!-- <doc_depend>doxygen</doc_depend> -->
<buildtool_depend>catkin</buildtool_depend> <buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>sensor_msgs</build_depend>
<build_depend>image_transport</build_depend>
<build_depend>cv_bridge</build_depend>
<build_depend>opencv</build_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>sensor_msgs</exec_depend>
<exec_depend>image_transport</exec_depend>
<exec_depend>cv_bridge</exec_depend>
<exec_depend>opencv</exec_depend>
<!-- The export tag contains other, unspecified, tags -->
<export> <export>
<!-- Other tools can request additional information be placed here --> <!-- Other tools can request additional information be placed here -->
</export> </export>
</package> </package>

View File

@ -1,56 +1,69 @@
#include <ros/ros.h> #include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h> #include <cv_bridge/cv_bridge.h>
#include <opencv2/aruco.hpp> #include <opencv2/aruco.hpp>
#include <opencv2/highgui/highgui.hpp> #include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp> #include <opencv2/imgproc/imgproc.hpp>
void imageCallback(const sensor_msgs::ImageConstPtr& msg) void processFrame(const cv::Mat& frame)
{ {
try // Create ArUco marker detector
cv::Ptr<cv::aruco::Dictionary> dictionary =
cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50); // Change dictionary type here
std::vector<int> markerIds;
std::vector<std::vector<cv::Point2f>> markerCorners;
// Detect markers in the image
cv::aruco::detectMarkers(frame, dictionary, markerCorners, markerIds);
// Draw detected markers
if (!markerIds.empty())
{ {
// Convert ROS image message to OpenCV image cv::aruco::drawDetectedMarkers(frame, markerCorners, markerIds);
cv::Mat frame = cv_bridge::toCvShare(msg, "bgr8")->image; ROS_INFO("Detected %zu ArUco markers", markerIds.size());
for (size_t i = 0; i < markerIds.size(); ++i)
// Create ArUco marker detector
cv::Ptr<cv::aruco::Dictionary> dictionary =
cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
std::vector<int> markerIds;
std::vector<std::vector<cv::Point2f>> markerCorners;
// Detect markers in the image
cv::aruco::detectMarkers(frame, dictionary, markerCorners, markerIds);
// Draw detected markers
if (!markerIds.empty())
{ {
cv::aruco::drawDetectedMarkers(frame, markerCorners, markerIds); ROS_INFO("Marker ID: %d", markerIds[i]);
ROS_INFO("Detected %zu ArUco markers", markerIds.size());
} }
else
{
ROS_INFO("No ArUco markers detected");
}
// Display the result
cv::imshow("ArUco Detection", frame);
cv::waitKey(1);
} }
catch (cv_bridge::Exception& e) else
{ {
ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str()); ROS_INFO("No ArUco markers detected");
} }
// Display the result
cv::imshow("ArUco Detection", frame);
cv::waitKey(1);
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
ros::init(argc, argv, "qr_code_detector"); ros::init(argc, argv, "qr_code_detector");
ros::NodeHandle nh; ros::NodeHandle nh;
image_transport::ImageTransport it(nh);
image_transport::Subscriber sub = it.subscribe("camera/image", 1, imageCallback); cv::VideoCapture cap(0); // Open the default camera
if (!cap.isOpened())
{
ROS_ERROR("Could not open the camera");
return -1;
}
cv::namedWindow("ArUco Detection"); cv::namedWindow("ArUco Detection");
ros::spin();
while (ros::ok())
{
cv::Mat frame;
cap >> frame; // Capture a new frame from the camera
if (frame.empty())
{
ROS_ERROR("Captured empty frame");
continue;
}
processFrame(frame);
ros::spinOnce();
}
cv::destroyWindow("ArUco Detection"); cv::destroyWindow("ArUco Detection");
return 0; return 0;
} }