1.先将图片放到桌面上
2.打开终端ctrl+alt+T,检察自己是否已安装opencv
3.创建工作环境
4.进入工作目次并创建ROS2包添加OpenCV依靠项
5.进入/home/kong/opencv_ws/opencv_use/src目次创建.cpp文件并编辑
6.代码如下
- my_opencv.cpp
- #include <cstdio>
- #include <opencv2/opencv.hpp>
- using namespace cv;
- int main(int argc, char ** argv)
- {
- (void) argc;
- (void) argv;
- Mat src = imread("/home/kong/Desktop/image.png");
- if(src.empty()){
- printf("无法读取图片,请检查图片路径是否正确。\n");
- }
- imshow("src",src);
- waitKey(0);
- printf("hello world opencv_use package\n");
- return 0;
- }
复制代码- CMakeLists.txt
- cmake_minimum_required(VERSION 3.8)
- project(opencv_use)
- 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(rclcpp REQUIRED)
- find_package(OpenCV REQUIRED)
- find_package(sensor_msgs REQUIRED)
- find_package(cv_bridge REQUIRED)
- find_package(image_transport REQUIRED)
- include_directories(${OpenCV_INCLUDE_DIRS})
- add_executable(my_opencv src/my_opencv.cpp)
- target_link_libraries(my_opencv ${OpenCV_LIBRARIES})
- target_include_directories(my_opencv PUBLIC
- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
- $<INSTALL_INTERFACE:include>)
- target_compile_features(my_opencv PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17
- ament_target_dependencies(
- my_opencv
- "rclcpp"
- "OpenCV"
- "sensor_msgs"
- "cv_bridge"
- "image_transport"
- )
- install(TARGETS my_opencv
- 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()
复制代码 7.编译运行

现象

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |