欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解C++?OpenCV實(shí)現(xiàn)圖像拼接的原理及方法

 更新時間:2022年07月19日 08:24:25   作者:猿力豬  
本文以實(shí)現(xiàn)圖像拼接為目標(biāo),把分割開的圖像進(jìn)行拼接還原,核心的內(nèi)容包括:OpenCV圖像拼接相關(guān)原理以及OpenCV圖像拼接案例的實(shí)現(xiàn),感興趣的可以了解一下

前言

本文以實(shí)現(xiàn)圖像拼接為目標(biāo),把分割開的圖像進(jìn)行拼接還原,核心的內(nèi)容包括:OpenCV圖像拼接相關(guān)原理以及OpenCV圖像拼接案例的實(shí)現(xiàn)

一、圖像拼接相關(guān)原理 

圖像特征采集

一幅圖中總存在著一些獨(dú)特的像素點(diǎn),這些點(diǎn)我們可以認(rèn)為就是這幅圖的特征,即為特征點(diǎn)

如何確定左邊的是狼,右邊的是豬? 

獲取一幅圖中存在的一些獨(dú)特的像素點(diǎn),需要解決兩個問題:

  • 解決尺度不變性問題,不同大小的圖片獲取到的特征是一樣的
  • 提取到的特征點(diǎn)要穩(wěn)定,能被精確定位 

特征提取算法

名稱支持尺寸不變性速度
SURF支持
SIFT支持比SURF慢
ORB不支持SURF算法快10倍
FAST沒有尺度不變性比ORB快

透視變換

透視變換是按照物體成像投影規(guī)律進(jìn)行變換,即將物體重新投影到新的成像平面

透視變換常用于機(jī)器人視覺導(dǎo)航研究中,由于相機(jī)視場與地面存在傾斜角使得物體成像產(chǎn)生畸變,通常通過透視變換實(shí)現(xiàn)對物體圖像的校正

透視矩陣

[u,v,w] 表示當(dāng)前平面坐標(biāo)的x,y,z,如果是平面,那么z=1

[x',y',z'] 表示目標(biāo)平面坐標(biāo)的x,y,z,如果是平面,那么z=1

以上公式,我們可以理解為,透視矩陣是原始平面可目標(biāo)平面之間的一種轉(zhuǎn)換關(guān)系

圖像拷貝

將一副圖像拷貝到另一副圖像上的過程

二、案例實(shí)現(xiàn)

這是本案例所用到的素材,如下圖所示: 

我們將上圖進(jìn)行分割,用于實(shí)現(xiàn)拼接還原,如下圖所示: 

Step1:導(dǎo)入目標(biāo)圖片

設(shè)置需要處理的兩張圖片,進(jìn)行拼接準(zhǔn)備工作 

    Mat left=imread("C:/Users/86177/Desktop/image/a11.png");//左側(cè):圖片路徑
    Mat right=imread("C:/Users/86177/Desktop/image/a22.png");//右側(cè):圖片路徑
 
    imshow("left",left);
    imshow("right",right);

Step2:特征點(diǎn)提取和匹配 

用SIFT算法來實(shí)現(xiàn)圖像拼接是很常用的方法,雖說SURF精確度和穩(wěn)定性不及SIFT,但是其綜合能力還是優(yōu)越一些

    //創(chuàng)建SURF對象
    Ptr<SURF>surf;   //可以容納800個特征點(diǎn)
    surf = SURF::create(800);//參數(shù) 查找的海森矩陣 create 海森矩陣閥值
 
    //暴力匹配器
    BFMatcher matcher;
 
    vector<KeyPoint>key1,key2;
    Mat c,d;
 
    //尋找特征點(diǎn)
    surf->detectAndCompute(left,Mat(),key2,d);
    surf->detectAndCompute(right,Mat(),key1,c);
 
    //特征點(diǎn)對比,保存下來
    vector<DMatch>matches;//DMatch 點(diǎn)和點(diǎn)之間的關(guān)系
    //使用暴力匹配器匹配特征點(diǎn),找到存來
    matcher.match(d,c,matches);
 
    //排序 從小到大
    sort(matches.begin(),matches.end());
 
    //保留最優(yōu)的特征點(diǎn)對象
    vector<DMatch>good_matches;//最優(yōu)
 
    //設(shè)置比例
    int ptrPoint = std::min(50,(int)(matches.size()*0.15));
 
    for(int i = 0;i < ptrPoint;i++)
    {
        good_matches.push_back(matches[i]);
    }
 
    //最佳匹配的特征點(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);

Step3:圖像配準(zhǔn)

我們就可以得到了兩幅待拼接圖的匹配點(diǎn)集,接下來我們進(jìn)行圖像的配準(zhǔn),即將兩張圖像轉(zhuǎn)換為同一坐標(biāo)下

    //特征點(diǎn)配準(zhǔn)
    vector<Point2f>imagepoint1,imagepoint2;
 
    for(int i = 0;i<good_matches.size();i++)
    {
        imagepoint1.push_back(key1[good_matches[i].trainIdx].pt);
        imagepoint2.push_back(key2[good_matches[i].queryIdx].pt);
    }
 
    //透視轉(zhuǎn)換
    Mat homo = findHomography(imagepoint1,imagepoint2,CV_RANSAC);
 
    imshow("homo",homo);

Step4:圖像拷貝

將我們的左圖拷貝到設(shè)置好的配準(zhǔn)圖(右圖)上 

    //創(chuàng)建拼接后的圖,計(jì)算圖的大小
    int dst_width = imageTranForm.cols;//獲取最右點(diǎn)為拼接圖長度
    int dst_height = left.rows;
 
    Mat dst(dst_height,dst_width,CV_8UC3);
    dst.setTo(0);
 
    imageTranForm.copyTo(dst(Rect(0,0,imageTranForm.cols,imageTranForm.rows)));
    left.copyTo(dst(Rect(0,0,left.cols,left.rows)));
            
    imshow("dst",dst);

Step5:圖像融合

去裂縫處理,讓我們的優(yōu)化兩圖的連接處,使得拼接自然

PS:上面拼接完的圖片看不太出來,拼接處理中,還是建議用上

//優(yōu)化兩圖的連接處,使得拼接自然
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);
 
        }
    }
 
}

其他圖片拼接效果,如下圖所示:

完整代碼

#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
{
    //四個頂點(diǎn)
    Point2f left_top;
    Point2f left_bottom;
    Point2f right_top;
    Point2f right_bottom;
}four_corners_t;
 
four_corners_t corners;
 
//計(jì)算配準(zhǔn)圖的四個頂點(diǎn)坐標(biāo)
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];
 
}
 
//優(yōu)化兩圖的連接處,使得拼接自然
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(int argc, char *argv[])
{
    Mat left=imread("C:/Users/86177/Desktop/image/test(1).png");//左側(cè):圖片路徑
    Mat right=imread("C:/Users/86177/Desktop/image/test(2).png");//右側(cè):圖片路徑
 
    imshow("left",left);
    imshow("right",right);
 
    //創(chuàng)建SURF對象
    Ptr<SURF>surf;   //可以容納800個特征點(diǎn)
    surf = SURF::create(800);//參數(shù) 查找的海森矩陣 create 海森矩陣閥值
 
    //暴力匹配器
    BFMatcher matcher;
 
    vector<KeyPoint>key1,key2;
    Mat c,d;
 
    //尋找特征點(diǎn)
    surf->detectAndCompute(left,Mat(),key2,d);
    surf->detectAndCompute(right,Mat(),key1,c);
 
    //特征點(diǎn)對比,保存下來
    vector<DMatch>matches;//DMatch 點(diǎn)和點(diǎn)之間的關(guān)系
    //使用暴力匹配器匹配特征點(diǎn),找到存來
    matcher.match(d,c,matches);
 
    //排序 從小到大
    sort(matches.begin(),matches.end());
 
    //保留最優(yōu)的特征點(diǎn)對象
    vector<DMatch>good_matches;//最優(yōu)
 
    //設(shè)置比例
    int ptrPoint = std::min(50,(int)(matches.size()*0.15));
 
    for(int i = 0;i < ptrPoint;i++)
    {
        good_matches.push_back(matches[i]);
    }
 
    //最佳匹配的特征點(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);
 
    //特征點(diǎn)配準(zhǔn)
    vector<Point2f>imagepoint1,imagepoint2;
 
    for(int i = 0;i<good_matches.size();i++)
    {
        imagepoint1.push_back(key1[good_matches[i].trainIdx].pt);
        imagepoint2.push_back(key2[good_matches[i].queryIdx].pt);
    }
 
    //透視轉(zhuǎn)換
    Mat homo = findHomography(imagepoint1,imagepoint2,CV_RANSAC);
 
    imshow("homo",homo);
 
    //四個頂點(diǎn)坐標(biāo)的轉(zhuǎn)換計(jì)算
    CalcCorners(homo,right);
 
    Mat imageTranForm;
    warpPerspective(right,imageTranForm,homo,
                    Size(MAX(corners.right_top.x,
                             corners.right_bottom.x),
                         left.rows));
 
    imshow("imageTranForm",imageTranForm);
 
    //創(chuàng)建拼接后的圖,計(jì)算圖的大小
    int dst_width = imageTranForm.cols;//獲取最右點(diǎn)為拼接圖長度
    int dst_height = left.rows;
 
    Mat dst(dst_height,dst_width,CV_8UC3);
    dst.setTo(0);
 
    imageTranForm.copyTo(dst(Rect(0,0,imageTranForm.cols,imageTranForm.rows)));
    left.copyTo(dst(Rect(0,0,left.cols,left.rows)));
 
    //優(yōu)化拼接,主要目的去除黑邊
    OptimizeSeam(left,imageTranForm, dst);
 
    imshow("dst",dst);
 
    waitKey(0);
 
    return 0;
}

三、總結(jié)

本文的核心內(nèi)容包括:OpenCV圖像拼接相關(guān)原理以及OpenCV圖像拼接案例的實(shí)現(xiàn)

圖像拼接在我們?nèi)粘I钪羞\(yùn)用其實(shí)算是非常廣了,比如說我們現(xiàn)在經(jīng)常見到的無人機(jī)航拍,以及我們手機(jī)相機(jī)的全景拍攝

圖像拼接是我們對圖像進(jìn)行其他處理的基礎(chǔ)條件,圖像拼接的好壞,將會直接影響了咱們出圖的效果!所以學(xué)會拼接算法對圖像進(jìn)行拼接處理,很重要!

以上就是詳解C++ OpenCV實(shí)現(xiàn)圖像拼接的原理及方法的詳細(xì)內(nèi)容,更多關(guān)于C++ OpenCV圖像拼接的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論