参考github上的SDK安装思路,在此做一下备份。 安装librealsense
- Build and Patch Kernel Modules for Jetson L4T
- Navigate to the root of libreansense2 directory.
- Run the script (note the ending characters - L4T)
./scripts/patch-realsense-ubuntu-L4T.sh
- Build librealsense2 SDK
sudo apt-get install git libssl-dev libusb-1.0-0-dev libudev-dev pkg-config libgtk-3-dev -y
./scripts/setup_udev_rules.sh
mkdir build && cd build
cmake .. -DBUILD_EXAMPLES=true -DCMAKE_BUILD_TYPE=release -DFORCE_RSUSB_BACKEND=false -DBUILD_WITH_CUDA=true && make -j$(($(nproc)-1)) && sudo make install
安装SDK对应版本的ros参考官网即可。
绕过ros,直接使用opencv调用librealsense,启动相机
cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8, 60);
cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 60);
rs2::device dev = pipe.get_active_profile().get_device();
dev.query_sensors()[1].set_option(rs2_option::RS2_OPTION_EXPOSURE, 50);
这里需要先获取到设备序列dev,第一个输出深度信息,第二个输出RGB信息,所以调整RGB的曝光值需要对第二个sensor设置。
dep_sensor = dev.query_sensors()[0];
color_sensor = dev.query_sensors()[1];
rs2_option::RS2_OPTION_EXPOSURE为想要设置的内容,可以替换,具体参见: rs_option.h File Reference
写好代码后放在ros工作空间下使用 需要在CMakelist中进行相关设置,主要是target_link_libraries, include_directories,以及find_package:
find_package(OpenCV REQUIRED)
find_package(realsense2 REQUIRED)
include_directories(
${catkin_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
${realsense2_LIBRARY}
)
set(DEPENDENCIES realsense2 )
target_link_libraries(
rs_camera
${catkin_LIBRARIES}
${OpenCV_LIBRARIES}
${DEPENDENCIES}
)
编译即可。 最后附上本文代码:
#include <librealsense2/rs.hpp>
#include <iostream>
#include <opencv2/opencv.hpp>
int main(int argc, char * argv[]) try
{
rs2::config cfg;
cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8, 60);
cfg.enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 60);
rs2::pipeline pipe;
pipe.start(cfg);
rs2::frameset frames;
rs2::device dev = pipe.get_active_profile().get_device();
dev.query_sensors()[1].set_option(rs2_option::RS2_OPTION_EXPOSURE, 50);
while (true)
{
frames = pipe.wait_for_frames();
rs2::align align_to_color(RS2_STREAM_COLOR);
frames = align_to_color.process(frames);
rs2::frame depth = frames.get_depth_frame();
rs2::frame color = frames.get_color_frame();
if (!color || !depth) break;
cv::Mat image(cv::Size(640, 480), CV_8UC3, (void*)color.get_data(), cv::Mat::AUTO_STEP);
cv::Mat depthmat(cv::Size(640, 480), CV_16U, (void*)depth.get_data(), cv::Mat::AUTO_STEP);
cv::imshow("image",image);
cv::imshow("depth",depthmat);
cv::waitKey(1);
}
return 1;
}
catch (const rs2::error & e)
{
std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return 0;
}
参考链接: librealsense API 官方github
|