现在正在做的一个项目,现将代码备份一下,循环可以设置任意幅图像拼接。
#ifndef debug
#include <opencv2/opencv.hpp>
#include<opencv2/nonfree/nonfree.hpp>
#include<opencv2/legacy/legacy.hpp>
#include<vector>
#include<iostream>
#include<sstream>
#include<string>
#include<time.h>
using namespace std;
using namespace cv;
#include "SurfStitch.h"
int main() {
//Mat img1 = imread("1.jpg");
Mat img1 = imread("E:\\DATA\\银行票据\\1\\1 (14).jpg");
Mat stitchedImage;
int n;
cout << "Dataset2" << endl;
cout << "请输入想拼接的图片数量(大于1小于18)" << endl;
cin >> n;
cout << "输入成功,开始计时" << endl;
clock_t start,finish;
double totaltime;
start=clock();
//将首张图片翻转
transpose(img1,img1);//转置
flip(img1,img1,0);
for (int k = 15; k <= n; k++) {
stringstream stream;
string str;
stream << k;
stream >> str;
string filename = "1 ";
filename = "E:\\DATA\\银行票据\\1\\"+filename+"("+str+")" + ".jpg";
cout << "正在拼接......." << filename << endl;
//const char *wh = filename.c_str();
//IplImage *temp = cvLoadImage(wh, -1);
//cvTranspose(temp,temp);
//Mat img(temp,0);
Mat img = imread(filename);
if (img.empty())
{
break;
}
transpose(img,img);//转置
//flip(img,img,1);
flip(img,img,0);
imwrite("1.bmp",img);
stitchedImage = SurfStich(img1,img);
//stitchedImage = Stitched(img1, img);
imwrite("ResultImage1.jpg", stitchedImage);
img1 = stitchedImage;
}
finish = clock();
totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "拼接成功" << endl;
cout << "拼接花费总时间为:" << totaltime << "秒!" << endl;
imshow("ResultImage", stitchedImage);
imwrite("ResultImage.jpg", stitchedImage);
waitKey(0);
return 0;
}
#endif
#include "highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/legacy/legacy.hpp"
#include <iostream>
using namespace cv;
using namespace std;
void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst);
typedef struct
{
Point2f left_top;
Point2f left_bottom;
Point2f right_top;
Point2f right_bottom;
}four_corners_t;
four_corners_t corners;
void CalcCorners(const Mat& H, const Mat& src)
{
double v2[] = { 0, 0, 1 };//左上角
double v1[3];//变换后的坐标值
Mat V2 = Mat(3, 1, CV_64FC1, v2); //列向量
Mat V1 = Mat(3, 1, CV_64FC1, v1); //列向量
V1 = H * V2;
//左上角(0,0,1)
cout << "V2: "