我正在尝试使用OpenCV拼接器类来拼接来自立体设置的多个帧,其中两个相机都不会移动。当跨越多个帧运行时,我的镶合效果很差。我尝试了一些不同的方法,在此我将尝试解释。
使用stitcher.stitch( )
鉴于一对立体视图,我为一些帧运行了以下代码(VideoFile
是OpenCV VideoCapture
对象的自定义包装器):
VideoFile f1( ... );
VideoFile f2( ... );
cv::Mat output_frame;
cv::Stitcher stitcher = cv::Stitcher::createDefault(true);
for( int i = 0; i < num_frames; i++ ) {
currentFrames.push_back(f1.frame( ));
currentFrames.push_back(f2.frame( ));
stitcher.stitch( currentFrames, output_mat );
// Write output_mat, put it in a named window, etc...
f1.next_frame();
f2.next_frame();
currentFrames.clear();
}
这种方法在每一帧上都有非常好的结果,但由于参数是在每个视频帧中估计的,因此您可以看到拼接处的小差异,其中参数略有不同。
使用estimateTransform()
和composePanorama()
为了解决上述方法的问题,我决定尝试仅在第一帧上估计参数,然后使用composePanorama()
来拼接所有后续帧。
for( int i = 0; i < num_frames; i++ ) {
currentFrames.push_back(f1.frame( ));
currentFrames.push_back(f2.frame( ));
if( ! have_transform ) {
status = stitcher.estimateTransform( currentFrames );
}
status = stitcher.composePanorama(currentFrames, output_frame );
// ... as above
}
不幸的是,似乎存在一个错误(在这里记录下来),导致两个视图以一种非常奇怪的方式分离,如下面的图片所示:
第一帧:
第二帧:
...
第八帧:
显然这没有用,但我认为这可能只是因为错误,该错误基本上会使内部参数矩阵在每次调用composePanorama()
时乘以一个常数。所以我对错误进行了微小的修补,阻止了这种情况发生,但是拼接结果很差。以下是修补程序(modules/stitching/src/stitcher.cpp
)和结果:
243 for (size_t i = 0; i < imgs_.size(); ++i)
244 {
245 // Update intrinsics
246 // change following to *=1 to prevent scaling error, but messes up stitching.
247 cameras_[i].focal *= compose_work_aspect;
248 cameras_[i].ppx *= compose_work_aspect;
249 cameras_[i].ppy *= compose_work_aspect;
结果:
有没有人知道我怎么能解决这个问题?基本上,我需要计算出变换一次,然后在其余帧(大约30分钟的视频)中使用它。
我最好想要一些关于修补拼缝器类的建议,但我也愿意尝试手动编写不同的解决方案。之前的一个尝试涉及查找SURF点、相关性和寻找单应性,与拼缝器类相比效果相当差,因此我宁愿在可能的情况下使用该类。