OpenCV圖像處理之圖像拼接詳解
圖像拼接技術(shù)
一、需求分析
將下面兩張圖像進(jìn)行拼接


拼接得到一張完整的圖像

二、具體步驟
1.選擇特征點(diǎn)
//1、選擇特征點(diǎn)
//左圖 右圖 識別特征點(diǎn) 是Mat對象 用c d保存
surf->detectAndCompute(left,Mat(),key2,d);
surf->detectAndCompute(right,Mat(),key1,c);
//特征點(diǎn)對比,保存 特征點(diǎn)為中心點(diǎn)區(qū)域比對
vector<DMatch> matches;
matcher.match(d,c,matches);
//排序從小到大 找到特征點(diǎn)連線
sort(matches.begin(),matches.end());
2.保存最優(yōu)的特征點(diǎn)對象
//2、保存最優(yōu)的特征點(diǎn)對象
vector<DMatch>good_matches;
int ptrpoint = std::min(50,(int)(matches.size()*0.15));
for (int i = 0;i < ptrpoint;i++)
{
good_matches.push_back(matches[i]);
}
//2-1、畫線 最優(yōu)的特征點(diǎn)對象連線
Mat outimg;
drawMatches(left,key2,right,key1,good_matches,outimg,
Scalar::all(-1),Scalar::all(-1),
vector<char>(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
//imshow("outimg",outimg);
3.特征點(diǎn)匹配
//3、特征點(diǎn)匹配
vector<Point2f>imagepoint1,imagepoint2;
for (int i= 0 ;i < good_matches.size();i++)
{
//查找特征點(diǎn)可連接處 變形
imagepoint1.push_back(key1[good_matches[i].trainIdx].pt);
//查找特征點(diǎn)可連接處 查找基準(zhǔn)線
imagepoint2.push_back(key2[good_matches[i].queryIdx].pt);
}
4.透視轉(zhuǎn)換 圖像融合
//4、透視轉(zhuǎn)換 圖形融合
Mat homo = findHomography(imagepoint1,imagepoint2,CV_RANSAC);
//imshow("homo",homo);
//根據(jù)透視轉(zhuǎn)換矩陣進(jìn)行計(jì)算 四個(gè)坐標(biāo)
CalcCorners(homo,right);
//接收透視轉(zhuǎn)換結(jié)果
Mat imageTransForm;
//透視轉(zhuǎn)換
warpPerspective(right,imageTransForm,homo,
Size(MAX(corners.right_top.x,corners.right_bottom.x),left.rows));
//右圖透視變換 由于本次圖片材料是自己截圖拼接的 因此看不出透視變換的明顯特征
//imshow("imageTransForm",imageTransForm);
//結(jié)果進(jìn)行整合
int dst_width = imageTransForm.cols;
int dst_height = left.rows;
Mat dst(dst_height,dst_width,CV_8UC3);
dst.setTo(0);
imageTransForm.copyTo(dst(Rect(0,0,imageTransForm.cols,imageTransForm.rows)));
left.copyTo(dst(Rect(0,0,left.cols,left.rows)));
右圖的透視轉(zhuǎn)換,由于圖像材料是自己截圖拼接的,因此看不出透視變換的明顯特征,但根據(jù)上圖可知已經(jīng)做出透視變換圖像處理操作

左圖與右圖的透視轉(zhuǎn)換結(jié)果 拼接 【這里只是將窗口移動測試看下前面步驟是否正確】

可以看出左圖與右圖的透視轉(zhuǎn)換結(jié)果 是可以進(jìn)行接下來的圖像融合操作的
5.優(yōu)化圖像 進(jìn)行最終的結(jié)果展示
//5、優(yōu)化圖像
OptimizeSeam(left,imageTransForm,dst);
//最終圖像拼接結(jié)果
imshow("dst",dst);
可以看出 順利完成 兩張圖像拼接的圖像處理操作

三、代碼實(shí)現(xiàn)
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>//圖像融合
#include <opencv2/xfeatures2d.hpp>//拼接算法
#include <opencv2/calib3d.hpp>
#include <opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;
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];//變換后的坐標(biāo)值
Mat V2 = Mat(3, 1, CV_64FC1, v2); //列向量
Mat V1 = Mat(3, 1, CV_64FC1, v1); //列向量
V1 = H * V2;
//左上角(0,0,1)
cout << "V2: " << V2 << endl;
cout << "V1: " << V1 << endl;
corners.left_top.x = v1[0] / v1[2];
corners.left_top.y = v1[1] / v1[2];
//左下角(0,src.rows,1)
v2[0] = 0;
v2[1] = src.rows;
v2[2] = 1;
V2 = Mat(3, 1, CV_64FC1, v2); //列向量
V1 = Mat(3, 1, CV_64FC1, v1); //列向量
V1 = H * V2;
corners.left_bottom.x = v1[0] / v1[2];
corners.left_bottom.y = v1[1] / v1[2];
//右上角(src.cols,0,1)
v2[0] = src.cols;
v2[1] = 0;
v2[2] = 1;
V2 = Mat(3, 1, CV_64FC1, v2); //列向量
V1 = Mat(3, 1, CV_64FC1, v1); //列向量
V1 = H * V2;
corners.right_top.x = v1[0] / v1[2];
corners.right_top.y = v1[1] / v1[2];
//右下角(src.cols,src.rows,1)
v2[0] = src.cols;
v2[1] = src.rows;
v2[2] = 1;
V2 = Mat(3, 1, CV_64FC1, v2); //列向量
V1 = Mat(3, 1, CV_64FC1, v1); //列向量
V1 = H * V2;
corners.right_bottom.x = v1[0] / v1[2];
corners.right_bottom.y = v1[1] / v1[2];
}
//圖像融合的去裂縫處理操作
void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst)
{
int start = MIN(corners.left_top.x, corners.left_bottom.x);//開始位置,即重疊區(qū)域的左邊界
double processWidth = img1.cols - start;//重疊區(qū)域的寬度
int rows = dst.rows;
int cols = img1.cols; //注意,是列數(shù)*通道數(shù)
double alpha = 1;//img1中像素的權(quán)重
for (int i = 0; i < rows; i++)
{
uchar* p = img1.ptr<uchar>(i); //獲取第i行的首地址
uchar* t = trans.ptr<uchar>(i);
uchar* d = dst.ptr<uchar>(i);
for (int j = start; j < cols; j++)
{
//如果遇到圖像trans中無像素的黑點(diǎn),則完全拷貝img1中的數(shù)據(jù)
if (t[j * 3] == 0 && t[j * 3 + 1] == 0 && t[j * 3 + 2] == 0)
{
alpha = 1;
}
else
{
//img1中像素的權(quán)重,與當(dāng)前處理點(diǎn)距重疊區(qū)域左邊界的距離成正比,實(shí)驗(yàn)證明,這種方法確實(shí)好
alpha = (processWidth - (j - start)) / processWidth;
}
d[j * 3] = p[j * 3] * alpha + t[j * 3] * (1 - alpha);
d[j * 3 + 1] = p[j * 3 + 1] * alpha + t[j * 3 + 1] * (1 - alpha);
d[j * 3 + 2] = p[j * 3 + 2] * alpha + t[j * 3 + 2] * (1 - alpha);
}
}
}
int main()
{
//左圖
Mat left = imread("D:/00000000000003jieduanshipincailliao/a1.png");
//右圖
Mat right = imread("D:/00000000000003jieduanshipincailliao/a2.png");
//左右圖顯示
imshow("left",left);
imshow("right",right);
//創(chuàng)建SURF對象
Ptr<SURF> surf;
//create 函數(shù)參數(shù) 海森矩陣閥值 800特征點(diǎn)以內(nèi)
surf = SURF::create(800);
//創(chuàng)建一個(gè)暴力匹配器 用于特征點(diǎn)匹配
BFMatcher matcher;
//特征點(diǎn)容器 存放特征點(diǎn)KeyPoint
vector<KeyPoint>key1,key2;
//保存特征點(diǎn)
Mat c,d;
//1、選擇特征點(diǎn)
//左圖 右圖 識別特征點(diǎn) 是Mat對象 用c d保存
surf->detectAndCompute(left,Mat(),key2,d);
surf->detectAndCompute(right,Mat(),key1,c);
//特征點(diǎn)對比,保存 特征點(diǎn)為中心點(diǎn)區(qū)域比對
vector<DMatch> matches;
matcher.match(d,c,matches);
//排序從小到大 找到特征點(diǎn)連線
sort(matches.begin(),matches.end());
//2、保存最優(yōu)的特征點(diǎn)對象
vector<DMatch>good_matches;
int ptrpoint = std::min(50,(int)(matches.size()*0.15));
for (int i = 0;i < ptrpoint;i++)
{
good_matches.push_back(matches[i]);
}
//2-1、畫線 最優(yōu)的特征點(diǎn)對象連線
Mat outimg;
drawMatches(left,key2,right,key1,good_matches,outimg,
Scalar::all(-1),Scalar::all(-1),
vector<char>(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
//imshow("outimg",outimg);
//3、特征點(diǎn)匹配
vector<Point2f>imagepoint1,imagepoint2;
for (int i= 0 ;i < good_matches.size();i++)
{
//查找特征點(diǎn)可連接處 變形
imagepoint1.push_back(key1[good_matches[i].trainIdx].pt);
//查找特征點(diǎn)可連接處 查找基準(zhǔn)線
imagepoint2.push_back(key2[good_matches[i].queryIdx].pt);
}
//4、透視轉(zhuǎn)換 圖形融合
Mat homo = findHomography(imagepoint1,imagepoint2,CV_RANSAC);
//imshow("homo",homo);
//根據(jù)透視轉(zhuǎn)換矩陣進(jìn)行計(jì)算 四個(gè)坐標(biāo)
CalcCorners(homo,right);
//接收透視轉(zhuǎn)換結(jié)果
Mat imageTransForm;
//透視轉(zhuǎn)換
warpPerspective(right,imageTransForm,homo,
Size(MAX(corners.right_top.x,corners.right_bottom.x),left.rows));
//右圖透視變換 由于本次圖片材料是自己截圖拼接的 因此看不出透視變換的明顯特征
//imshow("imageTransForm",imageTransForm);
//結(jié)果進(jìn)行整合
int dst_width = imageTransForm.cols;
int dst_height = left.rows;
Mat dst(dst_height,dst_width,CV_8UC3);
dst.setTo(0);
imageTransForm.copyTo(dst(Rect(0,0,imageTransForm.cols,imageTransForm.rows)));
left.copyTo(dst(Rect(0,0,left.cols,left.rows)));
//5、優(yōu)化圖像
OptimizeSeam(left,imageTransForm,dst);
//最終圖像拼接結(jié)果
imshow("dst",dst);
waitKey(0);
return 0;
}到此這篇關(guān)于OpenCV圖像處理之圖像拼接詳解的文章就介紹到這了,更多相關(guān)OpenCV圖像拼接內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++中strlen函數(shù)的三種實(shí)現(xiàn)方法
在C語言中我們要獲取字符串的長度,可以使用strlen?函數(shù),strlen?函數(shù)計(jì)算字符串的長度時(shí),直到空結(jié)束字符,但不包括空結(jié)束字符,因?yàn)閟trlen函數(shù)時(shí)不包含最后的結(jié)束字符的,因此一般使用strlen函數(shù)計(jì)算的字符串的長度會比使用sizeof計(jì)算的字符串的字節(jié)數(shù)要小2022-05-05
C++中指針的數(shù)據(jù)類型和運(yùn)算相關(guān)知識小結(jié)
這篇文章主要介紹了C++中指針的數(shù)據(jù)類型和運(yùn)算相關(guān)知識小結(jié),是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
C++實(shí)現(xiàn)LeetCode(74.搜索一個(gè)二維矩陣)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(74.搜索一個(gè)二維矩陣),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
使用C++實(shí)現(xiàn)工資管理中的隨機(jī)教師信息生成功能
這篇文章主要介紹了使用C++實(shí)現(xiàn)工資管理中的隨機(jī)教師信息生成功能,想要做一個(gè)教師工資管理系統(tǒng),就必須得準(zhǔn)備好數(shù)據(jù),但是這些數(shù)據(jù)如果用手一行一行地敲,那么工作量是非常大的,因此,我就產(chǎn)生了用C語言實(shí)現(xiàn)直接生成大量的教師基本信息的想法,需要的朋友可以參考下2023-05-05
VC++中HTControl控制類使用之CHTDlgBase對話框基類實(shí)例
這篇文章主要介紹了VC++中HTControl控制類使用之CHTDlgBase對話框基類,是比較豐富而實(shí)用的功能,需要的朋友可以參考下2014-08-08

