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()測試函數的耗時,如上。具體耗時還請大家自行測試~

 

結尾

結尾了~不知道該說些啥,嗯嗯嗯嗯呃,希望我的文章對大家有幫助吧。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM