Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion obstacle_detector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ find_package(OpenCV 4 REQUIRED)
add_library(${PROJECT_NAME} SHARED
src/obstacle_detector.cpp
# BEGIN STUDENT CODE
src/student_functions.cpp
# END STUDENT CODE
)
ament_target_dependencies(${PROJECT_NAME}
Expand Down Expand Up @@ -50,4 +51,4 @@ if(BUILD_TESTING)
ament_lint_auto_find_test_dependencies()
endif()

ament_package()
ament_package()
15 changes: 13 additions & 2 deletions obstacle_detector/src/obstacle_detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <opencv2/core/eigen.hpp>

// BEGIN STUDENT CODE
#include "student_functions.hpp"
// END STUDENT CODE

namespace obstacle_detector
Expand All @@ -44,6 +45,13 @@ class ObstacleDetector : public rclcpp::Node
{
// BEGIN STUDENT CODE
// Initialize publisher and subscriber
occupancy_grid_publisher_ = create_publisher<nav_msgs::msg::OccupancyGrid>("~/occupancy_grid", rclcpp::SystemDefaultsQoS());
camera_subscriber_ = image_transport::create_camera_subscription(
this, "/camera/image_raw",
std::bind(
&ObstacleDetector::ImageCallback, this, std::placeholders::_1,
std::placeholders::_2),
"raw", rclcpp::SensorDataQoS().get_rmw_qos_profile());
// END STUDENT CODE

declare_parameters<int>(
Expand All @@ -64,6 +72,8 @@ class ObstacleDetector : public rclcpp::Node

// BEGIN STUDENT CODE
// Declare subscriber and publisher members
rclcpp::Publisher<nav_msgs::msg::OccupancyGrid>::SharedPtr occupancy_grid_publisher_ {};
image_transport::CameraSubscriber camera_subscriber_ {};
// END STUDENT CODE

void ImageCallback(
Expand All @@ -83,7 +93,7 @@ class ObstacleDetector : public rclcpp::Node

// BEGIN STUDENT CODE
// Call FindColors()
cv::Mat detected_colors;
cv::Mat detected_colors = FindColors(cv_image->image, min_color, max_color);
// END STUDENT CODE

std::string tf_error_string;
Expand Down Expand Up @@ -117,7 +127,7 @@ class ObstacleDetector : public rclcpp::Node

// BEGIN STUDENT CODE
// Call ReprojectToGroundPlane
cv::Mat projected_colors;
cv::Mat projected_colors = ReprojectToGroundPlane(detected_colors, homography, map_size);
// END STUDENT CODE

cv::rotate(projected_colors, projected_colors, cv::ROTATE_90_CLOCKWISE);
Expand All @@ -141,6 +151,7 @@ class ObstacleDetector : public rclcpp::Node

// BEGIN STUDENT CODE
// Publish occupancy_grid_msg
occupancy_grid_publisher_->publish(occupancy_grid_msg);
// END STUDENT CODE
}

Expand Down
26 changes: 26 additions & 0 deletions obstacle_detector/src/student_functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "student_functions.hpp"

cv::Mat FindColors(const cv::Mat input, const cv::Scalar range_min, const cv::Scalar range_max)
{
cv::Mat input_hsv;
cv::cvtColor(input, input_hsv, cv::COLOR_BGR2HSV);

cv::Mat output(input.size(), CV_8UC1);

cv::inRange(input_hsv, range_min, range_max, output);

return output;
}

cv::Mat ReprojectToGroundPlane(const cv::Mat input,
const cv::Mat homography,
const cv::Size map_size)
{
cv::Mat output(map_size, CV_8UC1);

cv::warpPerspective(
input, output, homography, map_size, cv::INTER_NEAREST, cv::BORDER_CONSTANT,
cv::Scalar(127));

return output;
}
14 changes: 14 additions & 0 deletions obstacle_detector/src/student_functions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef STUDENT_FUNCTIONS_HPP // Check that the unique macro has not already been defined
#define STUDENT_FUNCTIONS_HPP // Define the unique macro

#include <opencv2/opencv.hpp>

cv::Mat FindColors(const cv::Mat input,
const cv::Scalar range_min,
const cv::Scalar range_max);

cv::Mat ReprojectToGroundPlane(const cv::Mat input,
const cv::Mat homography,
const cv::Size map_size);

#endif // Close the pre-processor if block