基於模板匹配的方式進行影象拼接
目的:對兩幅影象進行影象拼接
適用條件: 兩幅圖有完全相同的行或列
本程式碼是:上下拼接,其中,img1 是下圖,imgR2是 上圖;且兩原圖 列相同
程式碼:
int main() { //從檔案中讀入影象 Mat img1 = imread("D:\\12.jpg", 1); Mat imgR2 = imread("D:\\11.jpg", 1); //cout << "height: " << img.rows << "width: " << img.cols << endl;
//影象裁剪: 從影象1裁剪重疊區域,作為模板 Mat imgR1 = img1(Rect(0,0 , img1.cols, 3));
int iwidth = imgR2.cols - imgR1.cols + 1; int iheight = imgR2.rows - imgR1.rows + 1;
Mat ftmp(iwidth, iheight, CV_32S, 1);
double min_val, max_val; Point min_loc, max_loc;
matchTemplate(imgR2, imgR1, ftmp, 0); minMaxLoc(ftmp, &min_val, &max_val, &min_loc, &max_loc);
Mat result(min_loc.y + img1.rows, imgR1.cols, img1.type());
imgR2.rowRange(0,min_loc.y).copyTo(result.rowRange(0, min_loc.y)); img1.rowRange(0, img1.rows).copyTo(result.rowRange(min_loc.y, result.rows));
cout << "min_loc: " << min_loc.y << endl;
imshow("res", result); imwrite("D:\\1010.jpg", result);
cvWaitKey(0);
system("pause"); }