07.06工作日记,学习ros功能包,发布/接收图片

刚到公司啥都不会,公司让搞,ros学一下吧

参考连接:

https://www.cnblogs.com/penuel/p/11340317.html      创建功能包

https://blog.csdn.net/qq_34935373/article/details/103909011       ros发布/接收图片

https://blog.csdn.net/weixin_36976685/article/details/93994357

https://www.pianshen.com/article/97421551634/             这俩都是opencv踩坑

07.07更新 https://www.freesion.com/article/5587455636/  ros发布pcd数据然后rviz接收

一.创建功能包:

1.mkdir -p ~/catkin_ws/src创建工作ws和src文件夹

(或cd catkin_ws  然后mkdir src #创建src文件夹,代码丢到这里面)

2在src目录下:catkin_init_worskspace 初始化一下

3.此时src目录下会有一个 CMakelists.txt

4.cd ~/catkin_ws/

5.catkin_make 编译一下,此时

07.06工作日记,学习ros功能包,发布/接收图片_第1张图片

工作空间下会有biuld devel 和src

6.在bashrc中添加 source ~/catkin_ws/devel/setup.bash  

此时可以输入 echo $ROS_PACKAGE_PATH来看一下 是否make成功,输出是一条路径

7.source ~/.bachsrc 改变环境变量 (如何你 终端打开 gedit ~/.bashsrc里面有setup.sh就不用每次都输了)

8.创建功能包

cd ~/catkin_ws/src里

catkin_create_pkg 你功能包的名字 std_msgs rospy roscpp

9.cd ~/catkin_ws

10.catkin_make

此时创建好功能包

里面应该有三个东西,src目录 CMakelists.txt 和package.xml

完成功能包的创建。

二.利用ros发/收图片

在刚才最后一步,创建好的功能包我们命名成redwall_arm_vision,

里面只有三个文件,src目录和CMakeList.txt、package.xml,在src目录添加一张图片用于接下来的处理,我用的是大家非常熟悉的 lena.jpg:

07.06工作日记,学习ros功能包,发布/接收图片_第2张图片

利用vscode 输入 code. 打开项目,src目录下右键添加一个cpp文件,就命名为main.cpp把,程序内容如下:

#include 
#include 
#include 
using namespace std;
 
// OpenCV includes
#include 
#include 
#include 
#include 
#include 
#include 
using namespace cv;
 
#include 
#include 
#include 
#include 
 
int main( int argc, char** argv )
{
    int sample;
    cout << "请输入0~7:" << endl;
    cin>>sample;
    switch(sample){
	case 0:
	{
            cout << "Sample 0, Mat zeros" << endl;
	    Mat m= Mat::zeros(5,5, CV_32F);
	    cout << m << endl;
            break;
	}
        case 1:
        {	
            cout << "Sample 0, Mat ones" << endl;
	    Mat m= Mat::ones(5,5, CV_32F);
	    cout << m << endl;
	    break;
	}
	case 2:
	{	
            cout << "Sample 0, Mat eye" << endl;
            Mat m= Mat::eye(5,5, CV_32F);
	    cout << m << endl;
 
	    Mat a= Mat::eye(Size(3,2), CV_32F);
	    Mat b= Mat::ones(Size(3,2), CV_32F);
	    Mat c= a+b;
	    Mat d= a-b;
            cout << "Sample 0, 矩阵元素和差" << endl;
	    cout << a << endl;
	    cout << b << endl;
	    cout << c << endl;
	    cout << d << endl;
	    break;
	}
	case 3:
	{	
            cout << "Sample 0, Mat operations:" << endl;
	    Mat m0= Mat::eye(3,3, CV_32F);
	    m0=m0+Mat::ones(3,3, CV_32F);
	    Mat m1= Mat::eye(2,3, CV_32F);
	    Mat m2= Mat::ones(3,2, CV_32F);
 
	    cout << "\nm0\n" << m0 << endl;
	    cout << "\nm1\n" << m1 << endl;
	    cout << "\nm2\n" << m2 << endl;
 
	    cout << "\nm1.*2\n" << m1*2 << endl;
	    cout << "\n(m1+2).*(m1+3)\n" << (m1+1).mul(m1+3) << endl;
	    cout << "\nm1*m2\n" << m1*m2 << endl;
	    cout << "\nt(m2)\n" << m2.t() << endl;
	    cout << "\ninv(m0)\n" << m0.inv() << endl;
	    break;
	}
	case 4:
	{
            Mat image= imread("/home/redwall/catkin_ws/src/redwall_arm_vision/src/lena.jpg", CV_LOAD_IMAGE_COLOR);
            int myRow=511;
            int myCol=511;
	    int val=*(image.data+myRow*image.cols*image.channels()+ myCol);
	    cout << "Pixel value: " << val << endl;
	    // 有imshow就会报段错误
	    // imshow("Lena", image);
	    break;
	}
	case 5:
	{
            Mat image= imread("/home/redwall/catkin_ws/src/redwall_arm_vision/src/lena.jpg", CV_LOAD_IMAGE_COLOR);
            int myRow=511;
	    int myCol=511;
	    int B=*(image.data+myRow*image.cols*image.channels()+ myCol + 0);
	    int G=*(image.data+myRow*image.cols*image.channels()+ myCol + 1);
	    int R=*(image.data+myRow*image.cols*image.channels()+ myCol + 2);
	    cout << "Pixel value (B,G,R): (" << B << "," << G << "," << R << ")" << endl;
	    break;
	}
	case 6:
	{
            ros::init(argc,argv,"image_color");
            ros::NodeHandle nh;    
            image_transport::ImageTransport it(nh);
            image_transport::Publisher pub = it.advertise("camera/image", 1);
            /**************ROS与Opencv图像转换***********************/
            Mat image= imread("/home/redwall/catkin_ws/src/redwall_arm_vision/src/lena.jpg", CV_LOAD_IMAGE_COLOR);
            sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
            ros::Rate loop_rate(5);
            while (nh.ok()) {
                pub.publish(msg);
                ros::spinOnce();
                loop_rate.sleep();
            }
	}
 
        case 7:
        {
            ros::init(argc,argv,"image_gray");
            ros::NodeHandle nh;
            image_transport::ImageTransport it(nh);
            image_transport::Publisher pub = it.advertise("camera/image", 1);
            /**************ROS与Opencv图像转换***********************/
            Mat gray= imread("/home/redwall/catkin_ws/src/redwall_arm_vision/src/lena.jpg", CV_LOAD_IMAGE_GRAYSCALE);
            sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "mono8", gray).toImageMsg();
            ros::Rate loop_rate(5);
            while (nh.ok()) {
                pub.publish(msg);
                ros::spinOnce();
                loop_rate.sleep();
            }
        }
    }
 
    return 0;
 
}

然后我们需要更改src里面的CMakelists.txt内容:

cmake_minimum_required(VERSION 2.8.3)
project(redwall_arm_vision)
 
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  cv_bridge
  geometry_msgs
  sensor_msgs
  OpenCV
  image_transport
)
 
catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES redwall_arm_vision
   CATKIN_DEPENDS roscpp rospy std_msgs
#  DEPENDS system_lib
)
 
include_directories(
# include
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)
 
add_executable(main src/main.cpp)
target_link_libraries(main ${catkin_LIBRARIES} ${OpenCV_LIBRARIES})

继续改package.xml里面的内容



  redwall_arm_vision
  0.0.0
  redwall_arm_vision
 
  
  
  
  sijia
 
  TODO
 
  
  catkin
  roscpp
  rospy
  std_msgs
  cv_bridge
  geometry_msgs
  sensor_msgs
  image_transport
 
  roscpp
  rospy
  std_msgs
  cv_bridge
  geometry_msgs
  sensor_msgs
  message_generation
  image_transport
 
  cv_bridge
  geometry_msgs
  sensor_msgs
  message_runtime
  roscpp
  rospy
  std_msgs
  image_transport
 

此刻,完成功能包构建

1.编译功能包:

$ catkin_make -DCATKIN_WHITELIST_PACKAGES="redwall_arm_vision"

踩坑点a.记得要将main.cpp里面图片的路径改成自己的地址,首先在文件夹里复制图片,然后在代码段路径地方右键粘贴,直接可以复制路径。

此刻编译成功之后

2.打开终端roscore启动

rosrun redwall_arm_vision(功能包名)main(功能名)   来运行我们的功能

3.打开另一个终端 rviz

add我们的图片 显示出来了

07.06工作日记,学习ros功能包,发布/接收图片_第3张图片

你可能感兴趣的:(ros,工作笔记)