Exemple #1
0
        public OrientationField(int[,] bytes, int blockSize, bool isPixelwise)
        {
            BlockSize = blockSize;
            int maxX = bytes.GetUpperBound(1) + 1;
            int maxY = bytes.GetUpperBound(0) + 1;

            double[,] filterX = new double[, ] {
                { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 }
            };
            double[,] filterY = new double[, ] {
                { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 }
            };
            double[,] doubleBytes = new double[maxY, maxX];
            for (int row = 0; row < maxY; row++)
            {
                for (int column = 0; column < maxX; column++)
                {
                    doubleBytes[row, column] = (double)bytes[row, column];
                }
            }
            // градиенты
            double[,] Gx = ConvolutionHelper.Convolve(doubleBytes, filterX);
            double[,] Gy = ConvolutionHelper.Convolve(doubleBytes, filterY);
            // разделение на блоки
            this._blocks = new Block[(int)Math.Floor((float)(maxY / BlockSize)), (int)Math.Floor((float)(maxX / BlockSize))];
            for (int row = 0; row < _blocks.GetUpperBound(0) + 1; row++)
            {
                for (int column = 0; column < _blocks.GetUpperBound(1) + 1; column++)
                {
                    _blocks[row, column] = new Block(BlockSize, Gx, Gy, row * BlockSize, column * BlockSize);
                }
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            string[]     pathes = Directory.GetFiles("C:\\Users\\Tanya\\Documents\\tests_data\\db");
            StreamWriter writer = new StreamWriter("C:\\Users\\Tanya\\Documents\\Results\\AlgorithmVSCOMEResult.txt", true);

            for (int i = 0; i < 3 /*pathes.GetLength(0)*/; i++)
            {
                Tuple <int, int> redPoint = ImageHelper.FindColorPoint(pathes[i]);
                double[,] imgBytes = ImageEnhancementHelper.EnhanceImage(ImageHelper.LoadImage(pathes[i]));

                double[,] orientationField         = OrientationFieldGenerator.GenerateOrientationField(imgBytes.Select2D(x => (int)x));
                Complex[,] complexOrientationField = orientationField.Select2D(x => (new Complex(Math.Cos(2 * x), Math.Sin(2 * x))));

                Complex[,] filter = Filter.GetFilter(orientationField);
                Complex[,] complexFilteredField = ConvolutionHelper.ComplexConvolve(complexOrientationField, filter);
                double[,] filteredField         = complexFilteredField.Select2D(x => x.Magnitude);

                VSCOME vscome = new VSCOME(orientationField, filteredField);

                double[,] vscomeValue = vscome.CalculateVscomeValue();
                Tuple <int, int> corePoint = KernelHelper.Max2dPosition(vscomeValue);

                writer.WriteLine(GetDistance(redPoint, corePoint));

                // ImageHelper.SaveArray(orientationField, "C:\\Users\\Tanya\\Documents\\Results\\orientationField.jpg");
                // ImageHelper.SaveArray(filteredField, "C:\\Users\\Tanya\\Documents\\Results\\filteredField.jpg");
                //ImageHelper.SaveArray(vscomeValue, "C:\\Users\\Tanya\\Documents\\Results\\vscomeValue_1.jpg");
            }

            writer.Close();
        }
        public PixelwiseOrientationField(int[,] bytes, int blockSize)
        {
            BlockSize = blockSize;
            int maxX = bytes.GetUpperBound(1) + 1;
            int maxY = bytes.GetUpperBound(0) + 1;

            double[,] filterX = new double[, ] {
                { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 }
            };
            double[,] filterY = new double[, ] {
                { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 }
            };
            double[,] doubleBytes = new double[maxY, maxX];
            for (int row = 0; row < maxY; row++)
            {
                for (int column = 0; column < maxX; column++)
                {
                    doubleBytes[row, column] = (double)bytes[row, column];
                }
            }
            // градиенты
            double[,] Gx = ConvolutionHelper.Convolve(doubleBytes, filterX);
            double[,] Gy = ConvolutionHelper.Convolve(doubleBytes, filterY);

            // рассчет направления для каждого пикселя
            _orientation = new double[maxY, maxX];
            // только один блок - персональный для каждого пикселя
            for (int centerRow = 0; centerRow < maxY; centerRow++)
            {
                for (int centerColumn = 0; centerColumn < maxX; centerColumn++)
                {
                    _block = new Block(_orientation, BlockSize, Gx, Gy, centerRow, centerColumn);
                }
            }
        }
        public double [] FilterGaussian(double [] F)
        {
            double[,] F_2D = F.Make2D(height, width);
            Filter f_gaus = new Filter(sizeFil, (sizeFil - 1) / 6);

            F = (ConvolutionHelper.Convolve(F_2D, f_gaus.Matrix, 1)).Make1D();
            return(F);
        }
Exemple #5
0
        private static Mat GaussBlur(Mat mat, double current, double newSigma)
        {
            var sigma = Math.Sqrt(MathHelper.Sqr(newSigma) - MathHelper.Sqr(current));

            return
                (sigma < 1e-6
                    ? mat.Clone()
                    : ConvolutionHelper.Convolution(mat, Gauss.GetKernel(sigma)));
        }
        public static Complex[,] EsimateH2(double[,] img, double Sigma1, double Sigma2)
        {
            var z       = GetSquaredDerectionField(img, Sigma1);
            var kernel2 = KernelHelper.MakeComplexKernel((x, y) => Gaussian.Gaussian2D(x, y, Sigma2) * x, (x, y) => Gaussian.Gaussian2D(x, y, Sigma2) * (-y), KernelHelper.GetKernelSizeForGaussianSigma(Sigma2));

            var I20 = ConvolutionHelper.ComplexConvolve(z, kernel2);

            return(I20);
        }
        public static double[,] Smoothing(double[,] img, double sigma)
        {
            var kernel = KernelHelper.MakeKernel((x, y) => Gaussian.Gaussian2D(x, y, sigma),
                                                 KernelHelper.GetKernelSizeForGaussianSigma(sigma));

            double[,] data = ConvolutionHelper.Convolve(img, kernel);

            return(data);
        }
Exemple #8
0
        public static double[,] Reduce2(double[,] source, double factor)
        {
            var smoothed = ConvolutionHelper.Convolve(source,
                                                      KernelHelper.MakeKernel(
                                                          (x, y) => Gaussian.Gaussian2D(x, y, factor / 2d * 0.75d), KernelHelper.GetKernelSizeForGaussianSigma(factor / 2d * 0.75d)));
            var result = new double[(int)(source.GetLength(0) / factor), (int)(source.GetLength(1) / factor)];

            Resize(smoothed, result, factor, (x, y) => Gaussian.Gaussian2D(x, y, factor / 2d * 0.75d));
            return(result);
        }
        public void TestConvolutionForEvenSizedFilters()
        {
            var img = ImageHelper.LoadImage <double>(Resources.SampleFinger);

            var kernel = KernelHelper.MakeKernel((x, y) => 1, 4);

            var result = ConvolutionHelper.Convolve(img, kernel);

            ImageHelper.SaveArrayAndOpen(result);
        }
        public static Complex[,] GetSquaredDerectionField(double[,] img, double Sigma1)
        {
            var kernelX = KernelHelper.MakeKernel((x, y) => Gaussian.Gaussian2D(x, y, Sigma1) * x, KernelHelper.GetKernelSizeForGaussianSigma(Sigma1));
            var resultX = ConvolutionHelper.Convolve(img, kernelX);
            var kernelY = KernelHelper.MakeKernel((x, y) => Gaussian.Gaussian2D(x, y, Sigma1) * -y, KernelHelper.GetKernelSizeForGaussianSigma(Sigma1));
            var resultY = ConvolutionHelper.Convolve(img, kernelY);

            var preZ = KernelHelper.MakeComplexFromDouble(resultX, resultY);

            var z = preZ.Select2D(x => x * x);

            return(z);
        }
        public static Complex[,] EstimatePS(double[,] img, double Sigma1, double Sigma2)
        {
            var z = GetSquaredDerectionField(img, Sigma1);

            var kernel2 =
                KernelHelper.MakeComplexKernel((x, y) => Gaussian.Gaussian2D(x, y, Sigma2) * x / (x == 0 && y == 0 ? 1 : Math.Sqrt(x * x + y * y)),
                                               (x, y) => Gaussian.Gaussian2D(x, y, Sigma2) * y / (x == 0 && y == 0 ? 1 : Math.Sqrt(x * x + y * y)),
                                               KernelHelper.GetKernelSizeForGaussianSigma(Sigma2));

            var I20 = ConvolutionHelper.ComplexConvolve(z, kernel2);

            return(I20);
        }
Exemple #12
0
        public (I3DTensor Result, IReadOnlyList <(object X, object Y)> Index) MaxPool(
            int filterWidth,
            int filterHeight,
            int stride,
            bool calculateIndex)
        {
            var newColumns = (ColumnCount - filterWidth) / stride + 1;
            var newRows    = (RowCount - filterHeight) / stride + 1;
            var matrixList = new List <CpuMatrix>();
            var indexList  = calculateIndex ? new List <(object X, object Y)>() : null;
            var posList    = ConvolutionHelper.Default(ColumnCount, RowCount, filterWidth, filterHeight, stride);

            for (var k = 0; k < Depth; k++)
            {
                var matrix = _data[k];
                var xIndex = new int[newColumns * newRows];
                var yIndex = new int[newColumns * newRows];
                var layer  = new CpuMatrix(DenseMatrix.Create(newRows, newColumns, 0f));

                foreach (var item in posList)
                {
                    var first   = item.First();
                    var targetX = first.X / stride;
                    var targetY = first.Y / stride;
                    var maxVal  = float.MinValue;
                    var bestX   = -1;
                    var bestY   = -1;
                    foreach (var pos in item)
                    {
                        var val = matrix[pos.Y, pos.X];
                        if (val > maxVal || bestX == -1)
                        {
                            bestX  = pos.X;
                            bestY  = pos.Y;
                            maxVal = val;
                        }
                    }
                    var index = targetX * newRows + targetY;
                    xIndex[index]           = bestX;
                    yIndex[index]           = bestY;
                    layer[targetY, targetX] = maxVal;
                }
                matrixList.Add(layer);
                indexList?.Add((xIndex, yIndex));
            }
            return(new Cpu3DTensor(matrixList), indexList);
        }
Exemple #13
0
        public static double FindAt(Mat image, int radius, int x, int y)
        {
            var gauss = Gauss.GetFullKernel(radius / 3.0);

            double a = 0, b = 0, c = 0;

            for (var u = -radius; u <= radius; u++)
            {
                for (var v = -radius; v <= radius; v++)
                {
                    var ix         = ConvolutionHelper.ConvolveCell(image, SobelHelper.SobelKernelX, x + u, y + v);
                    var iy         = ConvolutionHelper.ConvolveCell(image, SobelHelper.SobelKernelY, x + u, y + v);
                    var gaussPoint = gauss.GetPixel(u + radius, v + radius, BorderWrapType.Copy);
                    a += gaussPoint * ix * ix;
                    b += gaussPoint * ix * iy;
                    c += gaussPoint * iy * iy;
                }
            }

            return(LambdaMin(a, b, c));
        }
Exemple #14
0
        public IMatrix Im2Col(int filterWidth, int filterHeight, int stride)
        {
            var rowList      = new List <float[]>();
            var convolutions = ConvolutionHelper.Default(ColumnCount, RowCount, filterWidth, filterHeight, stride);

            foreach (var filter in convolutions)
            {
                var row   = new float[filter.Length * Depth];
                int index = 0;
                for (var k = 0; k < Depth; k++)
                {
                    foreach (var item in filter)
                    {
                        row[index++] = this[item.Y, item.X, k];
                    }
                }
                rowList.Add(row);
            }
            var firstRow = rowList.First();

            return(new CpuMatrix(DenseMatrix.Create(rowList.Count, firstRow.Length, (i, j) => rowList[i][j])));
        }
        public static Complex[,] EstimateLS(double[,] l1, double Sigma1, double Sigma2)
        {
            var kernelX = KernelHelper.MakeKernel((x, y) => Gaussian.Gaussian2D(x, y, Sigma1) * x, KernelHelper.GetKernelSizeForGaussianSigma(Sigma1));
            var resultX = ConvolutionHelper.Convolve(l1, kernelX);
            var kernelY = KernelHelper.MakeKernel((x, y) => Gaussian.Gaussian2D(x, y, Sigma1) * -y, KernelHelper.GetKernelSizeForGaussianSigma(Sigma1));
            var resultY = ConvolutionHelper.Convolve(l1, kernelY);


            var preZ = KernelHelper.MakeComplexFromDouble(resultX, resultY);

            var z = preZ.Select2D(x => x * x);

            var kernel2 = KernelHelper.MakeComplexKernel((x, y) => Gaussian.Gaussian2D(x, y, Sigma2), (x, y) => 0,
                                                         KernelHelper.GetKernelSizeForGaussianSigma(Sigma2));

            var I20 = ConvolutionHelper.ComplexConvolve(z, kernel2);

            var I11 = ConvolutionHelper.Convolve(z.Select2D(x => x.Magnitude), kernel2.Select2D(x => x.Real));

            Complex[,] LS = KernelHelper.Zip2D(I20, I11, (x, y) => x / y);

            return(LS);
        }
Exemple #16
0
        public static double[,] GenerateOrientationField(double[,] bytes)
        {
            double size = 1;

            double avSigma = 5;

            var kernelAv = KernelHelper.MakeKernel((x, y) => Gaussian.Gaussian2D(x, y, avSigma),
                                                   KernelHelper.GetKernelSizeForGaussianSigma(avSigma));

            var kernelX = KernelHelper.MakeKernel((x, y) => Gaussian.Gaussian2D(x, y, size) * x,
                                                  KernelHelper.GetKernelSizeForGaussianSigma(size));

            var dx = ConvolutionHelper.Convolve(bytes, kernelX);

            var kernelY = KernelHelper.MakeKernel((x, y) => Gaussian.Gaussian2D(x, y, size) * -y,
                                                  KernelHelper.GetKernelSizeForGaussianSigma(size));

            var dy = ConvolutionHelper.Convolve(bytes, kernelY);

            var Gxx = dx.Select2D(x => x * x);

            var Gxy = dx.Select2D((x, row, column) => x * dy[row, column]);

            var Gyy = dy.Select2D(x => x * x);

            Gxx = ConvolutionHelper.Convolve(Gxx, kernelAv);
            Gxy = ConvolutionHelper.Convolve(Gxy, kernelAv);
            Gyy = ConvolutionHelper.Convolve(Gyy, kernelAv);

            var angles = Gxx.Select2D((gxx, row, column) => 0.5 * Math.Atan2(2.0 * Gxy[row, column], gxx - Gyy[row, column]));

            angles = angles.Select2D(angle => angle <= 0 ? angle + Math.PI / 2 : angle - Math.PI / 2);
            //ImageHelper.SaveFieldAbove(bytes, angles, "C:\\temp\\orField.png");
            //ImageHelper.SaveArray(angles, "C:\\temp\\angles.png");
            return(angles);
        }
        public double[,] GaussFilter()
        {
            double[,] filterX = new double[, ] {
                { -3, 0, 3 }, { -10, 0, 10 }, { -3, 0, 3 }
            };
            double[,] filterY = new double[, ] {
                { 3, 10, 3 }, { 0, 0, 0 }, { -3, -10, -3 }
            };

            double[,] Gx = ConvolutionHelper.Convolve(pic, filterX);
            double[,] Gy = ConvolutionHelper.Convolve(pic, filterY);

            double[,] X2 = new double[height, width];
            double[,] Y2 = new double[height, width];
            double[,] XY = new double[height, width];

            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    X2[i, j] = Gx[i, j] * Gx[i, j];
                    Y2[i, j] = Gy[i, j] * Gy[i, j];
                    XY[i, j] = Gx[i, j] * Gy[i, j];
                }
            }

            double sigma = 0.5;
            int    size  = KernelHelper.GetKernelSizeForGaussianSigma(sigma);

            double[,] w = KernelHelper.MakeKernel((x, y) => Gaussian.Gaussian2D(x, y, sigma), size); //Gaussian filter

            double[,] A = ConvolutionHelper.Convolve(X2, w);
            double[,] B = ConvolutionHelper.Convolve(Y2, w);
            double[,] C = ConvolutionHelper.Convolve(XY, w);

            double[,] R = new double[height, width];

            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    double[,] M = new double[2, 2];
                    M[0, 0]     = A[i, j];
                    M[0, 1]     = C[i, j];
                    M[1, 0]     = C[i, j];
                    M[1, 1]     = B[i, j];

                    double Tr  = M[0, 0] + M[1, 1];
                    double Det = M[0, 0] * M[1, 1] - M[1, 0] * M[0, 1];

                    R[i, j] = Det / Tr;
                }
            }

            double[,] matrix = new double[width, height];
            for (int i = 0; i < width; ++i)
            {
                for (int j = 0; j < height; ++j)
                {
                    matrix[i, j] = R[height - 1 - j, i];
                }
            }

            return(matrix);
        }