在自己搭建的控制器上面读取gmsl相机

笔者所在课题组从事无人驾驶相关方向研究,本人研究方向为视觉SLAM。课题组于校外成立研究院,并自主搭建了基于Xaier的控制器,购置森云智能gmsl相机,直接连与控制器上面。

相机协议为gmsl1(145),输出图像为1280*720,fps=25,编解码格式为YUYV。为在控制器上搭建视觉SLAM所需环境,做图下尝试:

一、安装系统ubuntu18.04

二、搭建ROS-melodic-desktop-full

三、安装opencv

1. 安装依赖环境:

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev 
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

常用问题:无法定位libjasper-dev

解决方法:

sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev

2. 下载opencv源码:

下载次新版opencv-3.4.15(Opencv源码下载地址—https://opencv.org/releases/)

3. 编译:

(1)解压

(2)mkdir build && cd ./build

(3)cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D   -D OPENCV_ENABLE_NONFREE=True ..
(4)make -j6

(5)sudo make install

4. 环境配置:

(1)修改etc/bash.bashrc

sudo gedit /etc/bash.bashrc

文件末尾添加以下内容,并保存

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

更新

sudo updatedb
source /etc/bash.bashrc

(2)修改动态库

#打开下列文件
sudo gedit /etc/ld.so.conf.d/opencv.conf 
 
# 添加lib路經 在 末尾 保存退出
/usr/local/lib
 
# 更新
sudo ldconfig

5. 检查安装情况:

#终端输入以下两命令,显示正常则安装成功
pkg-config --modversion opencv4 #查看版本号
pkg-config --libs opencv4 #查看libs库

6. 程序测试:

opencv在下载时已经提供了测试程序:~/opencv-4.5.1/samples/cpp/example_cmake$ 终端进入此目录后执行以下程序:

mkdir build
cd ./build
cmake ..
make
./opencv_example ## 生成一个可执行文件 拖入终端执行 也可

四、调用opencv读取相机数据:

#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <sstream>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>

using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
    ros::init(argc, argv, "read_gmsl_ros");
    ros::NodeHandle nh;
    image_transport::ImageTransport it(nh);
    image_transport::Publisher pub = it.advertise("/gmsl_cam/image_raw", 1);
    
    VideoCapture cap;
    
    if(!cap.open(0))
    {
      cout<<"Please check the ID of the camera."<<endl;
      return -1;
    }
    
    /*
     * 对该相机进行操作:
     * 1. 设置该相机输出输出不为RGB,即关掉默认输出
     * 2. 设置该相机的格式为YUYV
     * */
    cap.set(CAP_PROP_CONVERT_RGB, 0); 
    cap.set(CV_CAP_PROP_MODE, CV_CAP_MODE_YUYV);
    
    Mat image;
    
    //ros::Rate loop_rate(100);
    while(nh.ok())
    {
      cap >> image;
      
      sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
      pub.publish(msg);
      //ros::spinOnce();
      //loop_rate.sleep();
    }
    
    return 0;
}

详情可参考:https://blog.csdn.net/cyf15238622067/article/details/82767214

cmake_minimum_required(VERSION 3.0.2)
project(n_camera_prep)
SET(OpenCV_DIR /usr/local/share/OpenCV)

find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  roscpp
  OpenCV
)


catkin_package(

)

include_directories(
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)

add_executable(yuv2ros src/ReadYUVfile.cpp)

target_link_libraries(yuv2ros
  ${catkin_LIBRARIES}
  ${OpenCV_LIBS}
)

add_executable(read_gmsl_ros src/ReadGMSLvideo.cpp)

target_link_libraries(read_gmsl_ros
  ${catkin_LIBRARIES}
  ${OpenCV_LIBS}
)

 

上一篇:Spring Boot 2 发送邮件手把手图文教程


下一篇:安装Cartographer教程及踩坑