Ubuntu22.04, ros2 uses the built-in opencv to read pictures

I have been looking for tutorials on how to use the built-in opencv library or customize the installation of the opencv library in ros2 from the Internet for a long time. I found it confusing. After following the configuration, it still cannot be used. The most common problem is that it cannot be found. Undefined errors such as the header file or undefined reference to 'cv::imread(std::cxxll::basic string<char, std::char traits, std::allocator > const&, int)'.
And because I followed the tutorial to install opencv, the opencv that came with my ros2 was overwritten, and then I spent some time to reinstall ros2. I was learning ros2
with teacher Zhao Xuzuo before, and the teacher said that if I have any questions, I can go to the ros2 forum to search. , there may be answers, so I searched with the attitude of giving it a try, and sure enough I found a solution to the problem of how to use the opencv that comes with ros2.

1. First verify whether ros2 has opencv installed.

Run in the terminal:
pkg-config --modversion opencv4
will find the version number of opencv, indicating that opencv already exists.

2. Integrate opencv and ros2

When creating the ros2 function package, add opencv dependencies:
ros2 pkg create my_ros2_opencv_pkg --dependencies rclcpp OpenCV
or
ros2 pkg create my_ros2_opencv_pkg --dependencies rclcpp std_msgs sensor_msgs cv_bridge image_transport OpenCV
These two instructions can be followed by the --node-name node. name

3. In the .cpp file, add the header file:

#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.hpp>
#include <opencv2/opencv.hpp>

4. Configure CMakeLists.txt

If --node-name is added when generating the function package, there is no need to configure the CMakeLists.txt file.
add_executable(my_ros2_opencv_node src/name_of_your_cpp_file.cpp)
ament_target_dependencies(my_ros2_opencv_node rclcpp std_msgs sensor_msgs cv_bridge image_transport OpenCV)
install(TARGETS
my_ros2_opencv_node
DESTINATION lib/${PROJECT_NAME}
)
Original link .
(Infringement and deletion)

5. Add the path of the image

When using the imread function, you need to add the path of the picture, find the picture that needs to be read, right-click the mouse, select "Properties", find "Basic" -> "Superior Folder", select it, copy and paste it into the program, and then Change / to // to read the image.
The procedure is as follows:

#include "rclcpp/rclcpp.hpp"
#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
int main(int argc, char ** argv)
{
    
    
  (void) argc;
  (void) argv;

  printf("hello world pkg00_opencv package\n");
  Mat m=imread("//home//***//image//01.jpg");  //路径需要自己修改成自己电脑中的
  RCLCPP_INFO(rclcpp::get_logger("rclcpp"),"size:%d,%d",m.rows,m.cols);
  imshow("01",m);
  waitKey(0);
  return 0;
}

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_42283539/article/details/129027158