OPENCV 图像拼接 stitching的使用

    xiaoxiao2026-06-21  3

    图像拼接技术,现在有非常广泛的应用,如小型机器人的单目视觉的视野较为小,使机器人在应用时有一定的局限性,双目视觉能提供更广阔的视野,双目视觉中较为常用的是对左右两个摄像头获取的识图通过拼接来获取更广阔的视野,再如较为热门的VR,Youtube也极大地运用图像拼接的技术。 图像拼接主要分为几个主要的步骤: (1)图像的校正:由于成象器件拍摄姿态和扫描非线性会引起的图象几何失真,由于成像系统本身的原因会产生图像的畸变,通过图像的几何校正,图像的畸变校正,使获取的图像更大的接近真实的图像; (2)图像的滤波:均值滤波,中值滤波; (3)图像的配准:即图像的对齐,寻找图像边缘重合的部分,常用的图像匹配方法有Harris算法,SIFT算法等; (4)建立变换模型:根据模板或者图像特征之间的对应关系,计算出数学模型中的各参数值,从而建立两幅图像的数学变换模型 (5)坐标变换:根据建立的数学转换模型,将待拼接图像转换到参考图像的坐标系中,完成统一坐标变换。 (6)图像的融合:将待拼接图像的重合区域进行融合得到拼接重构的平滑无缝全景图像。 先介绍一下opencv中自带的图像拼接例子stitching:

    #include <iostream> #include <fstream> #include "opencv2/highgui/highgui.hpp" #include <opencv2\stitching\stitcher.hpp> using namespace std; using namespace cv; int main(void) { string srcFile[3] = { "1.jpg", "2.jpg", "3.jpg" }; string dstFile = "result.jpg"; vector<Mat> imgs; for (int i = 0; i<3; ++i) { Mat img = imread(srcFile[i]); if (img.empty()) { cout << "Can't read image '" << srcFile[i] << "'\n"; system("pause"); return -1; } imgs.push_back(img); } cout << "Please wait..." << endl; Mat pano; Stitcher stitcher = Stitcher::createDefault(false); Stitcher::Status status = stitcher.stitch(imgs, pano); if (status != Stitcher::OK) { cout << "Can't stitch images, error code=" << int(status) << endl; system("pause"); return -1; } imwrite(dstFile, pano); namedWindow("Result"); imshow("Result", pano); waitKey(0); destroyWindow("Result"); system("pause"); return 0; }

    先for循环读入图片,并往vector后加入一个数据;

    Stitcher Stitcher::createDefault(bool try_use_gpu=false)

    再用createDefault创建图像拼接的默认参数;

    Status Stitcher::stitch(InputArray images, OutputArray pano)

    Stitcher::stitch给出给出合成的图片; 下面是笔者合成图片的效果:

    图像有点畸变(可能是拍摄时的问题),不过拼接拼接效果还是很不错的 opencv提供的API手册中对stitcher介绍较少,

    接下来尝试自己用代码实现一下

    最新回复(0)