
C#
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
前言
之前用OpenCV做過攝像機標定及矯正,現在平台換了,改用C#,就用EmguCV做一下,其實就是OpenCV的C#版。
在EmguCV中有兩類攝像機標定的類,一個是CvInvoke類,一個是CameraCalibration類,兩種標定效果差不多,只不過CvInvoke涉及的函數大多都是指針類型的,而C#對於指針的操作比較麻煩。本文是在CameraCalibration類的基礎上完成攝像機標定,用CvInvoke類完成矯正圖像。
函數說明
1、角點檢測
- public static PointF[] FindChessboardCorners(
- Image<Gray, byte> image,
- Size patternSize,
- CALIB_CB_TYPE flags
- )
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
注:輸入圖像需是灰度圖,檢測之前角點需要開辟空間,如:
- cornersDetected = new PointF[nPoints]; //nPoints表示單幅圖像角點總數
- cornersDetected = CameraCalibration.FindChessboardCorners(chessboardImage, patternSize,
- CALIB_CB_TYPE.ADAPTIVE_THRESH | CALIB_CB_TYPE.NORMALIZE_IMAGE);
2、標定函數
- public static double CalibrateCamera(
- MCvPoint3D32f[][] objectPoints,
- PointF[][] imagePoints,
- Size imageSize,
- IntrinsicCameraParameters intrinsicParam,
- CALIB_TYPE calibrationType,
- MCvTermCriteria termCriteria,
- out ExtrinsicCameraParameters[] extrinsicParams
- )
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、映射矩陣求取
- public static void cvInitUndistortMap(
- IntPtr intrinsicMatrix,
- IntPtr distortionCoeffs,
- IntPtr mapx,
- IntPtr mapy
- )
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
- private Matrix<float> mapx = new Matrix<float>(height, width);
- private Matrix<float> mapy = new Matrix<float>(height, width);
4、幾何變換
- public static void cvRemap(
- IntPtr src,
- IntPtr dst,
- IntPtr mapx,
- IntPtr mapy,
- int flags,
- MCvScalar fillval
- )
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
程序說明
基於EmguCV攝像機標定及矯正的軟件界面如下:
如圖所示,界面包含棋盤格信息設置,標定及矯正事件的實現等等。
矯正前后圖像對比:
代碼實現了從攝像頭讀取棋盤格圖像或者從本地讀取圖像,圖像個數有Imges指定,棋盤格大小有Square Size指定;然后成功檢測到角點之后進行攝像頭標定,保存角點值和攝像頭內參數,通過Rectify按鈕實現畸變矯正功能。為避免每次標定時都要檢測角點,設置Read Corners按鈕,讀取角點(包含objectPoints和imagePoints),然后Start Calibrate實現標定。所有的數據都是保存到xml文件中,方便查看和提取。