攝像機定標opencv程序_第1頁
攝像機定標opencv程序_第2頁
攝像機定標opencv程序_第3頁
攝像機定標opencv程序_第4頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、#include cvut.h#include #include #include using namespace cvut;using namespace std;#pragma comment(lib,cxcore.lib)#pragma comment(lib,cv.lib)#pragma comment(lib,highgui.lib)void main() ifstream fin(calibdata.txt); /*定標所用圖像文件的路徑*/ofstream fout(caliberation_result.txt); /* 保存定標結果的文件 */讀取每一幅圖像,從中提取出角點,

2、然后對角點進行亞像素精確化cout開始提取角點;int image_count=0; /* 圖像數量 */CvSize image_size; /* 圖像的尺寸 */CvSize board_size = cvSize(5,7); /* 定標板上每行、列的角點數 */CvPoint2D32f*image_points_buf=newCvPoint2D32fboard_size.width*board_size.height; /* 緩存每幅圖像上檢測到的角點 */Seq image_points_seq; /* 保存檢測到的所有角點 */ string filename; while (get

3、line(fin,filename) image_count+; int count;Image view(filename); if (image_count = 1) image_size.width = view.size().width; image_size.height = view.size().height;/*提取角點*/if (0 = cvFindChessboardCorners( view.cvimage, board_size, image_points_buf, &count, CV_CALIB_CB_ADAPTIVE_THRESH ) coutcan not fi

4、nd chessboard corners!n;exit(1); else Image view_gray(view.size(),8,1); rgb2gray(view,view_gray);/*亞像素精確化*/ cvFindCornerSubPix( view_gray.cvimage, image_points_buf, count, cvSize(11,11), cvSize(-1,-1), cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ); image_points_seq.push_back(image_poin

5、ts_buf,count);/*在圖像上顯示角點位置*/ cvDrawChessboardCorners( view.cvimage, board_size, image_points_buf, count, 1);view.show(calib); cvWaitKey(); view.close(); delete image_points_buf; cout角點提取完成! n;攝像機定標cout開始定標;CvSize square_size = cvSize(10,10); /*實際測量得到的定標板上每個棋盤格的大小*/Matrix object_points(1,board_size.w

6、idth*board_size.height*image_count,3); /*保存定標板上角點的三維坐標*/Matrix image_points(1,image_points_seq.cvseq-total,2); /*保存提取的所有 角點 */Matrix point_counts(1,image_count,1); /* 每幅圖像中角點的數量 */Matrix intrinsic_matrix(3,3,1); /* 攝像機內參數矩陣 */Matrix distortion_coeffs(1,4,1); /* 攝像機的 4 個畸變系數:k1,k2,p1,p2 */Matrix rota

7、tion_vectors(1,image_count,3); /* 每幅圖像的旋轉向量 */Matrix translation_vectors(1,image_count,3); /* 每幅圖像的平移向量 */*初始化定標板上角點的三維坐標*/int i,j,t;for (t=0;timage_count;t+) for (i=0;iboard_size.height;i+) for (j=0;jboard_size.width;j+) /*假設定標板放在世界坐標系中z=0的平面上*/object_points(0,t*board_size.height*board_size.width+i

8、*board_size.width+j,0) i*square_size.width;object_points(0,t*board_size.height*board_size.width+i*board_size.width+j,1) j*square_size.height;object_points(0,t*board_size.height*board_size.width+i*board_size.width+j,2) 0;/*將角點的存儲結構轉換成矩陣形式*/for (i=0;itotal;i+) image_points(0,i,0) = image_points_seqi.x

9、;image_points(0,i,1) = image_points_seqi.y;/*初始化每幅圖像中的角點數量,這里我們假設每幅圖像中都可以看到完整的定標板*/for (i=0;iimage_count;i+)point_counts(0,i) = board_size.width*board_size.height;/*開始定標*/cvCalibrateCamera2(object_points.cvmat,image_points.cvmat,point_counts.cvmat,image_size,intrinsic_matrix.cvmat,distortion_coeffs.

10、cvmat,rotation_vectors.cvmat,translation_vectors.cvmat,0);cout定標完成! n;對定標結果進行評價cout開始評價定標結果n;double total_err = 0.0; /*所有圖像的平均誤差的總和*/double err = 0.0; /*每幅圖像的平均誤差*/Matrix image_points2(1,point_counts(0,0,0),2); /* 保存重新計算得到的投影點 */coutt每幅圖像的定標誤差:n;fout每幅圖像的定標誤差:n;for (i=0;iimage_count;i+) /*通過得到的攝像機內外

11、參數,對空間的三維點進行重新投影計算,得到新的投影點*/cvProjectPoints2(object_points.get_cols(i*point_counts(0,0,0),(i+1)*point_counts(0,0,0)-1).cvmat,rotation_vectors.get_col(i).cvmat,translation_vectors.get_col(i).cvmat, intrinsic_matrix.cvmat, distortion_coeffs.cvmat, image_points2.cvmat, 0,0,0,0);/*計算新的投影點和舊的投影點之間的誤差*/er

12、rcvNorm(image_points.get_cols(i*point_counts(0,0,0),(i+1)*point_counts(0,0,0)-1).cvm at,image_points2.cvmat, CV_L1);total_err += err/=point_counts(0,0,0);couttt 第i+1幅圖像的平均誤差:err像素n;foutt 第i+1幅圖像的平均誤差:err像素n;coutt 總體平均誤差:total_err/image_count像素n;fout總體平均誤差:total_err/image_count像素nn;cout評價完成! n;保存定標結果

13、cout開始保存定標結果;Matrix rotation_vector(3,1); /* 保存每幅圖像的旋轉向量 */Matrix rotation_matrix(3,3); /* 保存每幅圖像的旋轉矩陣 */fout相機內參數矩陣:n;foutintrinsic_matrixn;fout畸變系數:n;foutdistortion_coeffsn;for (i=0;iimage_count;i+) fout”第i+1幅圖像的旋轉向量:n;foutrotation_vectors.get_col(i);/*對旋轉向量進行存儲格式轉換*/for (j=0;j3;j+) rotation_vector(j,0,0) = rota

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論