Commit 6189047f authored by CANNERE Xavier's avatar CANNERE Xavier
Browse files

6.1.8

parent 680c218e
......@@ -197,12 +197,239 @@ After that you can put on comment the *RCLCPP* lines, there were here to test th
return 0;
}
Step 4 : Complete the methods
-----------------------------
In *getImage* complete the code with that. Depending the version of OpenCv, it will choose different configuration::
#if CV_VERSION_MAJOR == 4
IplImage _ipl_img=cvIplImage(cv_ptr->image);
#else
IplImage _ipl_img=cv_ptr->image;
#endif
IplImage *ptr_ipl_img= &_ipl_img;
Now we'll create the window where the image will be output::
cv::imshow(OPENCV_WINDOW, cv_ptr->image);
cv::waitKey(3);
But for that you need to declare this global variable::
static const std::string OPENCV_WINDOW = "Image window";
It doesn't compile, you need to add an other ``cpp`` file, create **DetectionCnam_codels.cpp** and paste that::
#include "DetectionCnam_codels.hpp"
CvPoint gravcenter;
using namespace std;
CvPoint binarisation(IplImage* image, int b, int g, int r, int tolerance) {
int x, y;
IplImage *mask, *bgr;
IplConvKernel *kernel;
int sommeX = 0, sommeY = 0;
int nbPixels = 0;
//gravcenter = {0,0};
gravcenter.x=0;
gravcenter.y=0;
// Create the mask &initialize it to white (no color detected)
mask = cvCreateImage(cvGetSize(image), image->depth, 1);
// Create the bgr image
bgr = cvCloneImage(image);
// We create the mask
cvInRangeS(bgr, cvScalar(b-tolerance, g-tolerance,r-tolerance), cvScalar(b+tolerance, g+tolerance,r+tolerance), mask);
// Create kernels for the morphological operation
kernel = cvCreateStructuringElementEx(5, 5, 2, 2, CV_SHAPE_ELLIPSE);
// Morphological opening (inverse because we have white pixels on black background)
cvDilate(mask, mask, kernel, 1);
cvErode(mask, mask, kernel, 1);
// We go through the mask to look for the tracked object and get its gravity center
for(x = 0; x < mask->width; x++) {
for(y = 0; y < mask->height; y++) {
// If its a tracked pixel, count it to the center of gravity's calcul
if(((uchar *)(mask->imageData + y*mask->widthStep))[x] == 255) {
sommeX += x;
sommeY += y;
(nbPixels)++;
}
}
}
// Show the result of the mask image
cvShowImage("DetectionCnam_Codels Mask", mask);
//Image camera
cvShowImage("image_camera",image);
cvWaitKey(3);
// We release the memory of kernels
cvReleaseStructuringElement(&kernel);
// We release the memory of the mask
cvReleaseImage(&mask);
// We release the memory of the hsv image
cvReleaseImage(&bgr);
//return nbPixels;
if(nbPixels > 0)
{
// gravcenter = ((int)(sommeX / (nbPixels)), (int)(sommeY / (*nbPixels)));
gravcenter.x = (int)(sommeX / (nbPixels));
gravcenter.y = (int)(sommeY / (nbPixels));
cvCircle(image,gravcenter,10,cvScalar(0,0,255,0),2);
cvShowImage("image_camera",image);
cvWaitKey(3);
return gravcenter;
} else
return cvPoint(-1, -1);
}
Create an other files ``Detectioncnam_codels.hpp`` in the include of the package and paste that inside::
#ifndef _CNAM_DETECTION_CODELS_HPP_
#define _CNAM_DETECTION_CODELS_HPP_
//pour utiliser la librairie OpenCV:
#include <opencv2/opencv.hpp>
#include <opencv2/core/core_c.h>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/highgui/highgui_c.h>
CvPoint binarisation(IplImage* image, int b, int g, int r, int tolerance);
#endif //_CNAM_DETECTION_CODELS_HPP_
Finally edit your CMakeLists and replace the existant code by that::
cmake_minimum_required(VERSION 3.8)
project(pkg_visual)
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)
find_package(sensor_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(rclcpp REQUIRED)
find_package(OpenCV REQUIRED)
find_package(cv_bridge REQUIRED)
find_package(image_transport REQUIRED)
add_executable(visual src/visual.cpp src/DetectionCnam_codels.cpp)
target_include_directories(visual PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_compile_features(visual PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17
ament_target_dependencies(
visual
"sensor_msgs"
"geometry_msgs"
"rclcpp"
"OpenCV"
"cv_bridge"
"image_transport"
)
install(TARGETS visual
DESTINATION lib/${PROJECT_NAME})
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
Your main files should look like this::
#include <cstdio>
#include "opencv2/opencv.hpp"
#include "rclcpp/rclcpp.hpp"
#include "sensor_msgs/msg/image.hpp"
#include "geometry_msgs/msg/twist.hpp"
#include "cv_bridge/cv_bridge.h"
#include "DetectionCnam_codels.hpp"
static const std::string OPENCV_WINDOW = "Image window";
rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr img_;
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr pub_;
class Visual : public rclcpp::Node
{
public:
Visual()
: Node("visu")
{
auto sensor_qos = rclcpp::QoS(rclcpp::SensorDataQoS());
img_ = this->create_subscription<sensor_msgs::msg::Image>(
"/camera/rgb/image_raw", sensor_qos,
std::bind(&Visual::getImage, this, std::placeholders::_1));
pub_ = this->create_publisher<geometry_msgs::msg::Twist>("/cmd_vel", 1);
}
private:
void getImage(const sensor_msgs::msg::Image::SharedPtr _msg)
{
cv_bridge::CvImagePtr cv_ptr;
try
{
cv_ptr = cv_bridge::toCvCopy(_msg, sensor_msgs::image_encodings::BGR8);
//RCLCPP_INFO("I have received image! ;-)");
}
catch (cv_bridge::Exception& e)
{
//RCLCPP_ERROR("cv_bridge exception: %s", e.what());
return;
}
#if CV_VERSION_MAJOR == 4
IplImage _ipl_img=cvIplImage(cv_ptr->image);
#else
IplImage _ipl_img=cv_ptr->image;
#endif
IplImage *ptr_ipl_img= &_ipl_img;
//For see OpenCV Image:
// Update GUI Window
cv::imshow(OPENCV_WINDOW, cv_ptr->image);
cv::waitKey(3);
}
};
int main(int argc, char ** argv)
{
(void) argc;
(void) argv;
rclcpp::init(argc, argv);
auto node = std::make_shared<Visual>();
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
Step 5 :
--------
Add this following code. We are declaring a point in the image and a message which contains pose::
CvPoint point;
geometry_msgs::msg::Twist cmd;
point = binarisation(ptr_ipl_img, b, g, r, seuil);
We need to declare variable for the point. Add that before the last``}`` of the class::
std::uint8_t b;
std::uint8_t g;
std::uint8_t r;
std::uint8_t seuil;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment