22#include <image_tools/cv_mat_sensor_msgs_image_type_adapter.hpp>
23#include <rclcpp/rclcpp.hpp>
25#include <sensor_msgs/msg/image.hpp>
26#include <std_msgs/msg/int32.hpp>
29#include <opencv2/opencv.hpp>
30#include <opencv2/features2d.hpp>
32#include "image_tools/cv_mat_sensor_msgs_image_type_adapter.hpp"
36 sensor_msgs::msg::Image);
39rclcpp::Publisher<image_tools::ROSCvMatContainer>::SharedPtr
debugImagePub;
40rclcpp::Subscription<image_tools::ROSCvMatContainer>::SharedPtr
imageSub;
42void segmentColor(
const cv::Mat& inputRGB,
int hueMean,
int hueWindow, cv::Mat& out)
45 cv::cvtColor(inputRGB, hsvInput, cv::COLOR_BGR2HSV);
46 cv::inRange(hsvInput, cv::Scalar(hueMean - hueWindow, 20, 20), cv::Scalar(hueMean + hueWindow, 255, 255), out);
47 cv::threshold(out, out, 100, 255, cv::THRESH_BINARY);
50int testImage(cv::Mat& input, cv::Mat& debugImage, std::string colorName,
int hueMean,
int hueWindow)
56 cv::SimpleBlobDetector::Params params;
59 params.filterByArea =
true;
61 params.maxArea = 100000;
62 params.filterByColor =
true;
63 params.blobColor = 255;
65 auto blobDetector = cv::SimpleBlobDetector::create(params);
66 std::vector<cv::KeyPoint> blobs;
67 blobDetector->detect(segmented, blobs);
69 std::cout <<
"-------" << std::endl;
72 std::cout <<
"blob " << colorName <<
" detected: " << b.size << std::endl;
80 float diameter = b.size;
81 auto radius = diameter*0.5;
82 r.x = b.pt.x - radius;
83 r.y = b.pt.y - radius;
86 cv::rectangle(debugImage,r, cv::Scalar(255,0,0),1 );
96int testRed(cv::Mat& input, cv::Mat& debugImage)
98 return testImage(input, debugImage,
"red", 130, 20);
103 return testImage(input, debugImage,
"blue", 10, 10);
108 return testImage(input, debugImage,
"green", 50, 10);
111int testRed(std::string path, cv::Mat& debugImage)
113 cv::Mat input = cv::imread(path);
114 return testImage(input, debugImage,
"red", 130, 20);
119 cv::Mat input = cv::imread(path);
120 return testImage(input, debugImage,
"blue", 10, 10);
125 cv::Mat input = cv::imread(path);
126 return testImage(input, debugImage,
"green", 50, 10);
135 cv::Mat image = img.
cv_mat();
138 auto outmat = outimg.
cv_mat();
140 int detectedColor = 0;
141 if (
testRed(image, outmat) > 0)
156 std_msgs::msg::Int32 detectedColorMsg;
157 detectedColorMsg.data = detectedColor;
167 rclcpp::init(argc, argv);
168 rclcpp::Node::SharedPtr nh;
170 detectionPub = nh->create_publisher<std_msgs::msg::Int32>(
"detected_color", 1);
176 while (!rclcpp::shutdown())
179 rclcpp::spin_some(nh);
void callback(const image_tools::ROSCvMatContainer &img)
int testBlue(cv::Mat &input, cv::Mat &debugImage)
int main(int argc, char **argv)
int testGreen(cv::Mat &input, cv::Mat &debugImage)
int testRed(cv::Mat &input, cv::Mat &debugImage)
rclcpp::Publisher< image_tools::ROSCvMatContainer >::SharedPtr debugImagePub
void segmentColor(const cv::Mat &inputRGB, int hueMean, int hueWindow, cv::Mat &out)
rclcpp::Subscription< image_tools::ROSCvMatContainer >::SharedPtr imageSub
rclcpp::Publisher< std_msgs::msg::Int32 >::SharedPtr detectionPub
int testImage(cv::Mat &input, cv::Mat &debugImage, std::string colorName, int hueMean, int hueWindow)
RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE(image_tools::ROSCvMatContainer, sensor_msgs::msg::Image)