Open CV学习记录(十九)—图片拼接

时间:2022-02-11 00:09:31
/*************************************************************************
Can't stitch images, error code=1
************************************************************************/


#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]={"./RawImage2/1.jpg","./RawImage2/2.jpg","./RawImage2/3.jpg"};
string dstFile="./RawImage2/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;
}
#include <iostream>
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap1(0);
VideoCapture cap2(1);
if (!cap1.isOpened())
{
cout << "failed to open!\n";
return -1;
}
if (!cap2.isOpened())
{
cout << "failed to open!\n";
return -1;
}
const char* filename1 = "video1.avi";
const char* filename2 = "video2.avi";
double w = 640, h = 480;
cap1.set(CAP_PROP_FRAME_HEIGHT, h);
cap1.set(CAP_PROP_FRAME_WIDTH, w);
cap2.set(CAP_PROP_FRAME_HEIGHT, h);
cap2.set(CAP_PROP_FRAME_WIDTH, w);
cvNamedWindow("Camera_1");
cvNamedWindow("Camera_2");
Mat frame1, frame2;
VideoWriter writer1 = VideoWriter(filename1, -1, 25, frame1.size());
VideoWriter writer2 = VideoWriter(filename2, -1, 25, frame2.size());
while (1)
{
cap1 >> frame1;
cap2 >> frame2;
if (frame1.empty())
{
cout << "failed from cap1 to frame1!\n";
return -1;
}
if (frame2.empty())
{
cout << "failed from cap2 to frame2!\n";
return -1;
}
imshow("Camera_1", frame1);
imshow("Camera_2", frame2);
writer1 << frame1;
writer2 << frame2;
if (!writer1.isOpened())
{
cout << "failed from frame1 to writer1!\n";
return -1;
}
if (!writer2.isOpened())
{
cout << "failed from frame2 to writer2!\n";
return -1;
}
if (waitKey(30) == 27)
break;
}
cout << "Done!\n";
}
//#include <cv.h>
//#include <highgui.h>
//#include <stdlib.h>
#include <iostream>
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
//#pragma comment(lib,"opencv_core245.lib")
//#pragma comment(lib,"opencv_highgui245.lib")
int main() {
const char* file[9] = { "E:\\3.jpg","E:\\3.jpg","E:\\3.jpg","E:\\3.jpg","E:\\3.jpg","E:\\3.jpg","E:\\3.jpg","E:\\3.jpg","E:\\3.jpg" };//9张原始图片
IplImage* pImg[9];
int i;

for (i = 0; i<9; ++i)
pImg[i] = cvLoadImage(file[i]);

int sw = pImg[0]->width;
int sh = pImg[0]->height;
IplImage* dstImg = cvCreateImage(cvSize(sw * 3, sh*3), pImg[0]->depth, pImg[0]->nChannels);

cvZero(dstImg);
printf("Please wait...\n");

for (i = 0; i<3; ++i)
{
for (int j = 0; j < 3; j++)
{
cvSetImageROI(dstImg, cvRect(i*sw, j*sh, sw, sh));
cvCopy(pImg[i], dstImg);
cvResetImageROI(dstImg);
cout << (i+1)*(j+1) << " 张处理结束...\n";
}
}

namedWindow("dstImg",WINDOW_NORMAL);
cvShowImage("dstImg", dstImg);
cvSaveImage("result1.jpg", dstImg);//拼接图片之一

cvWaitKey(0);

for (i = 0; i<4; ++i)
cvReleaseImage(&pImg[i]);

cvReleaseImage(&dstImg);
cvDestroyWindow("dstImg");
system("pause");
return 0;
}