1. 程式人生 > >opencv 開源影象拼接

opencv 開源影象拼接

網上搜的都是一行程式碼Stitcher::Status status = stitcher.stitch(imgs, pano);就出來的傻瓜拼接,連opencv基本的包都沒用。

自己好歹用了下基本的包實現了下。

魯棒性不太好,圖片少的時候沒事,圖片一多就出現了記憶體錯誤和木有特徵點的錯誤。

#include <iostream>
#include <fstream>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/stitcher.hpp"
#include "opencv2/stitching/warpers.hpp"

#define MaxPics 30
//F:\Program Files\opencv\modules\stitching\include\opencv2\stitching
using namespace std;
using namespace cv;
bool try_use_gpu = false;
int filenum;
vector<Mat> imgs;
string result_name = "result.jpg";
//void printUsage();
//int parseCmdArgs(int argc, char** argv);
int readDir(char *path,char fnames[MaxPics][100]);

int readDir(char *path,char fnames[MaxPics][100])
{
struct _finddata_t c_file;
intptr_t hFile;
filenum=0;

char fullpath[100];

sprintf(fullpath,"%s\\*.jpg",path);
// Find first .c file in current directory
if( (hFile = _findfirst( fullpath, &c_file )) == -1L )
printf( "No *.jpg files in current directory %s!\n",fullpath);
else
{
printf( "Listing of .jpg files in %s directory\n\n",path);
printf( "RDO HID SYS ARC FILE SIZE\n", ' ' );
printf( "--- --- --- --- ---- ----\n", ' ' );
do {
printf( ( c_file.attrib & _A_RDONLY ) ? " Y " : " N " );
printf( ( c_file.attrib & _A_SYSTEM ) ? " Y " : " N " );
printf( ( c_file.attrib & _A_HIDDEN ) ? " Y " : " N " );
printf( ( c_file.attrib & _A_ARCH ) ? " Y " : " N " );
//ctime_s( buffer, _countof(buffer), &c_file.time_write );
printf( " %-15s %9ld\n",c_file.name, c_file.size );
sprintf(fnames[filenum],"%s\\%s",path,c_file.name);
filenum++;
} while( _findnext( hFile, &c_file ) == 0 );
_findclose( hFile );
}
return filenum;
}
int main(int argc, char* argv[])
{
clock_t start,finish;
double totaltime;
start=clock();


argv[1]="pic";
char fdir[MaxPics][100];
filenum = readDir(argv[1],fdir);
Mat img, pano;
for(int i=0 ; i<filenum ;i++){
img = imread(fdir[i]);
imgs.push_back(img);
}
Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
stitcher.setRegistrationResol(0.1);//為了加速,我選0.1,預設是0.6,最大值1最慢,此方法用於特徵點檢測階段,如果找不到特徵點,調高吧
//stitcher.setSeamEstimationResol(0.1);//預設是0.1
//stitcher.setCompositingResol(-1);//預設是-1,用於特徵點檢測階段,找不到特徵點的話,改-1
stitcher.setPanoConfidenceThresh(1);//預設是1,見過有設0.6和0.4的
stitcher.setWaveCorrection(false);//預設是true,為加速選false,表示跳過WaveCorrection步驟
//stitcher.setWaveCorrectKind(detail::WAVE_CORRECT_HORIZ);//還可以選detail::WAVE_CORRECT_VERT ,波段修正(wave correction)功能(水平方向/垂直方向修正)。因為setWaveCorrection設的false,此語句沒用

//找特徵點surf演算法,此演算法計算量大,但對剛體運動、縮放、環境影響等情況下較為穩定
detail::SurfFeaturesFinder *featureFinder = new detail::SurfFeaturesFinder();
stitcher.setFeaturesFinder(featureFinder);

//找特徵點ORB演算法,但是發現草地這組圖,這個演算法不能完成拼接
//detail::OrbFeaturesFinder *featureFinder = new detail::OrbFeaturesFinder();
//stitcher.setFeaturesFinder(featureFinder);

//Features matcher which finds two best matches for each feature and leaves the best one only if the ratio between descriptor distances is greater than the threshold match_conf.
detail::BestOf2NearestMatcher *matcher = new detail::BestOf2NearestMatcher(false, 0.5f/*=match_conf預設是0.65,我選0.8,選太大了就沒特徵點啦,0.8都失敗了*/);
stitcher.setFeaturesMatcher(matcher);

// Rotation Estimation,It takes features of all images, pairwise matches between all images and estimates rotations of all cameras.
//Implementation of the camera parameters refinement algorithm which minimizes sum of the distances between the rays passing through the camera center and a feature,這個耗時短
stitcher.setBundleAdjuster(new detail::BundleAdjusterRay());
//Implementation of the camera parameters refinement algorithm which minimizes sum of the reprojection error squares.
//stitcher.setBundleAdjuster(new detail::BundleAdjusterReproj());

//Seam Estimation
//Minimum graph cut-based seam estimator
//stitcher.setSeamFinder(new detail::GraphCutSeamFinder(detail::GraphCutSeamFinderBase::COST_COLOR));//預設就是這個
//stitcher.setSeamFinder(new detail::GraphCutSeamFinder(detail::GraphCutSeamFinderBase::COST_COLOR_GRAD));//GraphCutSeamFinder的第二種形式
//啥SeamFinder也不用,Stub seam estimator which does nothing.
stitcher.setSeamFinder(new detail::NoSeamFinder);
//Voronoi diagram-based seam estimator.
//stitcher.setSeamFinder(new detail::VoronoiSeamFinder);

//exposure compensators曝光補償
//stitcher.setExposureCompensator(new detail::BlocksGainCompensator());//預設的就是這個
//不要曝光補償
stitcher.setExposureCompensator(new detail::NoExposureCompensator());
//Exposure compensator which tries to remove exposure related artifacts by adjusting image intensities
//stitcher.setExposureCompensator(new detail::detail::GainCompensator());
//Exposure compensator which tries to remove exposure related artifacts by adjusting image block intensities
//stitcher.setExposureCompensator(new detail::detail::BlocksGainCompensator());

//Image Blenders
//Blender which uses multi-band blending algorithm
//stitcher.setBlender(new detail::MultiBandBlender(try_use_gpu));//預設的是這個
//Simple blender which mixes images at its borders
stitcher.setBlender(new detail::FeatherBlender());//這個簡單,耗時少

//柱面?球面OR平面?預設為球面
PlaneWarper* cw = new PlaneWarper();
//SphericalWarper* cw = new SphericalWarper();
//CylindricalWarper* cw = new CylindricalWarper();
stitcher.setWarper(cw);

Stitcher::Status status = stitcher.estimateTransform(imgs);
if (status != Stitcher::OK)
{
cout << "Can't stitch images, error code = " << int(status) << endl;
return -1;
}
status = stitcher.composePanorama(pano);
if (status != Stitcher::OK)
{
cout << "Can't stitch images, error code = " << int(status) << endl;
return -1;
}
cout<<"程式開始";
imwrite(result_name, pano);
finish=clock();
totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<"\n此程式的執行時間為"<<totaltime<<"秒!"<<endl;
return 0;
}

1,Stitcher class from OpenCV library

  • Pros
    • Extremely simple. Build panorama in just one command.
    • It performs from finding features through blending in the selected projection by itself.
  • Cons
    • Uses SURF algorithm (patented, not allowed for commercial use).
    • Slow at searching for features, running on a smartphone

2,BoofCV

  • Pros
    • Still easy to use.
    • Java native.
  • Cons
    • It blends without warping into the spherical projection.
    • Still uses SURF.

當然自己實現最好,我的上述程式碼就是第一種了

相關推薦

opencv 開源影象拼接

網上搜的都是一行程式碼Stitcher::Status status = stitcher.stitch(imgs, pano);就出來的傻瓜拼接,連opencv基本的包都沒用。 自己好歹用了下基本的包實現了下。 魯棒性不太好,圖片少的時候沒事,圖片一多就出現了記憶體錯誤和木有特徵點的錯誤。 #includ

opencv 全景影象拼接

int imageStitcher() { vector<Mat> imgs; //圖片命名,可以不按順序來 string pattern = "F:/cpp_code/Demo20

Opencv 使用串聯匹配影象拼接

opencv自帶的stitching速度很慢, 其中一個最大的原因是每一張圖都要和其它的圖去匹配,如果有10張圖,除去自身不用匹配外,要匹配 10X(10-1) = 90 次。10張532*300圖拼接耗時14s左右,還姑且能忍受。可是100張圖要匹配9900次。耗時不是簡單的線性增長。 S

opencv學習筆記七十:影象拼接

簡要: 影象拼接在實際的應用場景很廣,舉一個例子,用你的手機對某一場景拍照,但是沒有辦法一次將所有你要拍的景物全部拍下來,所以你對該場景從左往右依次拍了好幾張圖,來把你要拍的所有景物記錄下來,影象拼接就是要將這些影象拼接成一個完整的大圖。 核心: 特徵點檢測 特徵點匹配

使用opencv,實現旋轉相機影象拼接

由於專案需要,其中一部分功能是需要實現360度旋轉相機的影象拼接顯示功能,於是使用opencv來將其實現。 由於旋轉相機,可以將其看成只有x和y方向上的平移,其中x方向重疊畫素比較多即平移比較多,y方向只有少量平移。   如上圖所示:待拼接的影象解析度為800*600;只

OpenCV簡單的拼接多幅影象

由於最近在學習超解析度演算法,但是大多數開源程式輸入圖片太大就顯示視訊記憶體不夠,因此就把手機拍攝的圖片先分割成一系列小的圖片,超解析度之後再憑藉在一塊 OpenCV的程式如下:比較簡單,對5x5即25張圖片進行拼接: #include <iostream>

影象拼接(二):OpenCV同時開啟兩個攝像頭捕獲視訊

使用OpenCV實現同時開啟兩個USB攝像頭,並實時顯示視訊。如果未檢測有兩個攝像頭,程式會結束併發出“攝像頭未安裝好”的警告。這裡推薦一個小巧的攝像頭視訊捕捉軟體:amcap,使用它可以方便的檢查每個攝像頭是否能正常工作。  捕獲視訊:  #include "opencv

使用OpenCV和Python拼接影象

寫在前面 首先這是一篇英文部落格的翻譯,先放上鍊接:https://www.pyimagesearch.com/2018/12/17/image-stitching-with-opencv-and-python/ 翻譯是靠谷歌翻譯和自己的理解,個別地方翻譯有點問題,請對照原文,大神可以直

python opencv 影象拼接

程式碼: import imutils import cv2 import numpy as np # stitch the images together to create a panorama def detectAndDescribe(image): # conve

OpenCV學習筆記(五十一)——imge stitching影象拼接stitching

stitching是OpenCV2.4.0一個新模組,功能是實現影象拼接,所有的相關函式都被封裝在Stitcher類當中。這個類當中我們可能用到的成員函式有createDefault、estimateTransform、composePanorama、stitch。其內部實

opencv 影象拼接

左右圖單應性變換原理圖 1.SURF特徵點提取 #include "pch.h" #include <opencv2\opencv.hpp> #include <opencv2\xfeatures2d.hpp> #include <iostream>

OpenCV探索之路(二十四)影象拼接影象融合技術

影象拼接在實際的應用場景很廣,比如無人機航拍,遙感影象等等,影象拼接是進一步做影象理解基礎步驟,拼接效果的好壞直接影響接下來的工作,所以一個好的影象拼接演算法非常重要。 再舉一個身邊的例子吧,你用你的手機對某一場景拍照,但是你沒有辦法一次將所有你要拍的景物全部拍下來,所以你

OpenCV影象拼接影象融合技術

影象拼接在實際的應用場景很廣,比如無人機航拍,遙感影象等等,影象拼接是進一步做影象理解基礎步驟,拼接效果的好壞直接影響接下來的工作,所以一個好的影象拼接演算法非常重要。 再舉一個身邊的例子吧,你用你的手機對某一場景拍照,但是你沒有辦法一次將所有你要拍的景物全部拍下來,

OpenCV影象處理入門學習教程三】基於SIFT特徵和SURF特徵的微旋轉影象拼接與融合生成全景影象的比較

安裝教程可以參考本人之前的一篇部落格:可以使OpenCV2和OpenCV3共存。那麼這裡為什麼又要提到OpenCV2和OpenCV3的區別了呢?其實本人也覺得挺奇葩的,因為從OpenCV3以來,一些比較新的功能都挪到了“opencv_contrib”庫裡,原因是他們覺得這些庫“不安全”,因此並沒有預設自帶這些

OpenCV】3.4.0影象拼接Stitching模組介紹

Images stitching 是opencv3.4.0中的模組之一,使用此模組可以實現對影象的拼接。在此之前需要編譯opencv3.4.0+contrib。具體編譯方法可以點此連結。也可以直接

C++\opencv 影象拼接演算法

Evelyn QQ:[email protected] 因為最近有需要,把影象拼接的siticher類裡面的具體函式實現看了一下,拼接最主要的部分是對影象進行變換, 也就是那個warp函式,影象拼接涉及到很多引數的設定,主要研究了一個整個warp過程 其實在進行

python+opencv實現影象全景拼接

一、演算法目的 隨便拍攝兩張圖片(圖一和圖二),兩圖之間有相同的拍攝區域,需要將兩幅影象無縫拼接在一起,完全接壤。 二、存在問題 由於鏡頭拍照的位置不同,會導致圖一和圖二雖然有相同的拍攝區域,但是並不能簡單的將圖二和圖一的重疊區域覆蓋進行拼接。因為拍攝圖一和

python+opencv實現影象全景拼接是遇到module 'cv2.cv2' has no attribute 'xfeatures2d_SIFT'問題

如果在安裝完成後發現還是不行的話你可以修改這句話:、 原來的是: sift = cv2.xfeatures2d.SIFT().create() 修改後的是: sift = cv2.xfea

OpenCV影象的淺拷貝與深拷貝 = copy clone區別

下面介紹三種OpenCV複製影象的方法: 方法1、過載運算子= 使用過載運算子“=”進行的拷貝是一種淺拷貝,雖然它們有不同的矩陣頭,但是二者共享相同的記憶體空間,二者內容相互關聯,任何一個變數變化的同時另一個變數也隨之改變。 /*OpenCV v1版本*/ IplImage im

OpenCV實現影象上新增漢字

OpenCV已經更新至3.0了,但自帶函式putText依然不支援影象上新增漢字,所以下面實現了影象中新增漢字功能,話不多說,程式碼奉上。 void GetStringSize(HDC hDC, const char* str, int* w, int* h) { SIZE size;