OpenCV中的全景拼接例程
使用Stitcher类,通过createDefault()方法创建拼接对象,通过stitch()方法执行默认的自动拼接。自动拼接和07年Brown和Lowe发表的论文描述的步骤基本一致,只不过使用的特征提取算法是ORB,而不是慢吞吞、有专利保护的SIFT和SURF。开源万岁!
代码内容:设置几张图片,扔到向量里面,然后计算全景图。
opencv-3.0.0源码中没有找到测试图片,很蛋碎。到github上找了下,发现都在[https://github.com/Itseez/opencv_extra](opencv_extra)这个项目下。。使用到了boat1.jpg~boat6.jpg
在fedora22+i53210+12G内存+全SSD条件下测试,还是有点慢的,大概5,6秒才出结果。当然,如果只有2张图片,秒出。
代码:
//图像拼接 //哦,这个程序是最简单的拼接,最傻瓜的那种,不必知道拼接的pipeline //只需要调用createDefault()和stitch()方法就可以完成拼接 #include <iostream> #include <opencv2/opencv.hpp> #include <opencv2/stitching/stitcher.hpp> using namespace std; using namespace cv; string IMAGE_PATH_PREFIX = "/home/chris/Pictures/"; bool try_use_gpu = false; vector<Mat> imgs; string result_name = IMAGE_PATH_PREFIX + "result.jpg"; int main() { Mat img = imread(IMAGE_PATH_PREFIX + "boat1.jpg"); imgs.push_back(img); img=imread(IMAGE_PATH_PREFIX+"boat2.jpg"); imgs.push_back(img); img=imread(IMAGE_PATH_PREFIX+"boat3.jpg"); imgs.push_back(img); img=imread(IMAGE_PATH_PREFIX+"boat3.jpg"); imgs.push_back(img); img=imread(IMAGE_PATH_PREFIX+"boat4.jpg"); imgs.push_back(img); img=imread(IMAGE_PATH_PREFIX+"boat5.jpg"); imgs.push_back(img); img=imread(IMAGE_PATH_PREFIX+"boat6.jpg"); imgs.push_back(img); Mat pano;//拼接结果图片 //Stitcher stitcher = Stitcher::createDefault(try_use_gpu); Stitcher stitcher = Stitcher::createDefault(true); Stitcher::Status status = stitcher.stitch(imgs, pano); if (status != Stitcher::OK) { cout << "Can't stitch images, error code = " << int(status) << endl; return -1; } imwrite(result_name, pano); } int main_test_feature_algo(){ #ifdef HAVE_OPENCV_XFEATURES2D cout << "Surf" << endl; #else cout << "Orb" << endl; #endif }
当然你也可以看下opencv-3.0.0/samples/cpp/stitching.cpp的代码
效果图:
Greatness is never a given, it must be earned.