Skip to content

Commit

Permalink
Merge branch 'enhancement/refactor-ninshiki' into feature/using-grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
mbsaloka committed May 7, 2024
2 parents 4dd50fe + 9ca9005 commit d4daaca
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion include/ninshiki_cpp/node/ninshiki_cpp_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class NinshikiCppNode
rclcpp::TimerBase::SharedPtr node_timer;

rclcpp::Publisher<DetectedObjects>::SharedPtr detected_object_publisher;
rclcpp::Publisher<Contours>::SharedPtr field_segmentation_publisher;
rclcpp::Publisher<Contours>::SharedPtr color_segmentation_publisher;
rclcpp::Subscription<Image>::SharedPtr image_subscriber;

ConfigGrpc config_grpc;
Expand Down
56 changes: 56 additions & 0 deletions include/ninshiki_cpp/utils/circles.hpp
Original file line number Diff line number Diff line change
@@ -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 <opencv2/opencv.hpp>
#include <vector>

namespace ninshiki_cpp::utils
{

class Circles
{
private:

std::vector<cv::Point> centers;
std::vector<float> radiuses;

public:

Circles();
Circles(std::vector<std::vector<cv::Point>> 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<cv::Point> get_centers() { return centers; }
std::vector<float> get_radiuses() { return radiuses; }

cv::Point get_first_center();
float get_first_radiuses();

void find(std::vector<std::vector<cv::Point>> contours);
};

} // namespace ninshiki_cpp::utils

#endif // NINSHIKI_CPP__UTILS__CIRCLES_HPP_
4 changes: 2 additions & 2 deletions src/ninshiki_cpp/node/ninshiki_cpp_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ NinshikiCppNode::NinshikiCppNode(
{
detected_object_publisher = node->create_publisher<DetectedObjects>(
get_node_prefix() + "/dnn_detection", 10);
field_segmentation_publisher = node->create_publisher<Contours>(
color_segmentation_publisher = node->create_publisher<Contours>(
get_node_prefix() + "/color_detection", 10);

image_subscriber =
Expand Down Expand Up @@ -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);
Expand Down
79 changes: 79 additions & 0 deletions src/ninshiki_cpp/utils/circles.cpp
Original file line number Diff line number Diff line change
@@ -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<std::vector<cv::Point>> contours)
{
centers.clear();
radiuses.clear();

for (std::vector<cv::Point> &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

0 comments on commit d4daaca

Please sign in to comment.