您的位置:首页 > 运维架构

OpenCV图片拼接

2014-04-25 18:01 363 查看
一、原图









1.jpg 2.jpg 3.jpg

二、拼接效果
1、拼接效果之一:简单拼接,有重叠,看着不太舒服






2、拼接效果之二:高级拼接,这下貌似好多了




三、源代码(一)
#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#pragma comment(lib,"opencv_core245.lib")
#pragma comment(lib,"opencv_highgui245.lib")
int main(){    
    char* file[3]={"1.jpg","2.jpg","3.jpg"};//3张原始图片    
    IplImage* pImg[3];      
    int i;    
    
    for(i=0;i<3;++i)        
        pImg[i]=cvLoadImage(file[i]);
        
    int sw=pImg[0]->width;    
    int sh=pImg[0]->height;
    IplImage* dstImg = cvCreateImage(cvSize(sw*3,sh),pImg[0]->depth,pImg[0]->nChannels);  
      
    cvZero(dstImg);
    printf("Please wait...\n");
    
    for(i=0;i<3;++i) {          
        cvSetImageROI(dstImg, cvRect(i*sw,0,sw,sh));         
        cvCopy(pImg[i], dstImg);          
        cvResetImageROI(dstImg);    
    }
    
    cvNamedWindow("dstImg");    
    cvShowImage("dstImg", dstImg);   
    cvSaveImage("result1.jpg",dstImg);//拼接图片之一    
    
    cvWaitKey(0);    
    
    for(i=0;i<3;++i)   
        cvReleaseImage(&pImg[i]);    
        
     cvReleaseImage(&dstImg);    
     cvDestroyWindow("dstImg");      
     system("pause");    
     return 0;
}



2、源代码(二)
#include <iostream>
#include <fstream>
#include "opencv2/highgui/highgui.hpp
"#include "opencv2/stitching/stitcher.hpp"
using namespace std;
using namespace cv;
#pragma comment(lib,"opencv_core245.lib")
#pragma comment(lib,"opencv_highgui245.lib")
#pragma comment(lib,"opencv_stitching245.lib")
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;
}



注:
1、原始图片(1,2,3)来源/article/1390603.html,缩放至1/4
2、代码(一)参考http://www.cnblogs.com/CBDoctor/archive/2011/09/19/2180998.html ,有较小改动
3、代码(二)参考OpenCV自带samples\cpp\stitching.cpp,改动较大
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: