火影 发表于 7 天前

在ros2上使用opencv显示一张图片

1.先将图片放到桌面上
https://i-blog.csdnimg.cn/direct/945c971af99549db89f4bf327a12c8b1.png
2.打开终端ctrl+alt+T,检察自己是否已安装opencv
https://i-blog.csdnimg.cn/direct/10dc1751c4cb4d4fba3a107fa0188db6.png
3.创建工作环境
https://i-blog.csdnimg.cn/direct/fe0ebfd2880648ef986a24917f8e8a86.png
4.进入工作目次并创建ROS2包添加OpenCV依靠项
https://i-blog.csdnimg.cn/direct/b2723cfddd914e76996b24f6d32bb326.png
5.进入/home/kong/opencv_ws/opencv_use/src目次创建.cpp文件并编辑
https://i-blog.csdnimg.cn/direct/3d847e3a6357492281cc09fae568833a.png
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.编译运行
https://i-blog.csdnimg.cn/direct/3bd505b38f514868af225c25ef468a9c.png
现象
https://i-blog.csdnimg.cn/direct/ab513605e4794138b02cc428cec545b7.png



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 在ros2上使用opencv显示一张图片