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

OpenCV圖像處理之圖像拼接詳解

 更新時(shí)間:2022年08月04日 08:41:27   作者:我今年十六歲  
本文主要介紹了如何使用C++?OpenCV實(shí)現(xiàn)圖像景拼接,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)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é)果 拼接 【這里只是將窗口移動(dòng)測試看下前面步驟是否正確】

可以看出左圖與右圖的透視轉(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)文章

最新評論

甘泉县| 会昌县| 潞西市| 桐庐县| 昭平县| 洪洞县| 灵武市| 和顺县| 郓城县| 镇江市| 云霄县| 洞口县| 丘北县| 二连浩特市| 当涂县| 太谷县| 远安县| 沾益县| 拜泉县| 大同市| 新和县| 隆昌县| 乌审旗| 定西市| 贡嘎县| 益阳市| 郎溪县| 平利县| 茂名市| 普陀区| 老河口市| 淄博市| 巴塘县| 秦皇岛市| 富平县| 蒙城县| 南城县| 宁乡县| 阳原县| 阿拉善盟| 赫章县|