Beispiel #1
0
        //static readonly float[,] sobelXKern = new float[3, 3]
        //{
        //    { -1,0,1 },
        //    { -2,0,2 },
        //    { -1,0,1 }
        //};

        //static readonly float[,] sobelYKern = new float[3, 3]
        //{
        //    { -1,-2,-1 },
        //    { 0, 0, 0 },
        //    { 1, 2, 1 }
        //};

        #region Sobel
        public static void CalculateHorizontalSobel(Index2 index, ArrayView3D <short> result, ArrayView3D <short> image)
        {
            if (index.X == 0 || index.Y == 0 ||
                index.X == image.Width - 1 || index.Y == image.Height - 1)
            {
                for (int i = 0; i < image.Depth; i++)
                {
                    result[index.X, index.Y, i] = 32000;
                }
                return;
            }

            for (int i = 0; i < image.Depth; i++)
            {
                var   kern = image.GetSliceView(i).GetSubView(new Index2(index.X - 1, index.Y - 1), new Index2(3, 3));
                short val  = 0;

                val += (short)(kern[0, 0] * -1);
                val += (short)(kern[2, 0] * 1);
                val += (short)(kern[0, 1] * -2);
                val += (short)(kern[2, 1] * 2);
                val += (short)(kern[0, 2] * -1);
                val += (short)(kern[2, 2] * 1);

                result[index.X, index.Y, i] = val;
            }
        }
        public static uint[] AccumulateEdges(ArrayView3D <short> imageShortView, ArrayView3D <byte> imageByteView, byte[] cannyData, byte pearsonThreshold, short maxValue)
        {
            uint[] result;

            var pearsonData     = CalculationWrappers.CalculatePearsonCorrelation(imageByteView, 0, pearsonThreshold).rawPicture;
            var signLengthByte  = CalculateSignatureLengthDerivative(imageByteView, true).rawPicture;
            var signLengthShort = CalculateSignatureLengthDerivative(imageShortView, true, maxValue).rawPicture;

            var picIndex = imageByteView.GetSliceView(0).Extent;

            using (var pearsBuffer = GpuContext.Instance.Accelerator.Allocate <byte>(picIndex))
                using (var signLengthByteBuffer = GpuContext.Instance.Accelerator.Allocate <byte>(picIndex))
                    using (var signLengthShortBuffer = GpuContext.Instance.Accelerator.Allocate <byte>(picIndex))
                        using (var cannyBuffer = GpuContext.Instance.Accelerator.Allocate <byte>(picIndex))
                            using (var imgBuffer = GpuContext.Instance.Accelerator.Allocate <uint>(picIndex))
                            {
                                pearsBuffer.CopyFrom(pearsonData, 0, Index2.Zero, picIndex.Size);
                                signLengthByteBuffer.CopyFrom(signLengthByte, 0, Index2.Zero, picIndex.Size);
                                signLengthShortBuffer.CopyFrom(signLengthShort, 0, Index2.Zero, picIndex.Size);
                                cannyBuffer.CopyFrom(cannyData, 0, Index2.Zero, picIndex.Size);

                                _accumulateEdgesKernel(picIndex, imgBuffer.View, pearsBuffer.View, cannyBuffer.View, signLengthByteBuffer.View, signLengthShortBuffer.View);
                                GpuContext.Instance.Accelerator.Synchronize();

                                result = imgBuffer.GetAsArray();
                            }

            return(result);
        }
        public static PicturesData CalculateSignatureLengthDerivative(ArrayView3D <byte> imageView, bool normalize)
        {
            PicturesData result   = new PicturesData();
            var          slice    = imageView.GetSliceView(0);
            var          picIndex = new Index3(slice.Extent.X, slice.Extent.Y, 3);

            using (var calcBuffer = GpuContext.Instance.Accelerator.Allocate <float>(picIndex))
                using (var byteBuf = GpuContext.Instance.Accelerator.Allocate <byte>(picIndex))
                    using (var imageBuffer = GpuContext.Instance.Accelerator.Allocate <uint>(picIndex))
                    {
                        if (normalize)
                        {
                            BCalculateSignatureLengthDerivativeWithNormalizationKernel(slice.Extent, calcBuffer.View, imageView);
                        }
                        else
                        {
                            BCalculateSignatureLengthDerivativeKernel(slice.Extent, calcBuffer.View, imageView);
                        }
                        GpuContext.Instance.Accelerator.Synchronize();

                        var array    = calcBuffer.GetAsArray();
                        var maxBand1 = array.Take(slice.Extent.Size).Max();
                        var maxBand2 = array.Skip(slice.Extent.Size).Take(slice.Extent.Size).Max();
                        //var maxBand3 = array.Skip(slice.Extent.Size * 2).Max();
                        var maxBand3 = XMath.Max(maxBand1, maxBand2);

                        NormalizeLengthMapKernel(slice.Extent, calcBuffer, 0, maxBand1); GpuContext.Instance.Accelerator.Synchronize();
                        NormalizeLengthMapKernel(slice.Extent, calcBuffer, 1, maxBand2); GpuContext.Instance.Accelerator.Synchronize();
                        NormalizeLengthMapKernel(slice.Extent, calcBuffer, 2, maxBand3); GpuContext.Instance.Accelerator.Synchronize();

                        FloatToByteKernel(calcBuffer.Extent.Size, calcBuffer.AsLinearView(), byteBuf.AsLinearView());
                        GpuContext.Instance.Accelerator.Synchronize();

                        result.rawPicture = byteBuf.GetAsArray().Skip(slice.Extent.Size * 2).ToArray();


                        PinConvertFromByteKernel(slice.Extent.Size, imageBuffer.GetSliceView(0).AsLinearView(), byteBuf.GetSliceView(0).AsLinearView(), 1.0, 0);
                        PinConvertFromByteKernel(slice.Extent.Size, imageBuffer.GetSliceView(1).AsLinearView(), byteBuf.GetSliceView(1).AsLinearView(), 1.0, 0);
                        PinConvertFromByteKernel(slice.Extent.Size, imageBuffer.GetSliceView(2).AsLinearView(), byteBuf.GetSliceView(2).AsLinearView(), 1.0, 0);
                        GpuContext.Instance.Accelerator.Synchronize();

                        var img  = imageBuffer.GetAsArray();
                        var size = slice.Extent.Size;

                        var buf = new uint[size];
                        Array.Copy(img, 0, buf, 0, size);
                        result.xPicture = buf;

                        buf = new uint[size];
                        Array.Copy(img, size, buf, 0, size);
                        result.yPicture = buf;

                        buf = new uint[size];
                        Array.Copy(img, size * 2, buf, 0, size);
                        result.xyPicture = buf;
                    }

            return(result);
        }
        public static CorrelationData CalculatePearsonCorrelation(ArrayView3D <byte> imageView, byte lowThreshold = 0, byte highThreshold = 0)
        {
            var result = new CorrelationData();

            var slice      = imageView.GetSliceView(0);
            var comboSlice = new Index3(slice.Width, slice.Height, 3);

            using (var calcBuf = GpuContext.Instance.Accelerator.Allocate <float>(comboSlice))
                using (var byteBuf = GpuContext.Instance.Accelerator.Allocate <byte>(comboSlice))
                    using (var imageBuf = GpuContext.Instance.Accelerator.Allocate <uint>(comboSlice))
                    {
                        PearsonKernel(slice.Extent, calcBuf.View, imageView);
                        GpuContext.Instance.Accelerator.Synchronize();

                        var maxCorrelation = calcBuf.GetAsArray().Max();
                        NormalizeKernel(calcBuf.Extent.Size, calcBuf.AsLinearView(), maxCorrelation);
                        GpuContext.Instance.Accelerator.Synchronize();

                        var maxGradientValue = calcBuf.GetAsArray().Skip(slice.Extent.Size).Take(slice.Extent.Size).Max();
                        NormalizeKernel(slice.Extent.Size, calcBuf.GetSliceView(1).AsLinearView(), maxGradientValue);
                        GpuContext.Instance.Accelerator.Synchronize();

                        FloatToByteKernel(calcBuf.Extent.Size, calcBuf.AsLinearView(), byteBuf.AsLinearView());
                        GpuContext.Instance.Accelerator.Synchronize();

                        if (highThreshold != 0 || lowThreshold != 0)
                        {
                            ThresholdingKernel(byteBuf.Extent.Size, byteBuf.AsLinearView(), lowThreshold, highThreshold);
                            GpuContext.Instance.Accelerator.Synchronize();
                        }

                        result.rawPicture = byteBuf.GetAsArray().Skip(slice.Extent.Size * 2).ToArray();

                        PiсConvertFromByteKernel(slice.Extent.Size, imageBuf.GetSliceView(0).AsLinearView(), byteBuf.GetSliceView(0).AsLinearView(), 1.0, 0);
                        PiсConvertFromByteKernel(slice.Extent.Size, imageBuf.GetSliceView(1).AsLinearView(), byteBuf.GetSliceView(1).AsLinearView(), 1.0, 0);
                        PiсConvertFromByteKernel(slice.Extent.Size, imageBuf.GetSliceView(2).AsLinearView(), byteBuf.GetSliceView(2).AsLinearView(), 1.0, 0);
                        GpuContext.Instance.Accelerator.Synchronize();

                        var img  = imageBuf.GetAsArray();
                        var size = slice.Extent.Size;

                        var buf = new uint[size];
                        Array.Copy(img, 0, buf, 0, size);
                        result.xCorrelation = buf;

                        buf = new uint[size];
                        Array.Copy(img, size, buf, 0, size);
                        result.yCorrelation = buf;

                        buf = new uint[size];
                        Array.Copy(img, size * 2, buf, 0, size);
                        result.xyCorrelation = buf;
                    }

            return(result);
        }
        public static uint[] PictureConvertion(ArrayView3D <byte> imageView, int band, double mult, short min)
        {
            uint[] result;
            using (var bufOut = GpuContext.Instance.Accelerator.Allocate <uint>(imageView.Width * imageView.Height))
            {
                PiсConvertFromByteKernel(bufOut.Length, bufOut.View, imageView.GetSliceView(band - 1).AsLinearView(), mult, min);
                GpuContext.Instance.Accelerator.Synchronize();
                result = bufOut.GetAsArray();
            }

            return(result);
        }
        public static uint[] CalculatePseudoColor(ArrayView3D <short> imageView, int redBand, int greenBand, int blueBand, short max)
        {
            uint[] result;
            var    sliceIndex = imageView.GetSliceView(0).Extent;

            using (var imageBuf = GpuContext.Instance.Accelerator.Allocate <uint>(sliceIndex))
            {
                CalPseudoColorKernel(sliceIndex, imageBuf.View, imageView, redBand, greenBand, blueBand, max);
                GpuContext.Instance.Accelerator.Synchronize();

                result = imageBuf.GetAsArray();
            }

            return(result);
        }
Beispiel #7
0
        public static void CalculateBrightnessStats(Index index, ArrayView3D <short> input, ArrayView <double> meanBrightness, ArrayView <short> maxBrightness, ArrayView <double> standartDeviation)
        {
            long sum = 0;

            short max = 0;

            var band = input.GetSliceView(index).AsLinearView();

            for (int i = 0; i < band.Length; i++)
            {
                var val = band[i];
                if (val < 0)
                {
                    val = 0;
                }
                //for deviation and mean sum
                sum += val;
                if (val > max)
                {
                    max = val;
                }
            }

            double mean = sum / (double)band.Length;

            meanBrightness[index] = mean;
            maxBrightness[index]  = max;

            double dividend = 0;

            for (int i = 0; i < band.Length; i++)
            {
                var val = band[i];
                if (val < 0)
                {
                    val = 0;
                }

                dividend += XMath.Pow(XMath.Abs(val - mean), 2);
            }

            standartDeviation[index] = XMath.Sqrt(dividend / band.Length);
        }
        public static uint[] CalcScanlineImage(ArrayView3D <short> imageView, int band, int row, int column)
        {
            uint[] data;

            Index2 index = new Index2(imageView.Extent.X + imageView.Extent.Z, imageView.Extent.Y + imageView.Extent.Z);
            var    slice = imageView.GetSliceView(0).Extent;

            using (var imageBuf = GpuContext.Instance.Accelerator.Allocate <uint>(index))
            {
                CalcSliceBandKernel(slice, imageBuf, imageView, band);
                GpuContext.Instance.Accelerator.Synchronize();

                CalcSlicesKernel(imageView.Extent.Z, imageBuf, imageView, row, column);
                GpuContext.Instance.Accelerator.Synchronize();

                data = imageBuf.GetAsArray();
            }

            return(data);
        }
Beispiel #9
0
        public static void StandardDeviation(Index index, ArrayView <double> result, ArrayView3D <short> input)
        {
            long sum = 0;

            var band = input.GetSliceView(index).AsLinearView();

            for (int i = 0; i < band.Length; i++)
            {
                sum += band[i];
            }

            double mean     = sum / band.Length;
            double dividend = 0;

            for (int i = 0; i < band.Length; i++)
            {
                dividend += XMath.Pow(band[i] - mean, 2);
            }

            result[index] = XMath.Sqrt(dividend / band.Length);
        }
        public static (MemoryBuffer3D <byte>, uint[]) ConvertToByteRepresentation(ArrayView3D <short> imageView, short maxValue)
        {
            var outputBuf           = GpuContext.Instance.Accelerator.Allocate <byte>(imageView.Extent);
            var convertToByteKernel = GpuContext.Instance.Accelerator.LoadAutoGroupedStreamKernel <Index3, ArrayView3D <byte>, ArrayView3D <short>, short>(Kernels.ConvertToByte);

            convertToByteKernel(imageView.Extent, outputBuf.View, imageView, maxValue);
            GpuContext.Instance.Accelerator.Synchronize();
            uint[] buff;

            var slice = outputBuf.GetSliceView(30);

            using (var imageBuf = GpuContext.Instance.Accelerator.Allocate <uint>(slice.Extent))
            {
                PiсConvertFromByteKernel(imageView.GetSliceView(0).AsLinearView().Extent, imageBuf.AsLinearView(), slice.AsLinearView(), 1.0, 0);
                GpuContext.Instance.Accelerator.Synchronize();

                buff = imageBuf.GetAsArray();
            }

            return(outputBuf, buff);
        }