SLAM算法与工程实践系列文章
下面是SLAM算法与工程实践系列文章的总链接,本人发表这个系列的文章链接均收录于此
SLAM算法与工程实践系列文章链接
下面是专栏地址:
SLAM算法与工程实践系列专栏
文章目录
- SLAM算法与工程实践系列文章
- SLAM算法与工程实践系列文章链接
- SLAM算法与工程实践系列专栏
- 前言
- SLAM算法与工程实践——相机篇:传统相机使用(1)
- 相机相关命令
- 出现的问题
- 调用相机
- 以MJPG格式打开相机
前言
这个系列的文章是分享SLAM相关技术算法的学习和工程实践
SLAM算法与工程实践——相机篇:传统相机使用(1)
相机相关命令
插上USB相机,使用命令查看USB设备
lsusb
可以识别相机
使用命令查看识别到几个摄像头
ll /dev/video*
然后改变其权限
sudo chmod 777 /dev/video0
sudo chmod 777 /dev/video1
安装 v4l-utils
工具包
sudo apt-get install v4l-utils
使用下列命令查看相机参数
v4l2-ctl -d /dev/video0 --list-formats-ext
在命令行输出为:
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture
[0]: 'MJPG' (Motion-JPEG, compressed)
Size: Discrete 2560x800
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.067s (15.000 fps)
Size: Discrete 2560x720
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1280x800
Interval: Discrete 0.008s (120.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1280x792
Interval: Discrete 0.008s (120.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1280x400
Interval: Discrete 0.008s (120.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x400
Interval: Discrete 0.005s (210.000 fps)
Interval: Discrete 0.008s (120.000 fps)
Interval: Discrete 0.017s (60.000 fps)
Size: Discrete 640x392
Interval: Discrete 0.005s (210.000 fps)
Interval: Discrete 0.008s (120.000 fps)
Interval: Discrete 0.017s (60.000 fps)
[1]: 'YUYV' (YUYV 4:2:2)
Size: Discrete 2560x800
Interval: Discrete 0.500s (2.000 fps)
Size: Discrete 2560x720
Interval: Discrete 0.500s (2.000 fps)
Size: Discrete 1280x800
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x792
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x400
Interval: Discrete 0.067s (15.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 640x400
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x392
Interval: Discrete 0.033s (30.000 fps)
可以看出本相机的参数和对应设置下的帧率,最高为 120 帧,分辨率为 2560x800,我这选用 1280x400
接下来看看你的USB摄像头支持什么附加属性,只需要执行下面的命令即可
v4l2-ctl -d /dev/video0 --list-ctrls
得到下面的结果
User Controls
brightness 0x00980900 (int) : min=-64 max=64 step=1 default=0 value=0
contrast 0x00980901 (int) : min=0 max=95 step=1 default=0 value=0
saturation 0x00980902 (int) : min=0 max=100 step=1 default=64 value=64
hue 0x00980903 (int) : min=-2000 max=2000 step=1 default=0 value=0
white_balance_automatic 0x0098090c (bool) : default=1 value=1
gamma 0x00980910 (int) : min=100 max=300 step=1 default=100 value=100
使用应用程序茄子(cheese)
输入命令:
sudo apt-get install cheese
装好后,用命令:cheese
,即可打开。如果指定打开video1,输入命令:
cheese -d /dev/video1
读取相机配置文件 HBV-1780-2.yaml
int UseStereoCam::loadCamParam(std::string cfgPath) {
cv::FileStorage fs(cfgPath, cv::FileStorage::READ);
if (fs.isOpened()) {
std::cout << "Stereo Camera Paramter is loading..." << std::endl;
//内参矩阵
fs["IntrinsicMatrixLeft"] >> IntrinsicMatrixLeft; //存放内参矩阵,左相机
IntrinsicMatrixLeft.at<double>(0, 0) = IntrinsicMatrixLeft.at<double>(0, 0) * resizeScalse;
IntrinsicMatrixLeft.at<double>(1, 1) = IntrinsicMatrixLeft.at<double>(1, 1) * resizeScalse;
IntrinsicMatrixLeft.at<double>(0, 2) = IntrinsicMatrixLeft.at<double>(0, 2) * resizeScalse;
IntrinsicMatrixLeft.at<double>(1, 2) = IntrinsicMatrixLeft.at<double>(1, 2) * resizeScalse;
fs["IntrinsicMatrixRight"] >> IntrinsicMatrixRight; //存放内参矩阵,右相机
IntrinsicMatrixRight.at<double>(0, 0) = IntrinsicMatrixRight.at<double>(0, 0) * resizeScalse;
IntrinsicMatrixRight.at<double>(1, 1) = IntrinsicMatrixRight.at<double>(1, 1) * resizeScalse;
IntrinsicMatrixRight.at<double>(0, 2) = IntrinsicMatrixRight.at<double>(0, 2) * resizeScalse;
IntrinsicMatrixRight.at<double>(1, 2) = IntrinsicMatrixRight.at<double>(1, 2) * resizeScalse;
//畸变系数
fs["DistCoeffLeft"] >> DistCoeffLeft; //畸变矩阵,左相机
fs["DistCoeffRight"] >> DistCoeffRight; //畸变矩阵,右相机
//旋转矩阵 平移向量
fs["RotationMatrix"] >> R;
fs["TranslationVector"] >> T;
T = T * resizeScalse;
fs.release();
leftFocalLength =
(IntrinsicMatrixLeft.at<double>(0, 0) + IntrinsicMatrixLeft.at<double>(1, 1)) / 2.0;
rightFocalLength =
(IntrinsicMatrixRight.at<double>(0, 0) + IntrinsicMatrixRight.at<double>(1, 1)) / 2.0;
// 处理焦距(例如取平均值)
focalLength = (leftFocalLength + rightFocalLength) / 2.0;
fx = (IntrinsicMatrixLeft.at<double>(0, 0) + IntrinsicMatrixRight.at<double>(0, 0)) / 2.0;
fy = (IntrinsicMatrixLeft.at<double>(1, 1) + IntrinsicMatrixRight.at<double>(1, 1)) / 2.0;
cx = (IntrinsicMatrixLeft.at<double>(0, 2) + IntrinsicMatrixRight.at<double>(0, 2)) / 2.0;
cy = (IntrinsicMatrixLeft.at<double>(1, 2) + IntrinsicMatrixRight.at<double>(1, 2)) / 2.0;
baseline = cv::norm(T) / 100; //基线(baseline)单位设置为分米
std::cout << "camera parameter loaded successfully!" << std::endl;
return 0;
} else {
std::cout << "Failed to load camera parameter!" << std::endl;
return -1;
}
}
出现的问题
参考:
解决OpenCV的GStreamer warning警告
在用opencv调用摄像头时报错
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1758) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Device '/dev/video0' failed during initialization
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (515) startPipeline OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1057) setProperty OpenCV | GStreamer warning: no pipeline
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
摄像头打开失败!
解决办法:
修改打开摄像头的代码如下:
cap.open(0,CAP_V4L2);
调用相机
在编写多个主函数共用src
和include
定义的类的文件时,CMakeLists.txt的编写应该类似下面的写法,然后包含每个子项目的子文件夹
cmake_minimum_required(VERSION 2.8)
project(DEPTH_COMPUTE)
IF (NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF ()
MESSAGE("Build type: " ${CMAKE_BUILD_TYPE})
# set path
set(LIB_PATH ${PROJECT_SOURCE_DIR}/lib)
set(HEAD_PATH ${PROJECT_SOURCE_DIR}/include)
set(EXEC_PATH ${PROJECT_SOURCE_DIR}/bin)
set(DEPTH_LIB calc_depth) # set libiary name
find_package(OpenCV 4 REQUIRED)
find_package(Eigen3 3.1.0 NO_MODULE)
find_package(Pangolin REQUIRED)
include_directories(${HEAD_PATH})
aux_source_directory(${PROJECT_SOURCE_DIR}/src LIB_SRC)
SET(LIBRARY_OUTPUT_PATH ${LIB_PATH})
add_library(${DEPTH_LIB} SHARED ${LIB_SRC})
TARGET_LINK_LIBRARIES(${DEPTH_LIB}
${OpenCV_LIBS}
${EIGEN3_LIBS}
${Pangolin_LIBRARIES}
)
# add subdirectories 这里是具体的主函数
add_subdirectory(useCamForShot)
add_subdirectory(openCamera)
子项目的CMakeLists.txt应该如下
cmake_minimum_required(VERSION 2.8)
project(OPENCAM)
find_package(OpenCV 4 REQUIRED)
include_directories(${HEAD_PATH})
link_directories(${LIB_PATH})
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-O3")
set(EXECUTABLE_OUTPUT_PATH ${EXEC_PATH})
#add_library(${DEPTH_LIB} SHARED ${SRC_LIST})
add_executable(open_camera open_camera.cpp)
target_link_libraries(open_camera ${DEPTH_LIB} ${OpenCV_LIBS})
以MJPG格式打开相机
参考:
OpenCV+V4L实现MJPG格式拉取USB摄像头
关于openCV VideoCapture获取摄像头MJPG视频的问题
相机支持的视频流格式
在打开相机后,设置参数
capture.open(camIndex, cv::CAP_V4L); // open camera
//设置编码格式,这里只能写在分辨率和帧率后面,否则无效
capture.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));//视频流格式