Ros kinetic 中自带的OpenCV使用测试

Ros kinetic 中自带的OpenCV使用测试

kinetic自带OpenCV-3.3.1

头文件包括

#include 
#include 
#include 

CMakeLists.txt 改动

set(OpenCV_DIR /opt/ros/kinetic/share/OpenCV-3.3.1-dev)
find_package(catkin REQUIRED COMPONENTS 
  roscpp
  OpenCV
)
include_directories(
 include
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)
target_link_libraries(testing
${catkin_LIBRARIES} 
${OpenCV_LIBRARIES}
)

开始测试

进入~/catkin_ws/src,终端中输入

$ catkin_create_pkg test1 roscpp

在test1/src中创建test.cpp,内容为

#include 
#include 
#include 
#include 
using namespace cv;
int main(int argc, char** argv )
{
   //判断输入参数个数是否合法
   if ( argc != 2 )
   {
	   printf("usage: DisplayImage.out \n");
	   return -1;
   }
    //读入图像
	Mat image;
	image = imread( argv[1], 1 );
	//判断输入路径下图像是否存在
	if ( !image.data )
	{
		printf("No image data \n");
		return -1;
	}
	//新建一个窗口显示图像
	namedWindow("Display Image", WINDOW_AUTOSIZE );
	imshow("Display Image", image);
	waitKey(0);
	return 0;
}

修改CMakeLists.txt为

cmake_minimum_required(VERSION 2.8.3)
project(test1)
set(OpenCV_DIR /opt/ros/kinetic/share/OpenCV-3.3.1-dev)
find_package(catkin REQUIRED COMPONENTS 
  roscpp
  OpenCV
)

include_directories(
 include
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)

add_executable(testing src/test.cpp)
target_link_libraries(testing
${catkin_LIBRARIES} 
${OpenCV_LIBRARIES}
)

运行
在Pictures文件夹下存入图片test.jpg
在终端中运行

$ cd catkin_ws
$ catkin_make
$ source devel/setup.bash
$ rosrun test1 testing /home/myname/Pictures/test.jpg

特别说明

1、cpp中的头文件,路径可能会不一样
2、CMakeLists.txt文件中如果没有

set(OpenCV_DIR /opt/ros/kinetic/share/OpenCV-3.3.1-dev)

会报错:

  Could not find a package configuration file provided by "OpenCV" with any
  of the following names:

    OpenCVConfig.cmake
    opencv-config.cmake

所以要只要添加一个包含 OpenCVConfig.cmake, opencv-config.cmake的路径即可
3、生成的执行文件(程序中的testing)不要命名为test
4、cpp的参数中,argc为运行时输入的参数数量,输入一个参数时为2,
argv为,argv[0]指向输入的运行程序路径及名称
argv[1]指向第一个参数字符串,即图片/home/myname/Pictures/test.jpg

你可能感兴趣的:(ros)