最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

OpenCV 圖像拼接和圖像融合的實(shí)現(xiàn)

 更新時(shí)間:2021年08月18日 11:59:41   作者:Madcola  
本文主要介紹了OpenCV 圖像拼接和圖像融合,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

圖像拼接在實(shí)際的應(yīng)用場(chǎng)景很廣,比如無(wú)人機(jī)航拍,遙感圖像等等,圖像拼接是進(jìn)一步做圖像理解基礎(chǔ)步驟,拼接效果的好壞直接影響接下來(lái)的工作,所以一個(gè)好的圖像拼接算法非常重要。

再舉一個(gè)身邊的例子吧,你用你的手機(jī)對(duì)某一場(chǎng)景拍照,但是你沒(méi)有辦法一次將所有你要拍的景物全部拍下來(lái),所以你對(duì)該場(chǎng)景從左往右依次拍了好幾張圖,來(lái)把你要拍的所有景物記錄下來(lái)。那么我們能不能把這些圖像拼接成一個(gè)大圖呢?我們利用opencv就可以做到圖像拼接的效果!

比如我們有對(duì)這兩張圖進(jìn)行拼接。

從上面兩張圖可以看出,這兩張圖有比較多的重疊部分,這也是拼接的基本要求。

那么要實(shí)現(xiàn)圖像拼接需要那幾步呢?簡(jiǎn)單來(lái)說(shuō)有以下幾步:

  • 對(duì)每幅圖進(jìn)行特征點(diǎn)提取
  • 對(duì)對(duì)特征點(diǎn)進(jìn)行匹配
  • 進(jìn)行圖像配準(zhǔn)
  • 把圖像拷貝到另一幅圖像的特定位置
  • 對(duì)重疊邊界進(jìn)行特殊處理

好吧,那就開(kāi)始正式實(shí)現(xiàn)圖像配準(zhǔn)。

第一步就是特征點(diǎn)提取?,F(xiàn)在CV領(lǐng)域有很多特征點(diǎn)的定義,比如sift、surf、harris角點(diǎn)、ORB都是很有名的特征因子,都可以用來(lái)做圖像拼接的工作,他們各有優(yōu)勢(shì)。本文將使用ORB和SURF進(jìn)行圖像拼接,用其他方法進(jìn)行拼接也是類(lèi)似的。

基于SURF的圖像拼接

用SIFT算法來(lái)實(shí)現(xiàn)圖像拼接是很常用的方法,但是因?yàn)镾IFT計(jì)算量很大,所以在速度要求很高的場(chǎng)合下不再適用。所以,它的改進(jìn)方法SURF因?yàn)樵谒俣确矫嬗辛嗣黠@的提高(速度是SIFT的3倍),所以在圖像拼接領(lǐng)域還是大有作為。雖說(shuō)SURF精確度和穩(wěn)定性不及SIFT,但是其綜合能力還是優(yōu)越一些。下面將詳細(xì)介紹拼接的主要步驟。

1.特征點(diǎn)提取和匹配

特征點(diǎn)提取和匹配的方法我在上一篇文章《OpenCV特征檢測(cè)和特征匹配方法匯總》中做了詳細(xì)的介紹,在這里直接使用上文所總結(jié)的SURF特征提取和特征匹配的方法。

//提取特征點(diǎn)    
SurfFeatureDetector Detector(2000);  
vector<KeyPoint> keyPoint1, keyPoint2;
Detector.detect(image1, keyPoint1);
Detector.detect(image2, keyPoint2);

//特征點(diǎn)描述,為下邊的特征點(diǎn)匹配做準(zhǔn)備    
SurfDescriptorExtractor Descriptor;
Mat imageDesc1, imageDesc2;
Descriptor.compute(image1, keyPoint1, imageDesc1);
Descriptor.compute(image2, keyPoint2, imageDesc2);

FlannBasedMatcher matcher;
vector<vector<DMatch> > matchePoints;
vector<DMatch> GoodMatchePoints;

vector<Mat> train_desc(1, imageDesc1);
matcher.add(train_desc);
matcher.train();

matcher.knnMatch(imageDesc2, matchePoints, 2);
cout << "total match points: " << matchePoints.size() << endl;

// Lowe's algorithm,獲取優(yōu)秀匹配點(diǎn)
for (int i = 0; i < matchePoints.size(); i++)
{
    if (matchePoints[i][0].distance < 0.4 * matchePoints[i][1].distance)
    {
        GoodMatchePoints.push_back(matchePoints[i][0]);
    }
}

Mat first_match;
drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match);
imshow("first_match ", first_match);

2.圖像配準(zhǔn)

這樣子我們就可以得到了兩幅待拼接圖的匹配點(diǎn)集,接下來(lái)我們進(jìn)行圖像的配準(zhǔn),即將兩張圖像轉(zhuǎn)換為同一坐標(biāo)下,這里我們需要使用findHomography函數(shù)來(lái)求得變換矩陣。但是需要注意的是,findHomography函數(shù)所要用到的點(diǎn)集是Point2f類(lèi)型的,所有我們需要對(duì)我們剛得到的點(diǎn)集GoodMatchePoints再做一次處理,使其轉(zhuǎn)換為Point2f類(lèi)型的點(diǎn)集。

vector<Point2f> imagePoints1, imagePoints2;

for (int i = 0; i<GoodMatchePoints.size(); i++)
{
    imagePoints2.push_back(keyPoint2[GoodMatchePoints[i].queryIdx].pt);
    imagePoints1.push_back(keyPoint1[GoodMatchePoints[i].trainIdx].pt);
}

這樣子,我們就可以拿著imagePoints1, imagePoints2去求變換矩陣了,并且實(shí)現(xiàn)圖像配準(zhǔn)。值得注意的是findHomography函數(shù)的參數(shù)中我們選澤了CV_RANSAC,這表明我們選擇RANSAC算法繼續(xù)篩選可靠地匹配點(diǎn),這使得匹配點(diǎn)解更為精確。

//獲取圖像1到圖像2的投影映射矩陣 尺寸為3*3  
Mat homo = findHomography(imagePoints1, imagePoints2, CV_RANSAC);
////也可以使用getPerspectiveTransform方法獲得透視變換矩陣,不過(guò)要求只能有4個(gè)點(diǎn),效果稍差  
//Mat   homo=getPerspectiveTransform(imagePoints1,imagePoints2);  
cout << "變換矩陣為:\n" << homo << endl << endl; //輸出映射矩陣     

//圖像配準(zhǔn)  
Mat imageTransform1, imageTransform2;
warpPerspective(image01, imageTransform1, homo, Size(MAX(corners.right_top.x, corners.right_bottom.x), image02.rows));
//warpPerspective(image01, imageTransform2, adjustMat*homo, Size(image02.cols*1.3, image02.rows*1.8));
imshow("直接經(jīng)過(guò)透視矩陣變換", imageTransform1);
imwrite("trans1.jpg", imageTransform1);

3. 圖像拷貝

拷貝的思路很簡(jiǎn)單,就是將左圖直接拷貝到配準(zhǔn)圖上就可以了。

//創(chuàng)建拼接后的圖,需提前計(jì)算圖的大小
int dst_width = imageTransform1.cols;  //取最右點(diǎn)的長(zhǎng)度為拼接圖的長(zhǎng)度
int dst_height = image02.rows;

Mat dst(dst_height, dst_width, CV_8UC3);
dst.setTo(0);

imageTransform1.copyTo(dst(Rect(0, 0, imageTransform1.cols, imageTransform1.rows)));
image02.copyTo(dst(Rect(0, 0, image02.cols, image02.rows)));

imshow("b_dst", dst);

4.圖像融合(去裂縫處理)

從上圖可以看出,兩圖的拼接并不自然,原因就在于拼接圖的交界處,兩圖因?yàn)楣庹丈珴傻脑蚴沟脙蓤D交界處的過(guò)渡很糟糕,所以需要特定的處理解決這種不自然。這里的處理思路是加權(quán)融合,在重疊部分由前一幅圖像慢慢過(guò)渡到第二幅圖像,即將圖像的重疊區(qū)域的像素值按一定的權(quán)值相加合成新的圖像。

//優(yōu)化兩圖的連接處,使得拼接自然
void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst)
{
    int start = MIN(corners.left_top.x, corners.left_bottom.x);//開(kāi)始位置,即重疊區(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中無(wú)像素的黑點(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);

        }
    }

}

多嘗試幾張,驗(yàn)證拼接效果

測(cè)試一

測(cè)試二

測(cè)試三

最后給出完整的SURF算法實(shí)現(xiàn)的拼接代碼。

#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];//變換后的坐標(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];

}

int main(int argc, char *argv[])
{
    Mat image01 = imread("g5.jpg", 1);    //右圖
    Mat image02 = imread("g4.jpg", 1);    //左圖
    imshow("p2", image01);
    imshow("p1", image02);

    //灰度圖轉(zhuǎn)換  
    Mat image1, image2;
    cvtColor(image01, image1, CV_RGB2GRAY);
    cvtColor(image02, image2, CV_RGB2GRAY);


    //提取特征點(diǎn)    
    SurfFeatureDetector Detector(2000);  
    vector<KeyPoint> keyPoint1, keyPoint2;
    Detector.detect(image1, keyPoint1);
    Detector.detect(image2, keyPoint2);

    //特征點(diǎn)描述,為下邊的特征點(diǎn)匹配做準(zhǔn)備    
    SurfDescriptorExtractor Descriptor;
    Mat imageDesc1, imageDesc2;
    Descriptor.compute(image1, keyPoint1, imageDesc1);
    Descriptor.compute(image2, keyPoint2, imageDesc2);

    FlannBasedMatcher matcher;
    vector<vector<DMatch> > matchePoints;
    vector<DMatch> GoodMatchePoints;

    vector<Mat> train_desc(1, imageDesc1);
    matcher.add(train_desc);
    matcher.train();

    matcher.knnMatch(imageDesc2, matchePoints, 2);
    cout << "total match points: " << matchePoints.size() << endl;

    // Lowe's algorithm,獲取優(yōu)秀匹配點(diǎn)
    for (int i = 0; i < matchePoints.size(); i++)
    {
        if (matchePoints[i][0].distance < 0.4 * matchePoints[i][1].distance)
        {
            GoodMatchePoints.push_back(matchePoints[i][0]);
        }
    }

    Mat first_match;
    drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match);
    imshow("first_match ", first_match);

    vector<Point2f> imagePoints1, imagePoints2;

    for (int i = 0; i<GoodMatchePoints.size(); i++)
    {
        imagePoints2.push_back(keyPoint2[GoodMatchePoints[i].queryIdx].pt);
        imagePoints1.push_back(keyPoint1[GoodMatchePoints[i].trainIdx].pt);
    }



    //獲取圖像1到圖像2的投影映射矩陣 尺寸為3*3  
    Mat homo = findHomography(imagePoints1, imagePoints2, CV_RANSAC);
    ////也可以使用getPerspectiveTransform方法獲得透視變換矩陣,不過(guò)要求只能有4個(gè)點(diǎn),效果稍差  
    //Mat   homo=getPerspectiveTransform(imagePoints1,imagePoints2);  
    cout << "變換矩陣為:\n" << homo << endl << endl; //輸出映射矩陣      

   //計(jì)算配準(zhǔn)圖的四個(gè)頂點(diǎn)坐標(biāo)
    CalcCorners(homo, image01);
    cout << "left_top:" << corners.left_top << endl;
    cout << "left_bottom:" << corners.left_bottom << endl;
    cout << "right_top:" << corners.right_top << endl;
    cout << "right_bottom:" << corners.right_bottom << endl;

    //圖像配準(zhǔn)  
    Mat imageTransform1, imageTransform2;
    warpPerspective(image01, imageTransform1, homo, Size(MAX(corners.right_top.x, corners.right_bottom.x), image02.rows));
    //warpPerspective(image01, imageTransform2, adjustMat*homo, Size(image02.cols*1.3, image02.rows*1.8));
    imshow("直接經(jīng)過(guò)透視矩陣變換", imageTransform1);
    imwrite("trans1.jpg", imageTransform1);


    //創(chuàng)建拼接后的圖,需提前計(jì)算圖的大小
    int dst_width = imageTransform1.cols;  //取最右點(diǎn)的長(zhǎng)度為拼接圖的長(zhǎng)度
    int dst_height = image02.rows;

    Mat dst(dst_height, dst_width, CV_8UC3);
    dst.setTo(0);

    imageTransform1.copyTo(dst(Rect(0, 0, imageTransform1.cols, imageTransform1.rows)));
    image02.copyTo(dst(Rect(0, 0, image02.cols, image02.rows)));

    imshow("b_dst", dst);


    OptimizeSeam(image02, imageTransform1, dst);


    imshow("dst", dst);
    imwrite("dst.jpg", dst);

    waitKey();

    return 0;
}


//優(yōu)化兩圖的連接處,使得拼接自然
void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst)
{
    int start = MIN(corners.left_top.x, corners.left_bottom.x);//開(kāi)始位置,即重疊區(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中無(wú)像素的黑點(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);

        }
    }

}

基于ORB的圖像拼接

利用ORB進(jìn)行圖像拼接的思路跟上面的思路基本一樣,只是特征提取和特征點(diǎn)匹配的方式略有差異罷了。這里就不再詳細(xì)介紹思路了,直接貼代碼看效果。

#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];//變換后的坐標(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];

}

int main(int argc, char *argv[])
{
    Mat image01 = imread("t1.jpg", 1);    //右圖
    Mat image02 = imread("t2.jpg", 1);    //左圖
    imshow("p2", image01);
    imshow("p1", image02);

    //灰度圖轉(zhuǎn)換  
    Mat image1, image2;
    cvtColor(image01, image1, CV_RGB2GRAY);
    cvtColor(image02, image2, CV_RGB2GRAY);


    //提取特征點(diǎn)    
    OrbFeatureDetector  surfDetector(3000);  
    vector<KeyPoint> keyPoint1, keyPoint2;
    surfDetector.detect(image1, keyPoint1);
    surfDetector.detect(image2, keyPoint2);

    //特征點(diǎn)描述,為下邊的特征點(diǎn)匹配做準(zhǔn)備    
    OrbDescriptorExtractor  SurfDescriptor;
    Mat imageDesc1, imageDesc2;
    SurfDescriptor.compute(image1, keyPoint1, imageDesc1);
    SurfDescriptor.compute(image2, keyPoint2, imageDesc2);

    flann::Index flannIndex(imageDesc1, flann::LshIndexParams(12, 20, 2), cvflann::FLANN_DIST_HAMMING);

    vector<DMatch> GoodMatchePoints;

    Mat macthIndex(imageDesc2.rows, 2, CV_32SC1), matchDistance(imageDesc2.rows, 2, CV_32FC1);
    flannIndex.knnSearch(imageDesc2, macthIndex, matchDistance, 2, flann::SearchParams());

    // Lowe's algorithm,獲取優(yōu)秀匹配點(diǎn)
    for (int i = 0; i < matchDistance.rows; i++)
    {
        if (matchDistance.at<float>(i, 0) < 0.4 * matchDistance.at<float>(i, 1))
        {
            DMatch dmatches(i, macthIndex.at<int>(i, 0), matchDistance.at<float>(i, 0));
            GoodMatchePoints.push_back(dmatches);
        }
    }

    Mat first_match;
    drawMatches(image02, keyPoint2, image01, keyPoint1, GoodMatchePoints, first_match);
    imshow("first_match ", first_match);

    vector<Point2f> imagePoints1, imagePoints2;

    for (int i = 0; i<GoodMatchePoints.size(); i++)
    {
        imagePoints2.push_back(keyPoint2[GoodMatchePoints[i].queryIdx].pt);
        imagePoints1.push_back(keyPoint1[GoodMatchePoints[i].trainIdx].pt);
    }



    //獲取圖像1到圖像2的投影映射矩陣 尺寸為3*3  
    Mat homo = findHomography(imagePoints1, imagePoints2, CV_RANSAC);
    ////也可以使用getPerspectiveTransform方法獲得透視變換矩陣,不過(guò)要求只能有4個(gè)點(diǎn),效果稍差  
    //Mat   homo=getPerspectiveTransform(imagePoints1,imagePoints2);  
    cout << "變換矩陣為:\n" << homo << endl << endl; //輸出映射矩陣      

                                                //計(jì)算配準(zhǔn)圖的四個(gè)頂點(diǎn)坐標(biāo)
    CalcCorners(homo, image01);
    cout << "left_top:" << corners.left_top << endl;
    cout << "left_bottom:" << corners.left_bottom << endl;
    cout << "right_top:" << corners.right_top << endl;
    cout << "right_bottom:" << corners.right_bottom << endl;

    //圖像配準(zhǔn)  
    Mat imageTransform1, imageTransform2;
    warpPerspective(image01, imageTransform1, homo, Size(MAX(corners.right_top.x, corners.right_bottom.x), image02.rows));
    //warpPerspective(image01, imageTransform2, adjustMat*homo, Size(image02.cols*1.3, image02.rows*1.8));
    imshow("直接經(jīng)過(guò)透視矩陣變換", imageTransform1);
    imwrite("trans1.jpg", imageTransform1);


    //創(chuàng)建拼接后的圖,需提前計(jì)算圖的大小
    int dst_width = imageTransform1.cols;  //取最右點(diǎn)的長(zhǎng)度為拼接圖的長(zhǎng)度
    int dst_height = image02.rows;

    Mat dst(dst_height, dst_width, CV_8UC3);
    dst.setTo(0);

    imageTransform1.copyTo(dst(Rect(0, 0, imageTransform1.cols, imageTransform1.rows)));
    image02.copyTo(dst(Rect(0, 0, image02.cols, image02.rows)));

    imshow("b_dst", dst);


    OptimizeSeam(image02, imageTransform1, dst);


    imshow("dst", dst);
    imwrite("dst.jpg", dst);

    waitKey();

    return 0;
}


//優(yōu)化兩圖的連接處,使得拼接自然
void OptimizeSeam(Mat& img1, Mat& trans, Mat& dst)
{
    int start = MIN(corners.left_top.x, corners.left_bottom.x);//開(kāi)始位置,即重疊區(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中無(wú)像素的黑點(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);

        }
    }
}

看一看拼接效果,我覺(jué)得還是不錯(cuò)的。

看一下這一組圖片,這組圖片產(chǎn)生了鬼影,為什么?因?yàn)閮煞鶊D中的人物走動(dòng)了??!所以要做圖像拼接,盡量保證使用的是靜態(tài)圖片,不要加入一些動(dòng)態(tài)因素干擾拼接。

opencv自帶的拼接算法stitch

opencv其實(shí)自己就有實(shí)現(xiàn)圖像拼接的算法,當(dāng)然效果也是相當(dāng)好的,但是因?yàn)槠鋵?shí)現(xiàn)很復(fù)雜,而且代碼量很龐大,其實(shí)在一些小應(yīng)用下的拼接有點(diǎn)殺雞用牛刀的感覺(jué)。最近在閱讀sticth源碼時(shí),發(fā)現(xiàn)其中有幾個(gè)很有意思的地方。

1.opencv stitch選擇的特征檢測(cè)方式

一直很好奇opencv stitch算法到底選用了哪個(gè)算法作為其特征檢測(cè)方式,是ORB,SIFT還是SURF?讀源碼終于看到答案。

#ifdef HAVE_OPENCV_NONFREE
        stitcher.setFeaturesFinder(new detail::SurfFeaturesFinder());
#else
        stitcher.setFeaturesFinder(new detail::OrbFeaturesFinder());
#endif

在源碼createDefault函數(shù)中(默認(rèn)設(shè)置),第一選擇是SURF,第二選擇才是ORB(沒(méi)有NONFREE模塊才選),所以既然大牛們這么選擇,必然是經(jīng)過(guò)綜合考慮的,所以應(yīng)該SURF算法在圖像拼接有著更優(yōu)秀的效果。

2.opencv stitch獲取匹配點(diǎn)的方式

以下代碼是opencv stitch源碼中的特征點(diǎn)提取部分,作者使用了兩次特征點(diǎn)提取的思路:先對(duì)圖一進(jìn)行特征點(diǎn)提取和篩選匹配(1->2),再對(duì)圖二進(jìn)行特征點(diǎn)的提取和匹配(2->1),這跟我們平時(shí)的一次提取的思路不同,這種二次提取的思路可以保證更多的匹配點(diǎn)被選中,匹配點(diǎn)越多,findHomography求出的變換越準(zhǔn)確。這個(gè)思路值得借鑒。

matches_info.matches.clear();

Ptr<flann::IndexParams> indexParams = new flann::KDTreeIndexParams();
Ptr<flann::SearchParams> searchParams = new flann::SearchParams();

if (features2.descriptors.depth() == CV_8U)
{
    indexParams->setAlgorithm(cvflann::FLANN_INDEX_LSH);
    searchParams->setAlgorithm(cvflann::FLANN_INDEX_LSH);
}

FlannBasedMatcher matcher(indexParams, searchParams);
vector< vector<DMatch> > pair_matches;
MatchesSet matches;

// Find 1->2 matches
matcher.knnMatch(features1.descriptors, features2.descriptors, pair_matches, 2);
for (size_t i = 0; i < pair_matches.size(); ++i)
{
    if (pair_matches[i].size() < 2)
        continue;
    const DMatch& m0 = pair_matches[i][0];
    const DMatch& m1 = pair_matches[i][1];
    if (m0.distance < (1.f - match_conf_) * m1.distance)
    {
        matches_info.matches.push_back(m0);
        matches.insert(make_pair(m0.queryIdx, m0.trainIdx));
    }
}
LOG("\n1->2 matches: " << matches_info.matches.size() << endl);

// Find 2->1 matches
pair_matches.clear();
matcher.knnMatch(features2.descriptors, features1.descriptors, pair_matches, 2);
for (size_t i = 0; i < pair_matches.size(); ++i)
{
    if (pair_matches[i].size() < 2)
        continue;
    const DMatch& m0 = pair_matches[i][0];
    const DMatch& m1 = pair_matches[i][1];
    if (m0.distance < (1.f - match_conf_) * m1.distance)
        if (matches.find(make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
            matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
}
LOG("1->2 & 2->1 matches: " << matches_info.matches.size() << endl);

這里我仿照opencv源碼二次提取特征點(diǎn)的思路對(duì)我原有拼接代碼進(jìn)行改寫(xiě),實(shí)驗(yàn)證明獲取的匹配點(diǎn)確實(shí)較一次提取要多。

//提取特征點(diǎn)    
SiftFeatureDetector Detector(1000);  // 海塞矩陣閾值,在這里調(diào)整精度,值越大點(diǎn)越少,越精準(zhǔn) 
vector<KeyPoint> keyPoint1, keyPoint2;
Detector.detect(image1, keyPoint1);
Detector.detect(image2, keyPoint2);

//特征點(diǎn)描述,為下邊的特征點(diǎn)匹配做準(zhǔn)備    
SiftDescriptorExtractor Descriptor;
Mat imageDesc1, imageDesc2;
Descriptor.compute(image1, keyPoint1, imageDesc1);
Descriptor.compute(image2, keyPoint2, imageDesc2);

FlannBasedMatcher matcher;
vector<vector<DMatch> > matchePoints;
vector<DMatch> GoodMatchePoints;

MatchesSet matches;

vector<Mat> train_desc(1, imageDesc1);
matcher.add(train_desc);
matcher.train();

matcher.knnMatch(imageDesc2, matchePoints, 2);

// Lowe's algorithm,獲取優(yōu)秀匹配點(diǎn)
for (int i = 0; i < matchePoints.size(); i++)
{
    if (matchePoints[i][0].distance < 0.4 * matchePoints[i][1].distance)
    {
        GoodMatchePoints.push_back(matchePoints[i][0]);
        matches.insert(make_pair(matchePoints[i][0].queryIdx, matchePoints[i][0].trainIdx));
    }
}
cout<<"\n1->2 matches: " << GoodMatchePoints.size() << endl;

#if 1

FlannBasedMatcher matcher2;
matchePoints.clear();
vector<Mat> train_desc2(1, imageDesc2);
matcher2.add(train_desc2);
matcher2.train();

matcher2.knnMatch(imageDesc1, matchePoints, 2);
// Lowe's algorithm,獲取優(yōu)秀匹配點(diǎn)
for (int i = 0; i < matchePoints.size(); i++)
{
    if (matchePoints[i][0].distance < 0.4 * matchePoints[i][1].distance)
    {
        if (matches.find(make_pair(matchePoints[i][0].trainIdx, matchePoints[i][0].queryIdx)) == matches.end())
        {
            GoodMatchePoints.push_back(DMatch(matchePoints[i][0].trainIdx, matchePoints[i][0].queryIdx, matchePoints[i][0].distance));
        }
        
    }
}
cout<<"1->2 & 2->1 matches: " << GoodMatchePoints.size() << endl;
#endif

最后再看一下opencv stitch的拼接效果吧~速度雖然比較慢,但是效果還是很好的。

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/stitching/stitcher.hpp>
using namespace std;
using namespace cv;
bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "dst1.jpg";
int main(int argc, char * argv[])
{
    Mat img1 = imread("34.jpg");
    Mat img2 = imread("35.jpg");

    imshow("p1", img1);
    imshow("p2", img2);

    if (img1.empty() || img2.empty())
    {
        cout << "Can't read image" << endl;
        return -1;
    }
    imgs.push_back(img1);
    imgs.push_back(img2);


    Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
    // 使用stitch函數(shù)進(jìn)行拼接
    Mat pano;
    Stitcher::Status status = stitcher.stitch(imgs, pano);
    if (status != Stitcher::OK)
    {
        cout << "Can't stitch images, error code = " << int(status) << endl;
        return -1;
    }
    imwrite(result_name, pano);
    Mat pano2 = pano.clone();
    // 顯示源圖像,和結(jié)果圖像
    imshow("全景圖像", pano);
    if (waitKey() == 27)
        return 0;
}

到此這篇關(guān)于OpenCV 圖像拼接和圖像融合的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)OpenCV 圖像拼接和圖像融合內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • c++冒泡排序詳解

    c++冒泡排序詳解

    冒泡排序(Bubble Sort),是一種計(jì)算機(jī)科學(xué)領(lǐng)域的較簡(jiǎn)單的排序算法。它重復(fù)地走訪過(guò)要排序的數(shù)列,一次比較兩個(gè)元素,如果他們的順序錯(cuò)誤就把他們交換過(guò)來(lái)。走訪數(shù)列的工作是重復(fù)地進(jìn)行直到?jīng)]有再需要交換,也就是說(shuō)該數(shù)列已經(jīng)排序完成。
    2017-05-05
  • 基于C++字符串替換函數(shù)的使用詳解

    基于C++字符串替換函數(shù)的使用詳解

    本篇文章是對(duì)C++字符串替換函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 基于C語(yǔ)言編寫(xiě)簡(jiǎn)易的英文統(tǒng)計(jì)和加密系統(tǒng)

    基于C語(yǔ)言編寫(xiě)簡(jiǎn)易的英文統(tǒng)計(jì)和加密系統(tǒng)

    這篇文章主要介紹如何基于C語(yǔ)言編寫(xiě)一個(gè)簡(jiǎn)易的英文統(tǒng)計(jì)和加密系統(tǒng),實(shí)際上就是對(duì)字符數(shù)組的基本操作的各種使用,感興趣的可以了解一下
    2023-05-05
  • C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易的三子棋游戲

    C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易的三子棋游戲

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易的三子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • C語(yǔ)言中access/_access函數(shù)的使用實(shí)例詳解

    C語(yǔ)言中access/_access函數(shù)的使用實(shí)例詳解

    本文通過(guò)實(shí)例代碼給大家介紹了C語(yǔ)言中access/_access函數(shù)的使用,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • MFC繪制不規(guī)則窗體的方法

    MFC繪制不規(guī)則窗體的方法

    這篇文章主要介紹了MFC繪制不規(guī)則窗體的方法,涉及MFC窗體操作的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • C++ Boost Optional示例超詳細(xì)講解

    C++ Boost Optional示例超詳細(xì)講解

    Boost是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱。Boost庫(kù)是一個(gè)可移植、提供源代碼的C++庫(kù),作為標(biāo)準(zhǔn)庫(kù)的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開(kāi)發(fā)引擎之一,是為C++語(yǔ)言標(biāo)準(zhǔn)庫(kù)提供擴(kuò)展的一些C++程序庫(kù)的總稱
    2022-11-11
  • C++獲取本機(jī)登陸過(guò)的QQ號(hào)碼示例程序

    C++獲取本機(jī)登陸過(guò)的QQ號(hào)碼示例程序

    這篇文章主要介紹了使用C++獲取本機(jī)登陸過(guò)的QQ號(hào)碼列表的程序示例,大家可以參考使用
    2013-11-11
  • C++友元函數(shù)與拷貝構(gòu)造函數(shù)詳解

    C++友元函數(shù)與拷貝構(gòu)造函數(shù)詳解

    這篇文章主要介紹了C++友元函數(shù)與拷貝構(gòu)造函數(shù),需要的朋友可以參考下
    2014-07-07
  • 利用C語(yǔ)言實(shí)現(xiàn)2048小游戲的方法

    利用C語(yǔ)言實(shí)現(xiàn)2048小游戲的方法

    2048是比較流行的一款數(shù)字游戲,相信對(duì)大家來(lái)說(shuō)都不陌生,這篇文章給大家分享了利用C語(yǔ)言實(shí)現(xiàn)2048小游戲的方法,對(duì)大家學(xué)習(xí)理解C語(yǔ)言具有一定的參考借鑒價(jià)值,有需要的朋友們下面來(lái)一起看看吧。
    2016-10-10

最新評(píng)論

比如县| 施秉县| 马边| 永嘉县| 晋江市| 得荣县| 民勤县| 天峨县| 吴忠市| 亚东县| 清水河县| 湛江市| 广水市| 金山区| 宁都县| 高平市| 澄迈县| 宜黄县| 夏邑县| 瑞昌市| 晋中市| 宜丰县| 团风县| 柏乡县| 余干县| 谷城县| 贡山| 临泉县| 灌阳县| 沈丘县| 监利县| 开鲁县| 抚顺县| 陆河县| 新蔡县| 甘肃省| 平阳县| 奈曼旗| 隆化县| 万州区| 广丰县|