OpenCV图像拼接函数

图像拼接函数

第一种方法:通过遍历图像,将待拼接的图像每个像素赋值给输出图像

//图像拼接函数
//imageVector  输入图像数组
//outputImage   输出图像
//colCount_   //每一行图像的数量
//imageWidth_,imageHeight_  //每一个输入图像的宽高,必须大小一致
void ImageStitching(vector<Mat> imageVector,Mat &outputImage,int rowImageCount_,int imageWidth_,int imageHeight_)
{
    for(int i=0;i<imageVector.size();i++)
    {
        Mat image = imageVector.at(i);
        for(int row=0;row<image.rows;row++)
        {
            Vec3b *pixRow = image.ptr<Vec3b>(row);    //pixRow行指针
            Vec3b *outPixRow = outputImage.ptr<Vec3b>(row + i/rowImageCount_*imageHeight_); //指针偏移
            for(int col=0;col<image.cols;col++)
            {
                outPixRow[col + i%rowImageCount_*imageWidth_] = pixRow[col];
            }
        }
    }
    //  imshow("outputImage",outputImage);
}

 第二种方法:OpenCV自带的拼接函数hconcatvconcat,将多张图像同时进行拼接。

//图像拼接函数
//imageVector  输入图像数组
//outputImage   输出图像
//colCount_   //每一行图像的数量
//imageWidth_,imageHeight_  //每一个输入图像的宽高,必须大小一致
void ImageStitching(vector<Mat> imageVector,Mat &outputImage,int rowImageCount_,int imageWidth_,int imageHeight_)
{
    vector<Mat> imageVec;   //存放待横向合并的图片
    vector<Mat> hImageVec;  //存放横向合并的结果图

    for(int i=0;i<imageVector.size();i++)
    {
        //横向合并
        if((i+1)%rowImageCount_ == 0)   //图片数量已经足够
        {
            Mat combine(imageHeight_,imageWidth_,CV_8UC3);  //每一行合并的结果图
            imageVec.push_back(imageVector.at(i));
            hconcat(imageVec,combine);  //横向合并
            hImageVec.push_back(combine);
            imageVec.clear();   //清空,继续添加下一行的图片
        }
        else
        {
            imageVec.push_back(imageVector.at(i));
        }
    }
    //将横向合并后的图像竖向合成一张图
    vconcat(hImageVec,outputImage);
//    imshow("outputImage2",outputImage);
}

调用函数

Mat img1,img2,img3,img4,img5,img6;
    vector<Mat> inputImageVector;
    img1 = imread("1.bmp");
    img2 = imread("2.bmp");
    img3 = imread("3.bmp");
    img4 = imread("4.bmp");
    img5 = imread("5.bmp");
    img6 = imread("6.bmp");
    if(!img1.empty() && !img2.empty() && !img3.empty() && !img4.empty() && !img5.empty() && !img6.empty())
    {
        inputImageVector.push_back(img1);
        inputImageVector.push_back(img2);
        inputImageVector.push_back(img3);
        inputImageVector.push_back(img4);
        inputImageVector.push_back(img5);
        inputImageVector.push_back(img6);
        const int imageWidth = 54;
        const int imageHeight = 55;
        const int rowImageCount = 3;  //表示三张图片合成一行
        Mat outputimage(imageHeight*2,imageWidth*3,CV_8UC3);
//        const int rowCount = 2;
        ImageStitching(inputImageVector,outputimage,rowImageCount,imageWidth,imageHeight);
    }

效果展示

未拼接的图片

拼接后的图片

函数耗时

double start = (double)getTickCount();
test()//函数
double time = ((double)getTickCount() - to)/getTickFrequency();    //time就是函数运行的时间

可以通过getTickCount(),getTickFrequency()测试函数的耗时,如上。具体耗时还请大家自行测试~

结尾

结尾了~不知道该说些啥,嗯嗯嗯嗯呃,希望我的文章对大家有帮助吧。

猜你喜欢

转载自www.cnblogs.com/yysky/p/10955960.html