SMACC2
Functions | Variables
opencv_perception_node.cpp File Reference
#include <image_tools/cv_mat_sensor_msgs_image_type_adapter.hpp>
#include <rclcpp/rclcpp.hpp>
#include <sensor_msgs/msg/image.hpp>
#include <std_msgs/msg/int32.hpp>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/features2d.hpp>
Include dependency graph for opencv_perception_node.cpp:

Go to the source code of this file.

Functions

 RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE (image_tools::ROSCvMatContainer, sensor_msgs::msg::Image)
 
void segmentColor (const cv::Mat &inputRGB, int hueMean, int hueWindow, cv::Mat &out)
 
int testImage (cv::Mat &input, cv::Mat &debugImage, std::string colorName, int hueMean, int hueWindow)
 
int testRed (cv::Mat &input, cv::Mat &debugImage)
 
int testBlue (cv::Mat &input, cv::Mat &debugImage)
 
int testGreen (cv::Mat &input, cv::Mat &debugImage)
 
int testRed (std::string path, cv::Mat &debugImage)
 
int testBlue (std::string path, cv::Mat &debugImage)
 
int testGreen (std::string path, cv::Mat &debugImage)
 
void update ()
 
void callback (const image_tools::ROSCvMatContainer &img)
 
int main (int argc, char **argv)
 

Variables

rclcpp::Publisher< std_msgs::msg::Int32 >::SharedPtr detectionPub
 
rclcpp::Publisher< image_tools::ROSCvMatContainer >::SharedPtr debugImagePub
 
rclcpp::Subscription< image_tools::ROSCvMatContainer >::SharedPtr imageSub
 

Function Documentation

◆ callback()

void callback ( const image_tools::ROSCvMatContainer img)

Definition at line 133 of file opencv_perception_node.cpp.

134{
135 cv::Mat image = img.cv_mat();
136
138 auto outmat = outimg.cv_mat();
139
140 int detectedColor = 0;
141 if (testRed(image, outmat) > 0)
142 {
143 detectedColor = 1;
144 }
145
146 if (testGreen(image, outmat) > 0)
147 {
148 detectedColor = 2;
149 }
150
151 if (testBlue(image, outmat) > 0)
152 {
153 detectedColor = 3;
154 }
155
156 std_msgs::msg::Int32 detectedColorMsg;
157 detectedColorMsg.data = detectedColor;
158 detectionPub->publish(detectedColorMsg);
159
160
161 outimg.header() = img.header();
162 debugImagePub->publish(outimg);
163}
A potentially owning, potentially non-owning, container of a cv::Mat and ROS header.
IMAGE_TOOLS_PUBLIC const std_msgs::msg::Header & header() const
Const access the ROS Header.
IMAGE_TOOLS_PUBLIC const cv::Mat & cv_mat() const
Const access the cv::Mat in this class.
int testBlue(cv::Mat &input, cv::Mat &debugImage)
int testGreen(cv::Mat &input, cv::Mat &debugImage)
int testRed(cv::Mat &input, cv::Mat &debugImage)
rclcpp::Publisher< image_tools::ROSCvMatContainer >::SharedPtr debugImagePub
rclcpp::Publisher< std_msgs::msg::Int32 >::SharedPtr detectionPub

References image_tools::ROSCvMatContainer::cv_mat(), debugImagePub, detectionPub, image_tools::ROSCvMatContainer::header(), testBlue(), testGreen(), and testRed().

Referenced by smacc2::utils::Bind< 1 >::bindaux(), smacc2::utils::Bind< 2 >::bindaux(), smacc2::utils::Bind< 3 >::bindaux(), smacc2::utils::Bind< 4 >::bindaux(), smacc2::introspection::EventGeneratorHandler::configureEventGenerator(), smacc2::introspection::StateReactorHandler::configureStateReactor(), smacc2::ISmaccClient::connectSignal(), smacc2::StateReactor::createEventCallback(), smacc2::ISmaccStateMachine::createSignalConnection(), main(), smacc2::client_bases::SmaccActionClientBase< ActionType >::onAborted(), smacc2::client_bases::SmaccActionClientBase< ActionType >::onCancelled(), smacc2::components::CpTopicSubscriber< MessageType >::onFirstMessageReceived(), smacc2::client_bases::SmaccSubscriberClient< MessageType >::onFirstMessageReceived(), cl_keyboard::ClKeyboard::OnKeyPress(), smacc2::components::CpTopicSubscriber< MessageType >::onMessageReceived(), smacc2::client_bases::SmaccSubscriberClient< MessageType >::onMessageReceived(), cl_multirole_sensor::ClMultiroleSensor< MessageType >::onMessageTimeout(), smacc2::client_bases::SmaccActionClientBase< ActionType >::onSucceeded(), and smacc2::state_reactors::SrConditional< TEv >::SrConditional().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 165 of file opencv_perception_node.cpp.

166{
167 rclcpp::init(argc, argv);
168 rclcpp::Node::SharedPtr nh;
169
170 detectionPub = nh->create_publisher<std_msgs::msg::Int32>("detected_color", 1);
171 imageSub = nh->create_subscription<image_tools::ROSCvMatContainer>("/image_raw", 1, callback);
172 debugImagePub = nh->create_publisher<image_tools::ROSCvMatContainer>("/opencv_debug_image", 1);
173
174 rclcpp::Rate r(10);
175
176 while (!rclcpp::shutdown())
177 {
178 update();
179 rclcpp::spin_some(nh);
180 r.sleep();
181 }
182
183 /*
184 testRed("../../red1.png");
185 testRed("../../green1.png");
186 testRed("../../blue1.png");
187 testRed("../../red2.png");
188
189 testGreen("../../red1.png");
190 testGreen("../../green1.png");
191 testGreen("../../blue1.png");
192 testGreen("../../red2.png");
193
194 testBlue("../../red1.png");
195 testBlue("../../green1.png");
196 testBlue("../../blue1.png");
197 testBlue("../../red2.png");
198 */
199}
void callback(const image_tools::ROSCvMatContainer &img)
rclcpp::Subscription< image_tools::ROSCvMatContainer >::SharedPtr imageSub
void update()

References callback(), debugImagePub, detectionPub, imageSub, and update().

Here is the call graph for this function:

◆ RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE()

RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE ( image_tools::ROSCvMatContainer  ,
sensor_msgs::msg::Image   
)

◆ segmentColor()

void segmentColor ( const cv::Mat &  inputRGB,
int  hueMean,
int  hueWindow,
cv::Mat &  out 
)

Definition at line 42 of file opencv_perception_node.cpp.

43{
44 cv::Mat hsvInput;
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);
48}

Referenced by testImage().

Here is the caller graph for this function:

◆ testBlue() [1/2]

int testBlue ( cv::Mat &  input,
cv::Mat &  debugImage 
)

Definition at line 101 of file opencv_perception_node.cpp.

102{
103 return testImage(input, debugImage, "blue", 10, 10);
104}
int testImage(cv::Mat &input, cv::Mat &debugImage, std::string colorName, int hueMean, int hueWindow)

References testImage().

Referenced by callback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testBlue() [2/2]

int testBlue ( std::string  path,
cv::Mat &  debugImage 
)

Definition at line 117 of file opencv_perception_node.cpp.

118{
119 cv::Mat input = cv::imread(path);
120 return testImage(input, debugImage, "blue", 10, 10);
121}

References testImage().

Here is the call graph for this function:

◆ testGreen() [1/2]

int testGreen ( cv::Mat &  input,
cv::Mat &  debugImage 
)

Definition at line 106 of file opencv_perception_node.cpp.

107{
108 return testImage(input, debugImage, "green", 50, 10);
109}

References testImage().

Referenced by callback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testGreen() [2/2]

int testGreen ( std::string  path,
cv::Mat &  debugImage 
)

Definition at line 123 of file opencv_perception_node.cpp.

124{
125 cv::Mat input = cv::imread(path);
126 return testImage(input, debugImage, "green", 50, 10);
127}

References testImage().

Here is the call graph for this function:

◆ testImage()

int testImage ( cv::Mat &  input,
cv::Mat &  debugImage,
std::string  colorName,
int  hueMean,
int  hueWindow 
)

Definition at line 50 of file opencv_perception_node.cpp.

51{
52 cv::Mat segmented;
53
54 segmentColor(input, hueMean, hueWindow, segmented);
55
56 cv::SimpleBlobDetector::Params params;
57 // params.filterByArea= true;
58 // params.minArea = 10;
59 params.filterByArea = true;
60 params.minArea = 100;
61 params.maxArea = 100000;
62 params.filterByColor = true;
63 params.blobColor = 255;
64
65 auto blobDetector = cv::SimpleBlobDetector::create(params);
66 std::vector<cv::KeyPoint> blobs;
67 blobDetector->detect(segmented, blobs);
68
69 std::cout << "-------" << std::endl;
70 for (auto& b : blobs)
71 {
72 std::cout << "blob " << colorName << " detected: " << b.size << std::endl;
73 }
74
75 if(!blobs.empty())
76 {
77 for(auto& b: blobs)
78 {
79 cv::Rect r;
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;
84 r.width = diameter;
85 r.height = diameter;
86 cv::rectangle(debugImage,r, cv::Scalar(255,0,0),1 );
87 }
88 }
89
90 // cv::imshow(colorName + " filter - "+ path, segmented);
91 // cv::waitKey();
92
93 return blobs.size();
94}
void segmentColor(const cv::Mat &inputRGB, int hueMean, int hueWindow, cv::Mat &out)

References segmentColor().

Referenced by testBlue(), testGreen(), and testRed().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testRed() [1/2]

int testRed ( cv::Mat &  input,
cv::Mat &  debugImage 
)

Definition at line 96 of file opencv_perception_node.cpp.

97{
98 return testImage(input, debugImage, "red", 130, 20);
99}

References testImage().

Referenced by callback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ testRed() [2/2]

int testRed ( std::string  path,
cv::Mat &  debugImage 
)

Definition at line 111 of file opencv_perception_node.cpp.

112{
113 cv::Mat input = cv::imread(path);
114 return testImage(input, debugImage, "red", 130, 20);
115}

References testImage().

Here is the call graph for this function:

◆ update()

void update ( )

Definition at line 129 of file opencv_perception_node.cpp.

130{
131}

Referenced by main().

Here is the caller graph for this function:

Variable Documentation

◆ debugImagePub

rclcpp::Publisher<image_tools::ROSCvMatContainer>::SharedPtr debugImagePub

Definition at line 39 of file opencv_perception_node.cpp.

Referenced by callback(), and main().

◆ detectionPub

rclcpp::Publisher<std_msgs::msg::Int32>::SharedPtr detectionPub

Definition at line 38 of file opencv_perception_node.cpp.

Referenced by callback(), and main().

◆ imageSub

rclcpp::Subscription<image_tools::ROSCvMatContainer>::SharedPtr imageSub

Definition at line 40 of file opencv_perception_node.cpp.

Referenced by main().