Hanjie's Blog

一只有理想的羊驼

在使用ZED Camera时,我希望获得原始的照片(未经过校正,对齐处理)。但由于系统是Ubuntu 14.04,只有ZED SDK 1.1.0版本支持,而对应的zed-ros-wrapper v1.0.0只能订阅已经经过校正对齐的照片/camera/left/image_rect_color/camera/rgb/image_rect_color(不过zed-ros-wrapper v1.2.0版本开始就开始提供原始照片的订阅,Topic为/camera/left/image_raw_color/camera/right/image_raw_color)。

如果直接使用ZED SDK所提供的图片获取接口'grab(); retrieveImage()',按照说明也是已经经过了对齐操作了,并非是原始照片。

还好由于ZED Camera是标准的网络摄像头,使用v4l2-ctl --all指令,有以下显示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Driver Info (not using libv4l2):
Driver name : uvcvideo
Card type : ZED
Bus info : usb-0000:00:14.0-3
Driver version: 4.4.59
Capabilities : 0x84200001
Video Capture
Streaming
Device Capabilities
Device Caps : 0x04200001
Video Capture
Streaming
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
Width/Height : 2560/720
Pixel Format : 'YUYV'
Field : None
Bytes per Line: 5120
Size Image : 3686400
Colorspace : SRGB
Custom Info : feedcafe
Crop Capability Video Capture:
Bounds : Left 0, Top 0, Width 2560, Height 720
Default : Left 0, Top 0, Width 2560, Height 720
Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 2560, Height 720
Selection: crop_bounds, Left 0, Top 0, Width 2560, Height 720
Streaming Parameters Video Capture:
Capabilities : timeperframe
Frames per second: 30.000 (30/1)
Read buffers : 0
brightness (int) : min=0 max=8 step=1 default=4 value=4
contrast (int) : min=0 max=8 step=1 default=4 value=4
saturation (int) : min=0 max=8 step=1 default=4 value=4
hue (int) : min=0 max=11 step=1 default=0 value=0
white_balance_temperature_auto (bool) : default=1 value=1
gain (int) : min=0 max=8 step=1 default=4 value=4
power_line_frequency (menu) : min=0 max=2 default=1 value=1
white_balance_temperature (int) : min=2800 max=6500 step=100 default=4600 value=4600 flags=inactive
sharpness (int) : min=0 max=8 step=1 default=4 value=4
exposure_auto (menu) : min=0 max=3 default=0 value=2
exposure_absolute (int) : min=5000 max=80000 step=5000 default=60000 value=45000 flags=inactive
exposure_auto_priority (bool) : default=0 value=0

所以可以使用OpenCV(opencv-2.4.13.2版本)自带的VideoCapture函数获取原始照片:

1
2
3
4
5
6
7
8
9
10
11
cv::VideoCapture* camera_ = new cv::VideoCapture(0);
cv::Mat raw, left_image, right_image;
if (camera_->grab()) {
camera_->retrieve(raw);
cv::Rect left_rect(0, 0, width_ / 2, height_);
cv::Rect right_rect(width_ / 2, 0, width_ / 2, height_);
left_image = raw(left_rect);
right_image = raw(right_rect);
cv::waitKey(10);
return true;
}

但在读取,设置摄像头快门速度(EXPOSURE)时,

1
2
camera_->set(CV_CAP_PROP_EXPOSURE, exposure_);
exposure_ = camera_->get(CV_CAP_PROP_EXPOSURE);

出现了以下错误:

1
2
HIGHGUI ERROR: V4L2: Unable to get property Exposure(9963793) - Invalid argument
HIGHGUI ERROR: V4L: Exposure control in V4L is not supported

发现是OpenCV的bug1,并且在最新的3.*版本已经修复,对于opencv-2.4.13.2版本需要手动修复。

解决方法

>在opencv-2.4.13.2/modules/highgui/src/cap_v4l.cpp文件中,将V4L2_CID_EXPOSURE替换为V4L2_CID_EXPOSURE_ABSOLUTE,重新build一次。

重新运行程序,发现读取问题解决了,但是设置参数时依然出现问题:

1
2
3
HIGHGUI ERROR: V4L2: Failed to set control "10094850": Input/output error (value 12500)
HIGHGUI WARNING: Setting property 10094850 through v4l2 failed. Trying with v4l1.
HIGHGUI ERROR: V4L: Exposure control in V4L is not supported

后来终于找到解决方法,需要先通过设置'exposure_auto',关闭自动模式,才能进行快门设置2。终端中运行:

1
v4l2-ctl --set-ctrl=exposure_auto=2

很奇怪,exposure_auto貌似只能设置为0/2。

为了让exposure_absolute生效,还需设置:

1
v4l2-ctl --set-ctrl=exposure_auto_priority=1

现在可以在C++中通过OpenCV设置摄像头的快门了。

以上方法经过测试发现都是无效的,V4L2无法对快门速度进行设置。

后来,发现可以通过对zed-ros-wrapper进行修改,进行快门设置。

zed_wrapper_nodelet.cpp中添加控制exposure的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(int argc, char **argv) {

// Defines the exposure control. A -1 value enable the AutoExposure/AutoGain control.
// Affected value should be between 0 and 100 for manual control.
// A 0 value only disable automode without modifing the last auto values,
// while a 1 to 100 value disable auto mode and set exposure to chosen value
int exposure = 50;

nh_ns.getParam("exposure", exposure);

// before zed->grab()
zed->setCameraSettingsValue(sl::zed::ZED_EXPOSURE, exposure);
}

然后就可以在launch文件中添加控制快门语句:

1
<param name="exposure" value="50"/>

  1. https://github.com/opencv/opencv/pull/6393↩︎

  2. https://askubuntu.com/questions/211971/v4l2-ctl-exposure-auto-setting-fails↩︎

Dependencies1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# CMake
sudo apt-get install cmake

# google-glog + gflags
sudo apt-get install libgoogle-glog-dev

# BLAS & LAPACK
sudo apt-get install libatlas-base-dev

# Eigen3
sudo apt-get install libeigen3-dev

# SuiteSparse and CXSparse (optional)
# - If you want to build Ceres as a *static* library (the default)
# you can use the SuiteSparse package in the main Ubuntu package
# repository:
sudo apt-get install libsuitesparse-dev

# - However, if you want to build Ceres as a *shared* library, you must
# add the following PPA:
sudo add-apt-repository ppa:bzindovic/suitesparse-bugfix-1319687
sudo apt-get update
sudo apt-get install libsuitesparse-dev
Install2
1
2
3
4
5
6
7
8
9
10
11
git clone https://ceres-solver.googlesource.com/ceres-solver
cd ceres-solver
mkdir ceres-bin
cd ceres-bin
cmake ..
make -j3
make test
# Optionally install Ceres, it can also be exported using CMake which
# allows Ceres to be used without requiring installation, see the documentation
# for the EXPORT_BUILD_DIR option for more information.
sudo make install

Test

1
bin/simple_bundle_adjuster ../data/problem-16-22106-pre.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
iter      cost      cost_change  |gradient|   |step|    tr_ratio  tr_radius  ls_iter  iter_time  total_time
0 4.185660e+06 0.00e+00 1.09e+08 0.00e+00 0.00e+00 1.00e+04 0 8.67e-02 2.07e-01
1 1.062590e+05 4.08e+06 8.99e+06 5.36e+02 9.82e-01 3.00e+04 1 1.85e-01 3.93e-01
2 4.992817e+04 5.63e+04 8.32e+06 3.19e+02 6.52e-01 3.09e+04 1 1.72e-01 5.65e-01
3 1.899774e+04 3.09e+04 1.60e+06 1.24e+02 9.77e-01 9.26e+04 1 1.77e-01 7.42e-01
4 1.808729e+04 9.10e+02 3.97e+05 6.39e+01 9.51e-01 2.78e+05 1 1.69e-01 9.11e-01
5 1.803399e+04 5.33e+01 1.48e+04 1.23e+01 9.99e-01 8.33e+05 1 1.74e-01 1.09e+00
6 1.803390e+04 9.02e-02 6.35e+01 8.00e-01 1.00e+00 2.50e+06 1 1.75e-01 1.26e+00

Solver Summary (v 1.13.0-eigen-(3.3.4)-lapack-suitesparse-(4.2.1)-cxsparse-(3.1.2)-openmp)

Original Reduced
Parameter blocks 22122 22122
Parameters 66462 66462
Residual blocks 83718 83718
Residual 167436 167436

Minimizer TRUST_REGION

Dense linear algebra library EIGEN
Trust region strategy LEVENBERG_MARQUARDT

Given Used
Linear solver DENSE_SCHUR DENSE_SCHUR
Threads 1 1
Linear solver threads 1 1
Linear solver ordering AUTOMATIC 22106,16
Schur structure 2,3,9 2,3,9

Cost:
Initial 4.185660e+06
Final 1.803390e+04
Change 4.167626e+06

Minimizer iterations 7
Successful steps 7
Unsuccessful steps 0

Time (in seconds):
Preprocessor 0.1207

Residual evaluation 0.1168
Jacobian evaluation 0.5111
Linear solver 0.5164
Minimizer 1.2377

Postprocessor 0.0032
Total 1.3617

Termination: CONVERGENCE (Function tolerance reached. |cost_change|/cost: 1.769761e-09 <= 1.000000e-06)

  1. http://ceres-solver.org/installation.html↩︎

  2. http://ceres-solver.org/installation.html↩︎

0%