Skip to content

Commit c79603c

Browse files
authored
Merge pull request #5 from vortexntnu/refactor-and-otsu-filter
otsu filter and refactor
2 parents ed941ed + c5fd2ea commit c79603c

File tree

11 files changed

+260
-120
lines changed

11 files changed

+260
-120
lines changed

image-filtering/CMakeLists.txt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
cmake_minimum_required(VERSION 3.8)
22
project(image_filtering)
33

4-
# === C++ standard ===
54
if(NOT CMAKE_CXX_STANDARD)
65
set(CMAKE_CXX_STANDARD 20)
76
endif()
@@ -10,13 +9,14 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
109
add_compile_options(-Wall -Wextra -Wpedantic)
1110
endif()
1211

13-
# find dependencies
1412
find_package(ament_cmake REQUIRED)
1513
find_package(rclcpp REQUIRED)
1614
find_package(rclcpp_components REQUIRED)
1715
find_package(OpenCV 4.5.4 REQUIRED)
1816
find_package(sensor_msgs REQUIRED)
1917
find_package(cv_bridge REQUIRED)
18+
find_package(spdlog REQUIRED)
19+
find_package(fmt REQUIRED)
2020

2121
include_directories(include)
2222

@@ -42,11 +42,13 @@ ament_target_dependencies(${LIB_NAME} PUBLIC
4242
rclcpp_components
4343
cv_bridge
4444
sensor_msgs
45+
spdlog
46+
fmt
4547
)
4648

4749
rclcpp_components_register_node(
4850
${LIB_NAME}
49-
PLUGIN "vortex::image_processing::ImageFilteringNode"
51+
PLUGIN "ImageFilteringNode"
5052
EXECUTABLE ${PROJECT_NAME}_node
5153
)
5254

@@ -67,12 +69,8 @@ install(
6769

6870
install(DIRECTORY
6971
launch
70-
params
72+
config
7173
DESTINATION share/${PROJECT_NAME}/
7274
)
7375

74-
if(BUILD_TESTING)
75-
add_subdirectory(test)
76-
endif()
77-
7876
ament_package()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**:
2+
ros__parameters:
3+
sub_topic: "/downwards_camera/image_raw"
4+
pub_topic: "/filtered_image"
5+
output_encoding: "mono8"
6+
filter_params:
7+
filter_type: "otsu"
8+
flip:
9+
flip_code: 1
10+
unsharpening:
11+
blur_size: 8
12+
erosion:
13+
size: 1
14+
dilation:
15+
size: 1
16+
white_balancing:
17+
contrast_percentage: 0.1
18+
ebus:
19+
erosion_size: 2
20+
blur_size: 30
21+
mask_weight: 5
22+
otsu:
23+
gsc_weight_r: 1.0 # Grayscale red weight
24+
gsc_weight_g: 0.0 # Grayscale green weight
25+
gsc_weight_b: 0.0 # Grayscale blue weight
26+
gamma_auto_correction: true
27+
gamma_auto_correction_weight: 4.0
28+
otsu_segmentation: true
29+
erosion_size: 10
30+
dilation_size: 10
31+
32+
# Filter params should reflect the FilterParams struct
33+
# defined in /include/image_filters/image_processing.hpp

image-filtering/include/image_filters/image_filtering_ros.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#define IMAGE_FILTERING_ROS_HPP
33

44
#include <cv_bridge/cv_bridge.h>
5+
#include <spdlog/spdlog.h>
56
#include <rclcpp/parameter_event_handler.hpp>
67
#include <rclcpp/qos.hpp>
78
#include <rclcpp/rclcpp.hpp>
89
#include <sensor_msgs/msg/image.hpp>
910
#include "image_processing.hpp"
1011

11-
namespace vortex::image_processing {
1212
class ImageFilteringNode : public rclcpp::Node {
1313
public:
1414
explicit ImageFilteringNode(const rclcpp::NodeOptions& options);
@@ -103,6 +103,4 @@ class ImageFilteringNode : public rclcpp::Node {
103103
std::string filter_;
104104
};
105105

106-
} // namespace vortex::image_processing
107-
108106
#endif // IMAGE_FILTERING_ROS_HPP

image-filtering/include/image_filters/image_processing.hpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33

44
#include <iostream>
55
#include <map>
6+
#include <numeric>
67
#include <opencv2/core.hpp>
78
#include <opencv2/highgui.hpp>
89
#include <opencv2/imgcodecs.hpp>
910
#include <opencv2/imgproc.hpp>
1011
#include <opencv2/xphoto.hpp>
1112

12-
namespace vortex::image_processing {
13-
1413
struct FlipParams {
1514
int flip_code;
1615
};
@@ -36,13 +35,25 @@ struct EbusParams {
3635
int mask_weight;
3736
};
3837

38+
struct OtsuParams {
39+
bool gamma_auto_correction;
40+
double gamma_auto_correction_weight;
41+
bool otsu_segmentation;
42+
double gsc_weight_r;
43+
double gsc_weight_g;
44+
double gsc_weight_b;
45+
int erosion_size;
46+
int dilation_size;
47+
};
48+
3949
struct FilterParams {
4050
FlipParams flip;
4151
UnsharpeningParams unsharpening;
4252
ErodingParams eroding;
4353
DilatingParams dilating;
4454
WhiteBalancingParams white_balancing;
4555
EbusParams ebus;
56+
OtsuParams otsu;
4657
};
4758

4859
typedef void (*FilterFunction)(const FilterParams&, const cv::Mat&, cv::Mat&);
@@ -87,14 +98,14 @@ void unsharpening_filter(const FilterParams& params,
8798
/**
8899
* Expands the dark areas of the image
89100
*/
90-
void eroding_filter(const FilterParams& params,
101+
void erosion_filter(const FilterParams& params,
91102
const cv::Mat& original,
92103
cv::Mat& modified);
93104

94105
/**
95106
* Expands the bright areas of the image
96107
*/
97-
void dilating_filter(const FilterParams& params,
108+
void dilation_filter(const FilterParams& params,
98109
const cv::Mat& original,
99110
cv::Mat& modified);
100111

@@ -113,15 +124,22 @@ void ebus_filter(const FilterParams& params,
113124
const cv::Mat& original,
114125
cv::Mat& filtered);
115126

127+
/**
128+
* A filter based on Otsu's method
129+
*/
130+
void otsu_segmentation_filter(const FilterParams& params,
131+
const cv::Mat& original,
132+
cv::Mat& output);
133+
116134
const static std::map<std::string, FilterFunction> filter_functions = {
117135
{"no_filter", no_filter},
118136
{"flip", flip_filter},
119137
{"sharpening", sharpening_filter},
120138
{"unsharpening", unsharpening_filter},
121-
{"eroding", eroding_filter},
122-
{"dilating", dilating_filter},
139+
{"erosion", erosion_filter},
140+
{"dilation", dilation_filter},
123141
{"white_balancing", white_balance_filter},
124-
{"ebus", ebus_filter}};
142+
{"ebus", ebus_filter},
143+
{"otsu", otsu_segmentation_filter}};
125144

126-
} // namespace vortex::image_processing
127145
#endif // IMAGE_PROCESSING_HPP

image-filtering/launch/image_filtering_launch.py renamed to image-filtering/launch/image_filtering.launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def generate_launch_description():
1313
parameters=[
1414
os.path.join(
1515
get_package_share_directory('image_filtering'),
16-
'params',
16+
'config',
1717
'image_filtering_params.yaml',
1818
)
1919
],

image-filtering/package.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
<depend>rclcpp_components</depend>
1616
<depend>cv_bridge</depend>
1717
<depend>sensor_msgs</depend>
18-
<depend>image_transport</depend>
1918
<depend>rclcpp</depend>
2019

2120
<export>

image-filtering/params/image_filtering_params.yaml

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)