functional node using OpenCV
This commit is contained in:
parent
e15a7a9579
commit
18cc0f789c
|
|
@ -4,30 +4,18 @@
|
|||
<version>0.0.0</version>
|
||||
<description>The test package</description>
|
||||
|
||||
<!-- One maintainer tag required, multiple allowed, one person per tag -->
|
||||
<!-- Example: -->
|
||||
<!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
|
||||
<maintainer email="thomas@todo.todo">thomas</maintainer>
|
||||
<maintainer email="user@example.com">Your Name</maintainer>
|
||||
<license>BSD</license>
|
||||
|
||||
|
||||
<!-- 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 -->
|
||||
<!-- Example: -->
|
||||
<!-- <url type="website">http://wiki.ros.org/test</url> -->
|
||||
|
||||
|
||||
<!-- Author tags are optional, multiple are allowed, one per tag -->
|
||||
<!-- Authors do not have to be maintainers, but could be -->
|
||||
<!-- Example: -->
|
||||
<!-- <author email="jane.doe@example.com">Jane Doe</author> -->
|
||||
|
||||
|
||||
<!-- The *depend tags are used to specify dependencies -->
|
||||
<!-- Dependencies can be catkin packages or system dependencies -->
|
||||
<!-- Examples: -->
|
||||
|
|
@ -48,12 +36,25 @@
|
|||
<!-- <test_depend>gtest</test_depend> -->
|
||||
<!-- Use doc_depend for packages you need only for building documentation: -->
|
||||
<!-- <doc_depend>doxygen</doc_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>
|
||||
<!-- Other tools can request additional information be placed here -->
|
||||
|
||||
</export>
|
||||
</package>
|
||||
|
|
|
|||
|
|
@ -1,56 +1,69 @@
|
|||
#include <ros/ros.h>
|
||||
#include <image_transport/image_transport.h>
|
||||
#include <cv_bridge/cv_bridge.h>
|
||||
#include <opencv2/aruco.hpp>
|
||||
#include <opencv2/highgui/highgui.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::Mat frame = cv_bridge::toCvShare(msg, "bgr8")->image;
|
||||
|
||||
// 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("Detected %zu ArUco markers", markerIds.size());
|
||||
for (size_t i = 0; i < markerIds.size(); ++i)
|
||||
{
|
||||
cv::aruco::drawDetectedMarkers(frame, markerCorners, markerIds);
|
||||
ROS_INFO("Detected %zu ArUco markers", markerIds.size());
|
||||
ROS_INFO("Marker ID: %d", markerIds[i]);
|
||||
}
|
||||
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)
|
||||
{
|
||||
ros::init(argc, argv, "qr_code_detector");
|
||||
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");
|
||||
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");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue