一、原图
1.jpg 2.jpg 3.jpg
二、拼接效果
1、拼接效果之一:简单拼接,有重叠,看着不太舒服
2、拼接效果之二:高级拼接,这下貌似好多了
三、源代码(一)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#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、源代码(二)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#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)来源http://blog.csdn.net/yang_xian521/article/details/7589431,缩放至1/4
2、代码(一)参考http://www.cnblogs.com/CBDoctor/archive/2011/09/19/2180998.html ,有较小改动
3、代码(二)参考OpenCV自带samples\cpp\stitching.cpp,改动较大