This commit is contained in:
Robot 2023-10-27 21:57:54 +08:00
parent 3997ee6f6f
commit aeebb08296
3 changed files with 69 additions and 11 deletions

View File

@ -0,0 +1,36 @@
cmake_minimum_required(VERSION 3.8)
project(cv_pkg)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(cv_bridge REQUIRED)
find_package(OpenCV REQUIRED)
add_executable(cv_hsv src/cv_hsv.cpp)
ament_target_dependencies(cv_hsv "rclcpp" "sensor_msgs" "cv_bridge" "OpenCV")
install(TARGETS cv_hsv
DESTINATION lib/${PROJECT_NAME})
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()

View File

@ -1,6 +1,6 @@
#include <rclcpp/rclcpp.hpp>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/msg/image.hpp>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
@ -27,22 +27,24 @@ void CamRGBCallback(const sensor_msgs::msg::Image::SharedPtr msg)
// Convert RGB image to HSV
Mat imgHSV;
vector<Mat> hsvSplit;
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV);
// Perform histogram equalization in the HSV space
vector<Mat> hsvSplit;
split(imgHSV, hsvSplit);
equalizeHist(hsvSplit[2], hsvSplit[2]);
merge(hsvSplit, imgHSV);
Mat imgThresholded;
// Threshold the image using the specified Hue, Saturation, and Value thresholds
inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded);
Mat imgThresholded;
inRange(imgHSV,
Scalar(iLowH, iLowS, iLowV),
Scalar(iHighH, iHighS, iHighV),
imgThresholded);
// Opening operation (remove some noise)
Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));
// Opening operation (remove some noise)
morphologyEx(imgThresholded, imgThresholded, MORPH_OPEN, element);
// Closing operation (connect some regions)
morphologyEx(imgThresholded, imgThresholded, MORPH_CLOSE, element);
@ -52,7 +54,6 @@ void CamRGBCallback(const sensor_msgs::msg::Image::SharedPtr msg)
int nPixCount = 0;
int nImgWidth = imgThresholded.cols;
int nImgHeight = imgThresholded.rows;
int nImgChannels = imgThresholded.channels();
for (int y = 0; y < nImgHeight; y++)
{
for (int x = 0; x < nImgWidth; x++)
@ -69,7 +70,7 @@ void CamRGBCallback(const sensor_msgs::msg::Image::SharedPtr msg)
{
nTargetX /= nPixCount;
nTargetY /= nPixCount;
printf("Color centroid coordinates (%d, %d) Point count = %d\n", nTargetX, nTargetY, nPixCount);
printf("Target (%d, %d) PixelCount = %d\n", nTargetX, nTargetY, nPixCount);
// Draw coordinates
Point line_begin = Point(nTargetX - 10, nTargetY);
Point line_end = Point(nTargetX + 10, nTargetY);
@ -80,7 +81,7 @@ void CamRGBCallback(const sensor_msgs::msg::Image::SharedPtr msg)
}
else
{
printf("Target color disappeared...\n");
printf("Target disappeared...\n");
}
// Display the processed images
@ -101,8 +102,6 @@ int main(int argc, char **argv)
// Create windows for image display and parameter adjustment
namedWindow("Threshold", WINDOW_AUTOSIZE);
// createTrackbar( "LowH", "Threshold", nullptr, 179, 0 );
// setTrackbarPos( "LowH", "Threshold", iLowH);
createTrackbar("LowH", "Threshold", &iLowH, 179); //Hue (0 - 179)
createTrackbar("HighH", "Threshold", &iHighH, 179);

23
demo_package/9_cv_hsv.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>cv_pkg</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="robot@6-robot.com">robot</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>sensor_msgs</depend>
<depend>cv_bridge</depend>
<depend>OpenCV</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>