diff --git a/CMakeLists.txt b/CMakeLists.txt index e247d8d..44349bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ add_library(${PROJECT_NAME} SHARED "src/${PROJECT_NAME}/node/ninshiki_cpp_node.cpp" "src/${PROJECT_NAME}/utils/color.cpp" "src/${PROJECT_NAME}/utils/contours.cpp" + "src/${PROJECT_NAME}/utils/circles.cpp" "src/${PROJECT_NAME}/utils/utils.cpp" ) @@ -52,6 +53,7 @@ add_library(${PROJECT_NAME}_exported SHARED "src/${PROJECT_NAME}/detector/lbp_detector.cpp" "src/${PROJECT_NAME}/utils/color.cpp" "src/${PROJECT_NAME}/utils/contours.cpp" + "src/${PROJECT_NAME}/utils/circles.cpp" "src/${PROJECT_NAME}/utils/utils.cpp" ) diff --git a/include/ninshiki_cpp/node/ninshiki_cpp_node.hpp b/include/ninshiki_cpp/node/ninshiki_cpp_node.hpp index d4470f7..d944a9e 100644 --- a/include/ninshiki_cpp/node/ninshiki_cpp_node.hpp +++ b/include/ninshiki_cpp/node/ninshiki_cpp_node.hpp @@ -63,7 +63,7 @@ class NinshikiCppNode rclcpp::TimerBase::SharedPtr node_timer; rclcpp::Publisher::SharedPtr detected_object_publisher; - rclcpp::Publisher::SharedPtr field_segmentation_publisher; + rclcpp::Publisher::SharedPtr color_segmentation_publisher; rclcpp::Subscription::SharedPtr image_subscriber; ConfigGrpc config_grpc; diff --git a/include/ninshiki_cpp/utils/circles.hpp b/include/ninshiki_cpp/utils/circles.hpp new file mode 100644 index 0000000..8b40205 --- /dev/null +++ b/include/ninshiki_cpp/utils/circles.hpp @@ -0,0 +1,56 @@ +// Copyright (c) 2021 ICHIRO ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef NINSHIKI_CPP__UTILS__CIRCLES_HPP_ +#define NINSHIKI_CPP__UTILS__CIRCLES_HPP_ + +#include +#include + +namespace ninshiki_cpp::utils +{ + +class Circles +{ +private: + + std::vector centers; + std::vector radiuses; + +public: + + Circles(); + Circles(std::vector> contours) { find(contours); } + + cv::Mat get_binary_mat(cv::Size mat_size) { return get_binary_mat_line(mat_size, cv::FILLED); } + cv::Mat get_binary_mat_line(cv::Size mat_size, int line_size); + + std::vector get_centers() { return centers; } + std::vector get_radiuses() { return radiuses; } + + cv::Point get_first_center(); + float get_first_radiuses(); + + void find(std::vector> contours); +}; + +} // namespace ninshiki_cpp::utils + +#endif // NINSHIKI_CPP__UTILS__CIRCLES_HPP_ \ No newline at end of file diff --git a/src/ninshiki_cpp/node/ninshiki_cpp_node.cpp b/src/ninshiki_cpp/node/ninshiki_cpp_node.cpp index d3bf4e6..849646f 100644 --- a/src/ninshiki_cpp/node/ninshiki_cpp_node.cpp +++ b/src/ninshiki_cpp/node/ninshiki_cpp_node.cpp @@ -37,7 +37,7 @@ NinshikiCppNode::NinshikiCppNode( { detected_object_publisher = node->create_publisher( get_node_prefix() + "/dnn_detection", 10); - field_segmentation_publisher = node->create_publisher( + color_segmentation_publisher = node->create_publisher( get_node_prefix() + "/color_detection", 10); image_subscriber = @@ -67,7 +67,7 @@ void NinshikiCppNode::publish() detected_object_publisher->publish(dnn_detection->detection_result); color_detection->detection(hsv_frame); - field_segmentation_publisher->publish(color_detection->detection_result); + color_segmentation_publisher->publish(color_detection->detection_result); lbp_detection->detection(received_frame); detected_object_publisher->publish(lbp_detection->detection_result); diff --git a/src/ninshiki_cpp/utils/circles.cpp b/src/ninshiki_cpp/utils/circles.cpp new file mode 100644 index 0000000..7b4bb6a --- /dev/null +++ b/src/ninshiki_cpp/utils/circles.cpp @@ -0,0 +1,79 @@ +// Copyright (c) 2021 ICHIRO ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "ninshiki_cpp/utils/circles.hpp" + +namespace ninshiki_cpp::utils +{ + +Circles::Circles() +{ + centers.clear(); + radiuses.clear(); +} + +cv::Mat Circles::get_binary_mat_line(cv::Size mat_size, int line_size) +{ + cv::Mat binary_mat(mat_size, CV_8UC1); + binary_mat = cv::Scalar(0); + + if (centers.size() > 0 && radiuses.size() > 0) + { + for (unsigned int i = 0; i < centers.size() && i < radiuses.size(); i++) + { + cv::circle(binary_mat, centers[i], radiuses[i], 255, line_size); + } + } + + return binary_mat; +} + +cv::Point Circles::get_first_center() +{ + if (centers.size() <= 0) + return cv::Point(-1, -1); + + return centers[0]; +} + +float Circles::get_first_radiuses() +{ + if (radiuses.size() <= 0) + return 0; + + return radiuses[0]; +} + +void Circles::find(std::vector> contours) +{ + centers.clear(); + radiuses.clear(); + + for (std::vector &contour : contours) + { + cv::Point2f center; + float radius; + cv::minEnclosingCircle(cv::Mat(contour), center, radius); + + centers.push_back(center); + radiuses.push_back(radius); + } +} +} // namespace ninshiki_cpp::utils \ No newline at end of file