資源簡介
利用雙目相機采集到的左右兩幅圖片,采用BM立體匹配算法生成視差圖和深度圖。其中包括雙目標定、雙目校正、立體匹配、計算深度等步驟。
外加鼠標回調函數,可以獲得視差圖中每一點的坐標信息(在相機坐標系下的坐標),其中z代表深度值。
代碼親自驗證有效,有詳細注釋,如有問題,隨時博客聯系我,我會一一解答。

代碼片段和文件信息
//程序功能:demoV1.1-立體匹配與測距輸出深度圖
//官方網址:萊娜網?www.FpgaLena.com
//程序版本:2017-03-V1.1
//需要修改的參數:程序中共計4個位置需要根據應用場景來修改參數(已在代碼旁做了備注)
#include???
#include???
using?namespace?std;
using?namespace?cv;
const?int?imageWidth?=?1280;??????????????????????//攝像頭單目的分辨率########--【需要調整參數的位置1】--#############
const?int?imageHeight?=?720;
Size?imageSize?=?Size(imageWidth?imageHeight);
Mat?rgbImageL?grayImageL;
Mat?rgbImageR?grayImageR;
Mat?rectifyImageL?rectifyImageR;
Mat?disp?disp8;
Mat?depthMap;
Rect?validROIL;???????????????????????????????????//圖像校正之后,會對圖像進行裁剪,這里的validROI就是指裁剪之后的區域??
Rect?validROIR;
Mat?mapLx?mapLy?mapRx?mapRy;???????????????????//映射表??
Mat?Rl?Rr?Pl?Pr?Q;????????????????????????????//校正旋轉矩陣R,投影矩陣P?重投影矩陣Q
Mat?xyz;??????????????????????????????????????????//三維坐標
Point?origin;?????????????????????????????????????//鼠標按下的起始點
Rect?selection;???????????????????????????????????//定義矩形選框
bool?selectobject?=?false;????????????????????????//是否選擇對象
int?blockSize?=?6?uniquenessRatio?=?30?numDisparities?=?9;?//與算法相關的參數,【需要調整參數的位置2】--############
Ptr?bm?=?StereoBM::create(16?9);
//########--【以下雙目的標定參數為:需要調整參數的位置3】--#############
//相機雙目標定的結果與如下各參數的對應關系見:雙目標定結果說明.pdf,pdf文檔位于main.cpp(即本文檔)同級文件夾--#############
/*左目相機標定參數------------------------
fc_left_x???0????????????cc_left_x
0???????????fc_left_y????cc_left_y
0???????????0????????????1
-----------------------------------------*/
Mat?cameraMatrixL?=?(Mat_(3?3)?<1450.45938?0?579.88716
0?1452.62035?376.32695
0?0?1);
Mat?distCoeffL?=?(Mat_(5?1)?<0.03569?0.29314?-0.00011?-0.00491?0.00000);
//[kc_left_01??kc_left_02??kc_left_03??kc_left_04???kc_left_05]
/*右目相機標定參數------------------------
fc_right_x???0??????????????cc_right_x
0????????????fc_right_y?????cc_right_y
0????????????0??????????????1
-----------------------------------------*/
Mat?cameraMatrixR?=?(Mat_(3?3)?<1451.78149?0?684.04159
0?1453.54807?350.52935
0?0?1);
Mat?distCoeffR?=?(Mat_(5?1)?<0.09596?-0.42760?-0.00378?-0.00112?0.00000);
//[kc_right_01??kc_right_02??kc_right_03??kc_right_04???kc_right_05]
Mat?T?=?(Mat_(3?1)?<-28.11909?0.11966?0.21590);????//T平移向量
//[T_01????????T_02???????T_03]
Mat?rec?=?(Mat_(3?1)?<-0.01688?-0.00781?-0.00766);???//rec旋轉向量
//[rec_01?????rec_02?????rec_03]
//########--雙目的標定參數填寫完畢----------------------------------------------
Mat?R;?????????????????????????????????????????????????????//R矩陣,用于中間計算
//--立體匹配--------------------------------------------------------------------
void?stereo_match(int?void*)
{
bm->setBlockSize(2?*?blockSize?+?5);?????????????//SAD窗口大小,5~21之間為宜
bm->setROI1(validROIL);
bm->setROI2(validROIR);
bm->setPreFilterCap(31);
bm->setMinDisparity(0);??????????????????????????//最小視差,默認值為0?可以是負值,int型
bm->setNumDisparities(numDisparities
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-08-07?13:45??雙目demo005\
?????目錄???????????0??2019-05-20?08:03??雙目demo005\x64\
?????目錄???????????0??2019-08-07?13:44??雙目demo005\x64\Debug\
?????文件??????141312??2019-08-07?13:44??雙目demo005\x64\Debug\雙目demo005.exe
?????文件??????637024??2019-08-07?13:44??雙目demo005\x64\Debug\雙目demo005.ilk
?????文件?????1346560??2019-08-07?13:44??雙目demo005\x64\Debug\雙目demo005.pdb
?????目錄???????????0??2019-05-21?08:11??雙目demo005\雙目demo005\
?????文件????14811136??2019-08-07?13:45??雙目demo005\雙目demo005.sdf
?????文件????????1339??2019-05-20?08:03??雙目demo005\雙目demo005.sln
?????文件???????20480??2019-08-07?13:45??雙目demo005\雙目demo005.v12.suo
?????文件???????10235??2019-05-21?08:11??雙目demo005\雙目demo005\main.cpp
?????文件?????2764854??2017-04-09?16:24??雙目demo005\雙目demo005\Test-02-left.bmp
?????文件?????2764854??2017-04-09?16:24??雙目demo005\雙目demo005\Test-02-right.bmp
?????目錄???????????0??2019-05-20?08:03??雙目demo005\雙目demo005\x64\
?????目錄???????????0??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\
?????文件??????518591??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\main.obj
?????文件??????494592??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\vc120.idb
?????文件??????962560??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\vc120.pdb
?????文件????????1111??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\雙目demo005.Build.CppClean.log
?????文件?????????740??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\雙目demo005.log
?????目錄???????????0??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\雙目demo005.tlog\
?????文件?????????556??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\雙目demo005.tlog\cl.command.1.tlog
?????文件???????14082??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\雙目demo005.tlog\CL.read.1.tlog
?????文件?????????500??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\雙目demo005.tlog\CL.write.1.tlog
?????文件????????3182??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\雙目demo005.tlog\li
?????文件????????9048??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\雙目demo005.tlog\li
?????文件?????????486??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\雙目demo005.tlog\li
?????文件?????????177??2019-08-07?13:44??雙目demo005\雙目demo005\x64\Debug\雙目demo005.tlog\雙目demo005.lastbuildstate
?????文件????????5892??2019-05-20?08:03??雙目demo005\雙目demo005\雙目demo005.vcxproj
?????文件?????????945??2019-05-20?08:03??雙目demo005\雙目demo005\雙目demo005.vcxproj.filters
- 上一篇:2019上網絡管理員真題及解析.zip
- 下一篇:欺騙的藝術高清版
評論
共有 條評論