private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { try { PrintCharucoBoardDialogModel obj = d as PrintCharucoBoardDialogModel; obj.Image.CvImage = ChArUcoCalibration.DrawBoard(obj.SquaresX, obj.SquaresY, obj.SquareLength, obj.MarkerLength, new System.Drawing.Size(obj.SquaresX * obj.SquareLength, obj.SquaresY * obj.SquareLength), 0, obj.Dictionary); } catch (Exception) { } }
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is ChArUcoImageContainer) { ChArUcoImageContainer cauic = d as ChArUcoImageContainer; (VectorOfInt markerIds, VectorOfVectorOfPointF markerCorners, VectorOfInt charucoIds, VectorOfPointF charucoCorners)result = ChArUcoCalibration.Detect(cauic.OriginalImage.CvImage, cauic.SquaresX, cauic.SquaresY, cauic.SquareLength, cauic.MarkerLength, cauic.Dictionary); cauic.MarkerIds = result.markerIds; cauic.MarkerCorners = result.markerCorners; cauic.CharucoIds = result.charucoIds; cauic.CharucoCorners = result.charucoCorners; cauic.ProgressedImage = new CvImageContainer(); cauic.ProgressedImage.CvImage = ChArUcoCalibration.Draw(cauic.OriginalImage.CvImage, result.markerIds, result.markerCorners, result.charucoIds, result.charucoCorners); } }
private Task DoValidate(object o) { return(Task.Factory.StartNew(() => { Parent.SyncContext.Post(async c => { VectorOfInt allIds = new VectorOfInt(); VectorOfVectorOfPointF allCorners = new VectorOfVectorOfPointF(); VectorOfInt allCharucoIds = new VectorOfInt(); VectorOfPointF allCharucoCorners = new VectorOfPointF(); VectorOfInt markerCounterPerFrame = new VectorOfInt(); VectorOfInt charucoCounterPerFrame = new VectorOfInt(); int squaresX = 0; int squaresY = 0; float squareLength = 0f; float markerLength = 0f; PredefinedDictionaryName dictionary = PredefinedDictionaryName.Dict4X4_50; System.Drawing.Size size = new System.Drawing.Size(); bool fisheye = false; Parent.SyncContext.Send(async d => { fisheye = Parent.CameraViewModel.FishEyeCalibration; squaresX = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.SquaresX; squaresY = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.SquaresY; squareLength = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.SquareLength; markerLength = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.MarkerLength; dictionary = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.Dictionary; size = new System.Drawing.Size(Parent.CameraViewModel.ImageWidth, Parent.CameraViewModel.ImageHeight); foreach (ChArUcoImageContainer image in Images) { if (image.MarkerCorners != null && image.CharucoCorners.Size > 4) { allIds.Push(image.MarkerIds); allCorners.Push(image.MarkerCorners); allCharucoIds.Push(image.CharucoIds); allCharucoCorners.Push(image.CharucoCorners); markerCounterPerFrame.Push(new int[] { image.MarkerCorners.Size }); charucoCounterPerFrame.Push(new int[] { image.CharucoCorners.Size }); } } }, null); if (markerCounterPerFrame.Size > 0) { MetroDialogSettings settings = new MetroDialogSettings() { AnimateShow = false, AnimateHide = false }; var controller = await Parent.DialogCoordinator.ShowProgressAsync(Parent, "Please wait...", "Validate parameter now!", settings: Parent.MetroDialogSettings); controller.SetIndeterminate(); controller.SetCancelable(false); bool error = false; double rms = 0.0; try { Mat cameraMatrix = new Mat(3, 3, Emgu.CV.CvEnum.DepthType.Cv64F, 1); Mat distCoeffs = new Mat(1, Parent.CameraViewModel.FishEyeCalibration ? 4 : _DistCoeffs.Count, Emgu.CV.CvEnum.DepthType.Cv64F, 1); cameraMatrix.SetValue(0, 0, Fx); cameraMatrix.SetValue(1, 1, Fy); cameraMatrix.SetValue(0, 1, Fx * Alpha); cameraMatrix.SetValue(0, 2, Cx); cameraMatrix.SetValue(1, 2, Cy); cameraMatrix.SetValue(2, 2, 1.0f); for (int i = 0; i < distCoeffs.Cols && (Parent.CameraViewModel.FishEyeCalibration ? i < 4 : true); i++) { distCoeffs.SetValue(0, i, _DistCoeffs[i]); } rms = ChArUcoCalibration.ValidateCharuco(squaresX, squaresY, squareLength, markerLength, dictionary, size, allCharucoIds, allCharucoCorners, charucoCounterPerFrame, fisheye, delegate(byte[] input) { return Parent.IOProxy.GetRemoteChessboardCorner(input); }, cameraMatrix, distCoeffs); } catch (Exception ex) { error = true; } await controller.CloseAsync(); if (!error) { var con = await Parent.DialogCoordinator.ShowMessageAsync(Parent, "Result", string.Format("RMS: {0}", rms), MessageDialogStyle.Affirmative, null); } else { await Parent.DialogCoordinator.ShowMessageAsync(Parent, "Error", "Error during validation!"); } } else { await Parent.DialogCoordinator.ShowMessageAsync(Parent, "Error", "Not enough valide input frames available!"); } }, null); })); }
private Task DoCalibrate(object o) { return(Task.Factory.StartNew(async() => { VectorOfInt allIds = new VectorOfInt(); VectorOfVectorOfPointF allCorners = new VectorOfVectorOfPointF(); VectorOfInt allCharucoIds = new VectorOfInt(); VectorOfPointF allCharucoCorners = new VectorOfPointF(); VectorOfInt markerCounterPerFrame = new VectorOfInt(); VectorOfInt charucoCounterPerFrame = new VectorOfInt(); int squaresX = 0; int squaresY = 0; float squareLength = 0f; float markerLength = 0f; PredefinedDictionaryName dictionary = PredefinedDictionaryName.Dict4X4_50; System.Drawing.Size size = new System.Drawing.Size(); bool fisheye = false; Parent.SyncContext.Send(async c => { fisheye = Parent.CameraViewModel.FishEyeCalibration; squaresX = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.SquaresX; squaresY = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.SquaresY; squareLength = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.SquareLength; markerLength = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.MarkerLength; size = Parent.CameraViewModel.Image.CvImage.Size; dictionary = Parent.SettingContainer.Settings.CalibrationSettings.ChArucoCalibrationSettings.Dictionary; foreach (ChArUcoImageContainer image in Images) { if (image.MarkerCorners != null && image.CharucoCorners.Size > 4) { allIds.Push(image.MarkerIds); allCorners.Push(image.MarkerCorners); allCharucoIds.Push(image.CharucoIds); allCharucoCorners.Push(image.CharucoCorners); markerCounterPerFrame.Push(new int[] { image.MarkerCorners.Size }); charucoCounterPerFrame.Push(new int[] { image.CharucoCorners.Size }); } } }, null); if (markerCounterPerFrame.Size > 0) { MetroDialogSettings settings = new MetroDialogSettings() { AnimateShow = true, AnimateHide = true }; var controller = await Parent.DialogCoordinator.ShowProgressAsync(Parent, "Please wait...", "Calculating calibration parameter now!", settings: settings); controller.SetIndeterminate(); controller.SetCancelable(false); bool error = false; (Mat cameraMatrix, Mat distCoeffs, double rms)result = (null, null, 0.0); try { result = ChArUcoCalibration.CalibrateCharuco(squaresX, squaresY, squareLength, markerLength, dictionary, size, allCharucoIds, allCharucoCorners, charucoCounterPerFrame, fisheye, delegate(byte[] input) { return Parent.IOProxy.GetRemoteChessboardCorner(input); }); } catch (Exception ex) { error = true; } await controller.CloseAsync(); if (!error) { var con = await Parent.DialogCoordinator.ShowMessageAsync(Parent, "Result", string.Format("RMS: {0}\nDo you want to save?", result.rms), MessageDialogStyle.AffirmativeAndNegative, null); if (con == MessageDialogResult.Affirmative) { Parent.SyncContext.Post(async c => { Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Fx = result.cameraMatrix.GetValue(0, 0); Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Fy = result.cameraMatrix.GetValue(1, 1); Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Cx = result.cameraMatrix.GetValue(0, 2); Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Cy = result.cameraMatrix.GetValue(1, 2); Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Alpha = result.cameraMatrix.GetValue(0, 1) / result.cameraMatrix.GetValue(0, 0);; Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.DistCoeffs.Clear(); if (Parent.CameraViewModel.FishEyeCalibration) { for (int i = 0; i < result.distCoeffs.Rows && i < 8; i++) { Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.DistCoeffs.Add(result.distCoeffs.GetValue(i, 0)); } } else { for (int i = 0; i < result.distCoeffs.Cols && i < 8; i++) { Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.DistCoeffs.Add(result.distCoeffs.GetValue(0, i)); } } Parent.UpdateSettings(false); }, null); } } else { await Parent.DialogCoordinator.ShowMessageAsync(Parent, "Error", "Error during calibration!"); } } else { await Parent.DialogCoordinator.ShowMessageAsync(Parent, "Error", "Not enough valide input frames available!"); } })); }
internal override void SettingsUpdated() { base.SettingsUpdated(); bool changed = false; changed |= Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Fx != Fx; changed |= Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Fy != Fy; changed |= Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Cx != Cx; changed |= Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Cy != Cy; changed |= Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Alpha != Alpha; var firstNotSecond = _DistCoeffs.Except(Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.DistCoeffs).ToList(); var secondNotFirst = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.DistCoeffs.Except(_DistCoeffs).ToList(); changed |= firstNotSecond.Any() || secondNotFirst.Any(); if (changed) { Fx = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Fx; Fy = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Fy; Cx = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Cx; Cy = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Cy; Alpha = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.Alpha; _DistCoeffs = Parent.SettingContainer.Settings.CalibrationSettings.IntrinsicCalibrationSettings.DistCoeffs.ToList(); if (_DistCoeffs.Count == 4) { K1 = _DistCoeffs[0]; K2 = _DistCoeffs[1]; K3 = _DistCoeffs[2]; K4 = _DistCoeffs[3]; } Mat board = ChArUcoCalibration.DrawBoard(5, 5, 0.04f, 0.02f, new System.Drawing.Size(Parent.CameraViewModel.ImageWidth, Parent.CameraViewModel.ImageHeight), 10, PredefinedDictionaryName.Dict6X6_250); Mat boardDist = board.Clone(); Mat cameraMatrix = new Mat(3, 3, Emgu.CV.CvEnum.DepthType.Cv64F, 1); Mat distCoeffs = new Mat(1, Parent.CameraViewModel.FishEyeCalibration ? 4 : _DistCoeffs.Count, Emgu.CV.CvEnum.DepthType.Cv64F, 1); cameraMatrix.SetValue(0, 0, Fx); cameraMatrix.SetValue(1, 1, Fy); cameraMatrix.SetValue(0, 1, Fx * Alpha); cameraMatrix.SetValue(0, 2, Cx); cameraMatrix.SetValue(1, 2, Cy); cameraMatrix.SetValue(2, 2, 1.0f); Mat newK = new Mat(3, 3, Emgu.CV.CvEnum.DepthType.Cv64F, 1); for (int i = 0; i < distCoeffs.Cols && (Parent.CameraViewModel.FishEyeCalibration ? i < 4 : true); i++) { distCoeffs.SetValue(0, i, _DistCoeffs[i]); } if (Parent.CameraViewModel.FishEyeCalibration) { Fisheye.EstimateNewCameraMatrixForUndistorRectify(cameraMatrix, distCoeffs, new System.Drawing.Size(Parent.CameraViewModel.ImageWidth, Parent.CameraViewModel.ImageHeight), Mat.Eye(3, 3, Emgu.CV.CvEnum.DepthType.Cv64F, 1), newK, 0, new System.Drawing.Size(Parent.CameraViewModel.ImageWidth, Parent.CameraViewModel.ImageHeight), 0.3); Mat map1 = new Mat(); Mat map2 = new Mat(); Fisheye.InitUndistorRectifyMap(cameraMatrix, distCoeffs, Mat.Eye(3, 3, Emgu.CV.CvEnum.DepthType.Cv64F, 1), newK, new System.Drawing.Size(Parent.CameraViewModel.ImageWidth, Parent.CameraViewModel.ImageHeight), Emgu.CV.CvEnum.DepthType.Cv32F, map1, map2); CvInvoke.Remap(board, boardDist, map1, map2, Emgu.CV.CvEnum.Inter.Linear, Emgu.CV.CvEnum.BorderType.Constant); } else { CvInvoke.Undistort(board, boardDist, cameraMatrix, distCoeffs); } Parent.SyncContext.Post(c => { ChAruCoBoard = new CvImageContainer(); ChAruCoBoard.CvImage = boardDist; }, null); } }