基於EmguCV的攝像機標定及矯正


標簽: EmguCV攝像頭標定C#
 分類:
C#
 

目錄(?)[+]

 

前言

之前用OpenCV做過攝像機標定及矯正,現在平台換了,改用C#,就用EmguCV做一下,其實就是OpenCV的C#版。

在EmguCV中有兩類攝像機標定的類,一個是CvInvoke類,一個是CameraCalibration類,兩種標定效果差不多,只不過CvInvoke涉及的函數大多都是指針類型的,而C#對於指針的操作比較麻煩。本文是在CameraCalibration類的基礎上完成攝像機標定,用CvInvoke類完成矯正圖像。

函數說明

1、角點檢測

 

[csharp] view plain copy print ?
  1. public static PointF[] FindChessboardCorners(  
  2.     Image<Gray, byte> image,  
  3.     Size patternSize,  
  4.     CALIB_CB_TYPE flags  
  5. )  
 
         
 
         

Parameters

image
Type:  Emgu.CV.Image<Gray, Byte>Source chessboard view
patternSize
Type:  System.Drawing.SizeThe number of inner corners per chessboard row and column
flags
Type:  Emgu.CV.CvEnum.CALIB_CB_TYPEVarious operation flags

Return Value

Type:  PointF[]The corners detected if the chess board pattern is found, otherwise null is returned

 

注:輸入圖像需是灰度圖,檢測之前角點需要開辟空間,如:

 

[csharp]  view plain  copy print ?
  1. cornersDetected = new PointF[nPoints];       //nPoints表示單幅圖像角點總數   
  2. cornersDetected = CameraCalibration.FindChessboardCorners(chessboardImage, patternSize,  
  3.                 CALIB_CB_TYPE.ADAPTIVE_THRESH | CALIB_CB_TYPE.NORMALIZE_IMAGE);  

 

 

2、標定函數

[csharp] view plain copy print ?
  1. public static double CalibrateCamera(  
  2.     MCvPoint3D32f[][] objectPoints,  
  3.     PointF[][] imagePoints,  
  4.     Size imageSize,  
  5.     IntrinsicCameraParameters intrinsicParam,  
  6.     CALIB_TYPE calibrationType,  
  7.     MCvTermCriteria termCriteria,  
  8.     out ExtrinsicCameraParameters[] extrinsicParams  
  9. )  




 
         
 
         

Parameters

objectPoints
Type:  Emgu.CV.Structure.MCvPoint3D32f[][]The 3D location of the object points. The first index is the index of image, second index is the index of the point
imagePoints
Type:  System.Drawing..::..PointF[][]The 2D image location of the points. The first index is the index of the image, second index is the index of the point
imageSize
Type:  System.Drawing.SizeThe size of the image, used only to initialize intrinsic camera matrix
intrinsicParam
Type:  Emgu.CV.IntrinsicCameraParametersThe intrisinc parameters, might contains some initial values. The values will be modified by this function.
calibrationType
Type:  Emgu.CV.CvEnum.CALIB_TYPEcCalibration type
termCriteria
Type:  Emgu.CV.Structure.MCvTermCriteriaThe termination criteria
extrinsicParams
Type:  Emgu.CV.ExtrinsicCameraParameters[]The output array of extrinsic parameters.

Return Value

Type:  DoubleThe final reprojection error注:objectPoints表示棋盤角點在世界坐標系下的坐標,有多少幅棋盤圖像就應有多少角點坐標集,以物理尺寸為單位。imagePoints表示角點在圖像中的坐標,以像素為單位。返回值是重投影誤差。

 

 

3、映射矩陣求取

[csharp] view plain copy print ?
  1. public static void cvInitUndistortMap(  
  2.     IntPtr intrinsicMatrix,  
  3.     IntPtr distortionCoeffs,  
  4.     IntPtr mapx,  
  5.     IntPtr mapy  
  6. )  
 
         
 
         

 

Parameters

intrinsicMatrix
Type:  System.IntPtrThe camera matrix (A) [fx 0 cx; 0 fy cy; 0 0 1]
distortionCoeffs
Type:  System.ntPtrThe vector of distortion coefficients, 4x1 or 1x4 [k1, k2, p1, p2].
mapx
Type:  System.IntPtrThe output array of x-coordinates of the map
mapy
Type:  System.ntPtrThe output array of y-coordinates of the map
注:定義Matrix類的映射矩陣變量,其屬性中包含Ptr,可以直接當指針用,如:

 

 

[csharp]  view plain  copy print ?
  1. private Matrix<float> mapx = new Matrix<float>(height, width);  
  2. private Matrix<float> mapy = new Matrix<float>(height, width);  

4、幾何變換

 

[csharp] view plain copy print ?
  1. public static void cvRemap(  
  2.     IntPtr src,  
  3.     IntPtr dst,  
  4.     IntPtr mapx,  
  5.     IntPtr mapy,  
  6.     int flags,  
  7.     MCvScalar fillval  
  8. )  

Parameters

src
Type:  System.ntPtr 
Source image
dst
Type:  System.IntPtr 
Destination image
mapx
Type:  System.IntPtr 
The map of x-coordinates (32fC1 image)
mapy
Type:  System.ntPtr 
The map of y-coordinates (32fC1 image)
flags
Type:  System.Int32 
A combination of interpolation method and the optional flag CV_WARP_FILL_OUTLIERS
fillval
Type:  Emgu.CV.Structure.MCvScalar 
A value used to fill outliers
注:flags定義在  CvEnum下的 WARP枚舉類型,調用: (int)WARP.CV_WARP_INVERSE_MAP 

 

程序說明

基於EmguCV攝像機標定及矯正的軟件界面如下:

如圖所示,界面包含棋盤格信息設置,標定及矯正事件的實現等等。

矯正前后圖像對比:

代碼實現了從攝像頭讀取棋盤格圖像或者從本地讀取圖像,圖像個數有Imges指定,棋盤格大小有Square Size指定;然后成功檢測到角點之后進行攝像頭標定,保存角點值和攝像頭內參數,通過Rectify按鈕實現畸變矯正功能。為避免每次標定時都要檢測角點,設置Read Corners按鈕,讀取角點(包含objectPoints和imagePoints),然后Start Calibrate實現標定。所有的數據都是保存到xml文件中,方便查看和提取。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM