void doAction()
 {
     try
     {
         setDialogResult(null);
         _progressData = new ProgressData("Processing...", 0);
         _progressData.ProgressChanged += onProgressChanged;
         _action(_progressData);
         setDialogResult(true);
     }
     catch (ThreadAbortException)
     {
         setDialogResult(false);
     }
     closeAsync();
 }
Example #2
0
        private void buttonFlatDepthCalib_Click(object sender, RoutedEventArgs e)
        {
            // 改造
            string            path1;
            MotionDataHandler handler1;
            {
                if (openMotionData(out handler1, out path1))
                {
                    IEnumerable <CvMat> depthImages1 = null;
                    Utility.LoadImages(handler1.GetDepthImagePaths(), out depthImages1);

                    List <double> errorVarLog = new List <double>();
                    CvMat         resultMat   = null;

                    if (ProgressData.DoAction(progress =>
                    {
                        int length = handler1.FrameCount;
                        progress.InitProgress("Calculating...", length);
                        DepthUndistortionLinearCalibrator undistort = new DepthUndistortionLinearCalibrator(this.UndistortionData.CameraStruct, 1);
                        CvMat mat = null;

                        foreach (CvMat depthMat in depthImages1)
                        {
                            progress.CurrentValue++;
                            CalcEx.SmoothDepthStep(ref mat, depthMat, 19);
                            undistort.PutDepthImage(ref resultMat, mat, _undistortion);
                            viewDepthUndistionMat(resultMat, depthMat);
                        }

                        this.UndistortionData.SetUndistortionDepthMat(undistort.GetUndistortCoefMat(), path1);
                    }, "Flat Depth Calib", true))
                    {
                        displayLabels();
                        viewDepthUndistionMat(this.UndistortionData.UndistortionDepthMat);
                    }
                }
            }
        }
Example #3
0
        private void buttonCameraCalibration_Click(object sender, RoutedEventArgs e)
        {
            int    cols, rows;
            double horizLength, vertLength;

            if (!parseChessboardParameters(out cols, out rows, out horizLength, out vertLength))
            {
                return;
            }

            int imageNum;

            if (!int.TryParse(textCalibrationCameraIteration.Text, out imageNum) || imageNum <= 0)
            {
                System.Windows.MessageBox.Show(string.Format("キャリブレーションに使用するイメージ数が不正です: {0}", textCalibrationCameraIteration.Text));
                return;
            }

            // 以下改造
            MotionDataHandler handler;
            string            path;

            if (openMotionData(out handler, out path))
            {
                CvMat displayMat1 = null;
                CvMat displayMat3 = null;
                CvMat gray        = null;
                if (ProgressData.DoAction(progress =>
                {
                    int length = handler.FrameCount;
                    if (length == 0)
                    {
                        return;
                    }
                    progress.InitProgress("Find Chessboard...", length * 2);
                    CvSize boardSize = new CvSize(cols, rows);
                    List <CvPoint2D32f[]> list = new List <CvPoint2D32f[]>();
                    CvSize imageSize = new CvSize();
                    CvPoint2D32f[] lastCorners = null;

                    IEnumerable <CvMat> colorImages;
                    Utility.LoadImages(handler.GetColorImagePaths(), out colorImages);

                    foreach (CvMat imageMat in colorImages)
                    {
                        progress.CurrentValue++;
                        using (imageMat)
                        {
                            imageSize = new CvSize(imageMat.Cols, imageMat.Rows);
                            CvPoint2D32f[] corners;
                            int count;
                            CvEx.InitCvMat(ref gray, imageMat, MatrixType.U8C1);
                            imageMat.CvtColor(gray, ColorConversion.RgbToGray);
                            if (gray.FindChessboardCorners(boardSize, out corners, out count, ChessboardFlag.AdaptiveThresh))
                            {
                                CvEx.CloneCvMat(ref displayMat1, imageMat);
                                CvTermCriteria criteria = new CvTermCriteria(20, 0.1);
                                gray.FindCornerSubPix(corners, count, new CvSize(11, 11), new CvSize(-1, -1), criteria);
                                list.Add(corners);
                                CvEx.DrawChessboardCornerFrame(displayMat1, boardSize, corners, new CvScalar(64, 128, 64));
                                displayMat1.DrawChessboardCorners(boardSize, corners, true);
                                lastCorners = corners;
                                putImage(displayMat1, PixelFormats.Bgr24);
                            }
                            else
                            {
                                CvEx.CloneCvMat(ref displayMat3, imageMat);
                                putImage(displayMat3, PixelFormats.Bgr24);
                            }
                        }
                    }

                    progress.SetProgress("Calibrating...", length);

                    this.UndistortionData.CalibrateCamera(list, cols, rows, (horizLength + vertLength) / 2, imageSize, imageNum, path);
                    CvMat displayMat2 = CvEx.InitCvMat(displayMat1);
                    displayMat1.Undistort2(displayMat2, this.UndistortionData.CameraStruct.CreateCvMat(), this.UndistortionData.DistortStruct.CreateCvMat(true));
                    if (lastCorners != null)
                    {
                        drawUndistortedCornerFrame(displayMat2, lastCorners, boardSize);
                    }

                    putImage(displayMat2, PixelFormats.Bgr24);
                }, "Camera Calib", true))
                {
                    displayLabels();
                }
            }
        }
Example #4
0
        private void buttonCalibrateScaleOffset_Click(object sender, RoutedEventArgs e)
        {
            int    cols, rows;
            double horizLength, vertLength;

            if (!parseChessboardParameters(out cols, out rows, out horizLength, out vertLength))
            {
                return;
            }

            // 以下改造
            MotionDataHandler handler;
            string            path;

            if (openMotionData(out handler, out path))
            {
                CvMat displayMat1 = null;
                CvMat displayMat3 = null;
                CvMat gray        = null;

                if (ProgressData.DoAction(progress =>
                {
                    int length = handler.FrameCount;
                    if (length == 0)
                    {
                        return;
                    }
                    progress.InitProgress("Find Chessboard...", length * 2);

                    CvSize boardSize = new CvSize(cols, rows);
                    List <CvPoint3D32f?[]> list = new List <CvPoint3D32f?[]>();
                    CvSize imageSize = new CvSize();
                    CvPoint2D32f[] lastCorners = null;

                    IEnumerable <CvMat> colorImages, depthImages;
                    Utility.LoadImages(handler.GetColorImagePaths(), out colorImages);
                    Utility.LoadImages(handler.GetDepthImagePaths(), out depthImages);
                    var images = colorImages.Zip(depthImages, (first, second) => Tuple.Create(first, second));

                    foreach (Tuple <CvMat, CvMat> imagePair in images)
                    {
                        progress.CurrentValue++;

                        CvMat imageMat = imagePair.Item1;
                        CvMat depthMat = imagePair.Item2;
                        imageSize = new CvSize(imageMat.Cols, imageMat.Rows);
                        CvPoint2D32f[] corners;
                        int count;
                        CvEx.InitCvMat(ref gray, imageMat, MatrixType.U8C1);
                        imageMat.CvtColor(gray, ColorConversion.RgbToGray);
                        if (gray.FindChessboardCorners(boardSize, out corners, out count, ChessboardFlag.AdaptiveThresh))
                        {
                            CvEx.CloneCvMat(ref displayMat1, imageMat);
                            CvTermCriteria criteria = new CvTermCriteria(50, 0.01);
                            gray.FindCornerSubPix(corners, count, new CvSize(3, 3), new CvSize(-1, -1), criteria);
                            CvPoint3D32f?[] cornerPoints = new CvPoint3D32f?[corners.Length];
                            for (int j = 0; j < corners.Length; j++)
                            {
                                CvPoint2D32f corner = corners[j];
                                double?value = CalcEx.BilateralFilterDepthMatSinglePixel(corner, depthMat, 100, 4, 9);
                                if (value.HasValue)
                                {
                                    cornerPoints[j] = new CvPoint3D32f(corner.X, corner.Y, value.Value);
                                }
                            }
                            list.Add(cornerPoints);
                            CvEx.DrawChessboardCornerFrame(displayMat1, boardSize, corners, new CvScalar(64, 128, 64));
                            displayMat1.DrawChessboardCorners(boardSize, corners, true);
                            lastCorners = corners;
                            //putImage(displayMat1, PixelFormats.Bgr24);
                        }
                        else
                        {
                            CvEx.CloneCvMat(ref displayMat3, imageMat);
                            //putImage(displayMat3, PixelFormats.Bgr24);
                        }
                    }
                    progress.SetProgress("Scale Offset Calibrating...", length);

                    this.UndistortionData.CalibrateRealScaleAndOffset(list, cols, rows, horizLength, vertLength, imageSize);
                    CvMat displayMat2 = CvEx.InitCvMat(displayMat1);
                    displayMat1.Undistort2(displayMat2, this.UndistortionData.CameraStruct.CreateCvMat(), this.UndistortionData.DistortStruct.CreateCvMat(true));
                    if (lastCorners != null)
                    {
                        drawUndistortedCornerFrame(displayMat2, lastCorners, boardSize);
                    }

                    displayMat2.PutText(string.Format("XScale: {0}", this.UndistortionData.XScale), new CvPoint(20, 20), new CvFont(FontFace.HersheyPlain, 1, 1), new CvScalar(255, 255, 255));
                    displayMat2.PutText(string.Format("YScale: {0}", this.UndistortionData.YScale), new CvPoint(20, 40), new CvFont(FontFace.HersheyPlain, 1, 1), new CvScalar(255, 255, 255));
                    displayMat2.PutText(string.Format("Zoffset: {0}", this.UndistortionData.ZOffset), new CvPoint(20, 60), new CvFont(FontFace.HersheyPlain, 1, 1), new CvScalar(255, 255, 255));
                    putImage(displayMat2, PixelFormats.Bgr24);
                }, "Calibrate Scale Offset", true))
                {
                    displayLabels();
                }
            }
        }
 public ProgressData GetSubProgress(double startValue, double valueRange, Func<ProgressData, string> getMessage)
 {
     ProgressData sub = new ProgressData(this.Message, 0);
     sub.ProgressChanged += (sender, e) =>
     {
         double subMax = sub.MaximumValue;
         double subValue = Math.Max(0, Math.Min(sub.CurrentValue, subMax));
         if (subMax > 0)
         {
             this.SetProgress(getMessage(sub), startValue + subValue * valueRange / subMax);
         }
         else
         {
             this.SetProgress(getMessage(sub), startValue);
         }
     };
     return sub;
 }