//------------------------------------------------------------------------------------------------
        //Создание изображения из матрицы интенсивностей серого по шаблону
        public static WriteableBitmap CreateGrayScaleWriteableBitmapFromMatrix(
            RealMatrix grayScaleMatrix,
            int dpiX, int dpiY, BitMask2D bitMask
            )
        {
            int             pixelWidth      = grayScaleMatrix.ColumnCount;
            int             pixelHeight     = grayScaleMatrix.RowCount;
            WriteableBitmap writeableBitmap = WriteableBitmapCreator.CreateWriteableBitmap
                                                  (pixelWidth, pixelHeight, dpiX, dpiY, PixelFormats.Bgra32);
            WriteableBitmapWrapper bitmapWrapper = WriteableBitmapWrapper.Create(writeableBitmap);

            for (int x = 0; x < pixelWidth; x++)
            {
                for (int y = 0; y < pixelHeight; y++)
                {
                    if (bitMask[y, x] == true)
                    {
                        int  grayIntensity = ( int )Math.Round(grayScaleMatrix[y, x]);
                        byte red, green, blue;
                        red = green = blue = Convert.ToByte(grayIntensity);
                        Color color = Color.FromRgb(red, green, blue);
                        bitmapWrapper.SetPixelColor(x, y, color);
                    }
                }
            }
            return(writeableBitmap);
        }
        //-----------------------------------------------------------------------------------------------
        //Расшифровка интерферограммы
        public InterferogramDecodingResult DecodeInterferogram(
            RealMatrix[] interferograms, double[] phaseShifts, BitMask2D bitMask
            )
        {
            RealVector cosVector = this.GetCosVector(phaseShifts);
            RealVector sinVector = this.GetSinVector(phaseShifts);

            int        width       = interferograms[0].ColumnCount;
            int        height      = interferograms[0].RowCount;
            RealMatrix phaseMatrix = new RealMatrix(height, width);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    if (bitMask[y, x])
                    {
                        double[]   intensities                 = this.CreateIntensitiesAtPoint(x, y, interferograms);
                        RealVector intensitiesVector           = new RealVector(intensities);
                        RealVector orthogonalIntensitiesVector = intensitiesVector.GetOrthogonalVector();
                        double     phase =
                            this.CalculatePhase(orthogonalIntensitiesVector, sinVector, cosVector);
                        phaseMatrix[y, x] = phase;
                    }
                    else
                    {
                        phaseMatrix[y, x] = this.DefaultPhaseValue;
                    }
                }
            }
            InterferogramDecodingResult decodingResult = new InterferogramDecodingResult(phaseMatrix);

            return(decodingResult);
        }
        //--------------------------------------------------------------------------------------------------------------
        //Точки в пространстве из интенсивностей для строки интерферограммы
        public static Point3D[] GetSpatialPointsFromInterferogramsRow(
            RealMatrix[] interferograms, BitMask2D bitMask, int row
            )
        {
            if (interferograms.Length != 3)
            {
                throw new Exception();
            }
            int width  = interferograms[0].ColumnCount;
            int height = interferograms[0].RowCount;

            int            y      = row;
            List <Point3D> points = new List <Point3D>();

            for (int x = 0; x < width; x++)
            {
                if (bitMask[y, x])
                {
                    double[] intensities = MatricesManager.GeValuesFromMatrices(row, x, interferograms);
                    Point3D  point       = new Point3D(intensities);
                    points.Add(point);
                }
            }
            return(points.ToArray());
        }
        //-----------------------------------------------------------------------------------------------
        //Преобразование значений матрицы по шаблону
        public static RealMatrix TransformMatrixValues(
            RealMatrix matrix, BitMask2D stencilMatrix,
            RealIntervalTransform intervalTransform, double prickedValue
            )
        {
            int        rowCount          = matrix.RowCount;
            int        columnCount       = matrix.ColumnCount;
            RealMatrix transformedMatrix = new RealMatrix(rowCount, columnCount);

            for (int row = 0; row < rowCount; row++)
            {
                for (int column = 0; column < columnCount; column++)
                {
                    if (stencilMatrix[row, column] == true)
                    {
                        double value            = matrix[row, column];
                        double transformedValue =
                            intervalTransform.TransformToFinishIntervalValue(value);
                        transformedMatrix[row, column] = transformedValue;
                    }
                    else
                    {
                        transformedMatrix[row, column] = prickedValue;
                    }
                }
            }
            return(transformedMatrix);
        }
Example #5
0
        //---------------------------------------------------------------------------------------------------
        //Числители формулы расшифровки для всех точек интерферограммы ( по маске )
        public double[] GetDecodingFormulaNumerators(
            RealMatrix[] interferograms, double[] phaseShifts, BitMask2D bitMask
            )
        {
            bool validateArraySizes =
                this.ValidateArraySize(interferograms) &&
                this.ValidateArraySize(phaseShifts);

            if (!validateArraySizes)
            {
                throw new InterferogramDecodingException();
            }

            int width  = interferograms[0].ColumnCount;
            int height = interferograms[0].RowCount;

            List <double> numerators = new List <double>();

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    if (bitMask[y, x])
                    {
                        double[] intensities = this.CreateIntensitiesAtPoint(x, y, interferograms);
                        double   numerator   = this.GetDecodingFormulaNumerator(intensities, phaseShifts);
                        numerators.Add(numerator);
                    }
                }
            }
            return(numerators.ToArray());
        }
        //---------------------------------------------------------------------------------------------
        //---------------------------------------------------------------------------------------------
        public InterferogramDecodingResult DecodeInterferogram(
            BitMask2D bitMask, params RealMatrix[] interferograms
            )
        {
            //Выбранные точки изображения
            Point[] selectedImagePoints = bitMask.GetTruePoints();

            double[] phaseShifts = this.GetRandomPhaseShifts(interferograms.Length);
            GenericInterferogramDecoder genericInterferogramDecoder = new GenericInterferogramDecoder();

            double[] numerators = genericInterferogramDecoder.GetDecodingFormulaNumerators
                                      (interferograms, phaseShifts, bitMask);
            double[] denominators = genericInterferogramDecoder.GetDecodingFormulaDenominators
                                        (interferograms, phaseShifts, bitMask);

            Point2D[] points2D = PlaneManager.CreatePoints2D(numerators, denominators);

            if (this.ExecutePointsCorrecting)
            {
                points2D = this.GetTransformedPoints(points2D);
            }
            int        sizeX        = interferograms[0].ColumnCount;
            int        sizeY        = interferograms[0].RowCount;
            RealMatrix resultMatrix =
                this.CalculatePhaseMatrix(points2D, selectedImagePoints, sizeX, sizeY);
            InterferogramDecodingResult decodingResult = new InterferogramDecodingResult(resultMatrix);

            return(decodingResult);
        }
        //---------------------------------------------------------------------------------------------
        public InterferogramDecodingResult DecodeInterferogram(
            BitMask2D bitMask, params RealMatrix[] interferograms
            )
        {
            //Выбранные точки изображения
            Point[] selectedImagePoints = bitMask.GetTruePoints();

            //3D точки в плоскости, параллельной плоскости XY
            Point3D[] points3D =
                this.GetParallelToCoordinatePlanePoints3D(interferograms, selectedImagePoints);

            Point2D[] points2D = SpaceManager.GetProjectionXY(points3D);
            this.ProjectionPoints = points2D;
            points2D = PlaneManager.DisplacePointsToFirstQuadrant(points2D);

            Point2D[] transformedPoints = this.GetTransformedPoints(points2D);
            int       sizeX             = interferograms[0].ColumnCount;
            int       sizeY             = interferograms[0].RowCount;

            RealMatrix phaseMatrix =
                this.CalculatePhaseMatrix(transformedPoints, selectedImagePoints, sizeX, sizeY);

            InterferogramDecodingResult decodingResult = new InterferogramDecodingResult(phaseMatrix);

            return(decodingResult);
        }
        //--------------------------------------------------------------------------------------------------------------
        //Приведение матрицы фаз к диапазону 0..255 ( уровень серого )
        public static RealMatrix TrnsformPhaseMatrixToGrayScaleMatrix(
            RealMatrix matrix, BitMask2D bitMask, double prickedValue
            )
        {
            Interval <double>     startInterval     = new Interval <double>(0, 2 * Math.PI);
            Interval <double>     finishInterval    = new Interval <double>(0, 255);
            RealIntervalTransform intervalTransform = new RealIntervalTransform(startInterval, finishInterval);
            RealMatrix            scaledMatrix      =
                RealMatrixValuesTransform.TransformMatrixValues
                    (matrix, bitMask, intervalTransform, prickedValue);

            return(scaledMatrix);
        }
Example #9
0
        //------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------

        /*
         * //Значение целевой функции для интерферограмм при определенном значении гамма
         * //( Расстояния до средней точки )
         * private double GetTargetFunctionValue(
         *  double gamma, RealMatrix[] interferograms, BitMask2D bitMask
         * ) {
         *  RealMatrix[] gammaCorrectedInterferograms =
         *      this.GetGammaCorrectedInterferograms( gamma, interferograms );
         *  Point3D[] points = this.GetSpatialPoints( gammaCorrectedInterferograms, bitMask );
         *  Point3D midPoint = SpaceManager.GetMidPoint( points );
         *  double[] distancesFromPointsToMidPoint = SpaceManager.GetDistances( points, midPoint );
         *  double averageDistance = distancesFromPointsToMidPoint.Average();
         *  double targetFunctionValue =
         *      this.CalculateTargetFunctionValue( averageDistance, distancesFromPointsToMidPoint );
         *  return targetFunctionValue;
         * }
         */
        //------------------------------------------------------------------------------------------
        //Значение целевой функции для интерферограмм при определенном значении гамма
        //( Аппроксимация плоскостью - вычисление невязки )
        private double GetTargetFunctionValue(
            double gamma, RealMatrix[] interferograms, BitMask2D bitMask
            )
        {
            RealMatrix[] gammaCorrectedInterferograms =
                this.GetGammaCorrectedInterferograms(gamma, interferograms);
            Point3D[]         points            = this.GetSpatialPoints(gammaCorrectedInterferograms, bitMask);
            PlaneApproximator planeApproximator = new PlaneApproximator();
            PlaneDescriptor   planeDescriptor   = planeApproximator.Approximate(points);

            double[] misalignments       = planeDescriptor.GetPointsMisalignments(points);
            double   averageMisalignment = misalignments.Average();
            double   targetFunctionValue =
                this.CalculateTargetFunctionValue(averageMisalignment, misalignments);

            return(targetFunctionValue);
        }
Example #10
0
        //---------------------------------------------------------------------------------------------------
        //Расшифровка интерферограммы
        public InterferogramDecodingResult DecodeInterferogram(
            RealMatrix[] interferograms, double[] phaseShifts, BitMask2D bitMask
            )
        {
            bool validateArraySizes =
                this.ValidateArraySize(interferograms) &&
                this.ValidateArraySize(phaseShifts);

            if (!validateArraySizes)
            {
                throw new InterferogramDecodingException();
            }

            int width  = interferograms[0].ColumnCount;
            int height = interferograms[0].RowCount;

            RealMatrix resultMatrix = new RealMatrix(height, width);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    if (bitMask[y, x] == true)
                    {
                        double[] intensities = this.CreateIntensitiesAtPoint(x, y, interferograms);
                        double   phase       = this.Decode(intensities, phaseShifts);
                        resultMatrix[y, x] = phase;
                    }
                    else
                    {
                        resultMatrix[y, x] = this.DefaultPhaseValue;
                    }
                }
            }
            InterferogramDecodingResult decodingResult = new InterferogramDecodingResult(resultMatrix);

            return(decodingResult);
        }
        //---------------------------------------------------------------------------------------------------
        //Знаменатели формулы расшифровки
        public double[] GetDecodingFormulaDenominators(
            RealMatrix[] interferograms, double[] phaseShifts, BitMask2D bitMask
            )
        {
            int           width        = interferograms[0].ColumnCount;
            int           height       = interferograms[0].RowCount;
            List <double> denominators = new List <double>();

            if (bitMask == null)
            {
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        double[] intensities = this.CreateIntensitiesAtPoint(x, y, interferograms);
                        double   denominator = this.GetDecodingFormulaDenominator(intensities, phaseShifts);
                        denominators.Add(denominator);
                    }
                }
            }
            else
            {
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        if (bitMask[y, x])
                        {
                            double[] intensities = this.CreateIntensitiesAtPoint(x, y, interferograms);
                            double   denominator = this.GetDecodingFormulaDenominator(intensities, phaseShifts);
                            denominators.Add(denominator);
                        }
                    }
                }
            }
            return(denominators.ToArray());
        }
Example #12
0
        //---------------------------------------------------------------------------------------
        //---------------------------------------------------------------------------------------
        //Автоматическая гамма-коррекция интерферограмм
        public RealMatrix[] GetGammaCorrectedInterferograms(
            RealMatrix[] interferograms, BitMask2D bitMask
            )
        {
            if (interferograms.Length != 3)
            {
                throw new Exception();
            }
            double[] gammaValues = this.GetGammaValues();

            this.GammaValues          = gammaValues;
            this.TargetFunctionValues = new double[gammaValues.Length];

            double firstGammaValue        = gammaValues[0];
            double optimalGammaValue      = firstGammaValue;
            double minTargetFunctionValue =
                this.GetTargetFunctionValue(firstGammaValue, interferograms, bitMask);

            this.TargetFunctionValues[0] = minTargetFunctionValue;

            for (int index = 1; index < gammaValues.Length; index++)
            {
                double gamma = gammaValues[index];
                double targetFunctionValue = this.GetTargetFunctionValue(gamma, interferograms, bitMask);
                this.TargetFunctionValues[index] = targetFunctionValue;
                if (targetFunctionValue < minTargetFunctionValue)
                {
                    minTargetFunctionValue = targetFunctionValue;
                    optimalGammaValue      = gamma;
                }
            }
            RealMatrix[] resultInterferograms =
                this.GetGammaCorrectedInterferograms(optimalGammaValue, interferograms);

            return(resultInterferograms);
        }
        //---------------------------------------------------------------------------------------------
        public InterferogramDecodingResult DecodeInterferogram(RealMatrix[] interferograms, BitMask2D bitMask)
        {
            //double gamma = 2;
            //interferograms = MatricesManager.GetGammaCorrectedMatrices( gamma, interferograms );
            //interferograms = GetNormalizedMatrices( interferograms );

            //Выбранные точки изображения
            Point[]   selectedImagePoints = bitMask.GetTruePoints();
            Point3D[] points3D            =
                this.GetParallelToCoordinatePlanePoints3D(interferograms, selectedImagePoints);
            this.TrajectoryPoints = points3D;

            Point2D[] points2D = SpaceManager.GetProjectionXY(points3D);
            this.ProjectionPoints = points2D;

            EllipseDescriptor ellipseDescriptor = this.GetEllipseDescriptor(points2D);
            Point2D           ellipseCentre     = ellipseDescriptor.GetCentre();
            Point3D           pointsCentre      = new Point3D(ellipseCentre.X, ellipseCentre.Y, points3D[0].Z);

            RealVector cylinderGuidLine           = this.GetCylinderGuidLine(ellipseDescriptor);
            RealVector normalizedCylinderGuidLine = cylinderGuidLine.GetNormalizedVector();

            /*
             * this.CalculateCirclePointsDispersion( cylinderGuidLine, points3D, pointsCentre );
             * double cylinderGuidLineOptimalAngle = this.GetCylinderGuidLineOptimalAngle();
             * cylinderGuidLine = this.GetRotatedInPlaneCylinderGuidLine
             *  ( cylinderGuidLine, cylinderGuidLineOptimalAngle );
             */

            Point3D[] circlePoints = this.GetCirclePoints(points3D, normalizedCylinderGuidLine);
            this.CirclePoints = circlePoints;
            Point3D circlePointsCentre =
                this.GetCorrectedCirclePoint(pointsCentre, normalizedCylinderGuidLine);

            //Поворот точек
            RealMatrix rotationMatrix = this.GetRotationMatrix(circlePoints);

            //rotationMatrix.WriteToTextFile( @"d:\!!\RotationMatrix.txt" ); // debug

            Point3D[] rotatedCirclePoints = this.RotatePoints(circlePoints, rotationMatrix);
            this.RotatedCirclePoints = rotatedCirclePoints;

            Point3D rotatedCirclePointsCentre = PlaneManager.RotateVector
                                                    (new RealVector(circlePointsCentre), rotationMatrix).ToPoint3D();

            Point3D[] correctedPoints =
                this.DisplacePoints(rotatedCirclePoints, rotatedCirclePointsCentre);

            int sizeX = interferograms[0].ColumnCount;
            int sizeY = interferograms[0].RowCount;

            RealMatrix phaseMatrix = this.CalculatePhaseMatrixByCirclePoints
                                         (correctedPoints, selectedImagePoints, sizeX, sizeY);

            InterferogramDecodingResult decodingResult = new InterferogramDecodingResult(phaseMatrix);

            return(decodingResult);
        }
Example #14
0
        //---------------------------------------------------------------------------------------------
        public InterferogramDecodingResult DecodeInterferogram(RealMatrix[] interferograms, BitMask2D bitMask)
        {
            //Выбранные точки изображения
            Point[] selectedImagePoints = bitMask.GetTruePoints();

            //Ортогональные векторы
            RealVector[] orthogonalVectors = this.GetOrthogonalVectors(interferograms, selectedImagePoints);

            RandomNumberGenerator randomNumberGenerator = new RandomNumberGenerator();

            int indexVector1 = randomNumberGenerator.GetNextInteger(orthogonalVectors.Length - 1);
            int indexVector2 = randomNumberGenerator.GetNextInteger(orthogonalVectors.Length - 1);

            RealVector vector1 = orthogonalVectors[indexVector1];
            RealVector vector2 = orthogonalVectors[indexVector2];

            RealVector normalY;
            RealVector normalX;

            RealMatrix rotMatrixY;
            RealMatrix rotMatrixX;
            RealMatrix rotMatrix;

            double alfa;
            double beta;

            normalY    = SpaceManager.GetVectorsCrossProduct(vector1, vector2);
            alfa       = Math.Atan(-(normalY[0] / normalY[2]));
            rotMatrixY = SpaceManager.GetRotationMatrixAroundAxisY(alfa);

            normalX    = SpaceManager.RotateVector(normalY, rotMatrixY);
            beta       = Math.Atan(-(normalX[1] / normalX[2]));
            rotMatrixX = SpaceManager.GetRotationMatrixAroundAxisX(beta);

            rotMatrix = rotMatrixX * rotMatrixY;

            RealVector[] rotVectors = SpaceManager.RotateVectors(orthogonalVectors, rotMatrix);
            Point3D[]    points3D   = this.GetPoints3D(rotVectors);

            //this.OrthogonalVectorsPoints = points3D;

            Point2D[] points2D = SpaceManager.GetProjectionXY(points3D);
            this.PlaneXYPoints = points2D;

            RealMatrix covariationMatrix = Statistician.GetCovariationMatrix(points2D);
            double     a11 = covariationMatrix[0, 0];
            double     a22 = covariationMatrix[1, 1];
            double     a12 = covariationMatrix[0, 1];

            double[] eigenValues   = MathHelper.GetEigenValuesMatrix2x2(covariationMatrix);
            double   minEigenValue = eigenValues.Min();
            double   maxEigenValue = eigenValues.Max();

            RealVector eigenVector1 = new RealVector
                                      (
                1, -((a11 + a12 - minEigenValue) / (a12 + a22 - minEigenValue))
                                      );

            RealVector eigenVector2 = new RealVector
                                      (
                1, -((a11 + a12 - maxEigenValue) / (a12 + a22 - maxEigenValue))
                                      );

            Complex complex = new Complex(eigenVector1[0], eigenVector1[1]);
            double  omega   = complex.Phase;

            double     a  = Math.Sqrt(minEigenValue);
            double     b  = Math.Sqrt(maxEigenValue);
            double     h  = a / Math.Sqrt(b * b - a * a);
            RealVector Nc = new RealVector(Math.Cos(omega), -Math.Sin(omega), h);

            Point3D[] circlePoints = this.GetCirclePoints(points3D, Nc);
            //this.OrthogonalVectorsPoints = circlePoints;

            RealVector[] circlePointsVectors = new RealVector[circlePoints.Length];
            for (int index = 0; index < circlePoints.Length; index++)
            {
                circlePointsVectors[index] = new RealVector(circlePoints[index]);
            }

            indexVector1 = randomNumberGenerator.GetNextInteger(circlePointsVectors.Length - 1);
            indexVector2 = randomNumberGenerator.GetNextInteger(circlePointsVectors.Length - 1);

            vector1 = circlePointsVectors[indexVector1];
            vector2 = circlePointsVectors[indexVector2];

            normalY    = SpaceManager.GetVectorsCrossProduct(vector1, vector2);
            alfa       = Math.Atan(-(normalY[0] / normalY[2]));
            rotMatrixY = SpaceManager.GetRotationMatrixAroundAxisY(alfa);

            normalX    = SpaceManager.RotateVector(normalY, rotMatrixY);
            beta       = Math.Atan(-(normalX[1] / normalX[2]));
            rotMatrixX = SpaceManager.GetRotationMatrixAroundAxisX(beta);

            rotMatrix = rotMatrixX * rotMatrixY;

            rotVectors = SpaceManager.RotateVectors(circlePointsVectors, rotMatrix);
            points3D   = this.GetPoints3D(rotVectors);
            points2D   = SpaceManager.GetProjectionXY(points3D);

            this.OrthogonalVectorsPoints = points3D;

            RealMatrix phaseMatrix = new RealMatrix(interferograms[0].RowCount, interferograms[0].ColumnCount);

            for (int index = 0; index < selectedImagePoints.Length; index++)
            {
                Point2D circlePoint = points2D[index];
                int     x           = selectedImagePoints[index].X;
                int     y           = selectedImagePoints[index].Y;

                phaseMatrix[y, x] = Math.PI + Math.Atan2(circlePoint.Y, circlePoint.X);
            }


            this.EigenVector1 =
                new RealVector(eigenVector1[0] / eigenVector1.Length, eigenVector1[1] / eigenVector1.Length);

            this.EigenVector2 =
                new RealVector(eigenVector2[0] / eigenVector2.Length, eigenVector2[1] / eigenVector2.Length);



            InterferogramDecodingResult decodingResult = new InterferogramDecodingResult(phaseMatrix);

            return(decodingResult);
        }
Example #15
0
 //------------------------------------------------------------------------------------------
 private Point3D[] GetSpatialPoints(RealMatrix[] interferograms, BitMask2D bitMask)
 {
     Point3D[] points =
         InterferometryHelper.GetSpatialPointsFromInterferograms(interferograms, bitMask);
     return(points);
 }