Example #1
0
        public static double[,] GetGaussian2D(int size, double alpha = 2.5)
        {
            var gaus1D = new double[size];
            var mu     = size / 2;
            var b      = (-alpha * alpha) / (size * mu);

            for (var i = 0; i < size; i++)
            {
                gaus1D[i] = Math.Exp(b * MathCV.Pow2(i - mu));
            }

            var result = new double[size, size];

            for (var j = 0; j < size; j++)
            {
                var val = gaus1D[j];
                for (var i = 0; i < size; i++)
                {
                    result[i, j] = gaus1D[i] * val;
                }
            }

            return(result);
        }
Example #2
0
        /// <summary>
        ///   Codes a image using the light field based on the given parameters.
        /// </summary>
        /// <param name="parameter">
        ///   <see cref="LightfieldParam" />
        /// </param>
        /// <returns></returns>
        public unsafe UnmanagedImage GetUnmanagedImage(LightfieldParam parameter)
        {
            var lightFieldSize = ViewRows * ViewColumns;

            if (lightFieldSize == 0)
            {
                return(null);
            }

            var doField    = Math.Min(MaximumDepthOfField(parameter.ViewPoint), parameter.DepthOfField);
            var focusPoint = parameter.ViewPoint;

            if (focusPoint == default)
            {
                focusPoint = ViewPoint;
            }

            var doFocus = parameter.DepthOfFocus;
            var x0Focus = Math.Min(Math.Max(focusPoint.X, doField), ViewColumns - doField - 1);
            var y0Focus = Math.Min(Math.Max(focusPoint.Y, doField), ViewRows - doField - 1);

            var nLightFields       = MathCV.Pow2(doField + doField + 1);
            var invNormLightFields = 1 / (float)nLightFields;

            var img = DoGetUnmanImage(x0Focus, y0Focus);

            if (img == null)
            {
                return(null);
            }

            var format = img.PixelFormat;

            var h  = img.Height;
            var s  = img.Stride;
            var n  = s * h;
            var dt = (byte *)img.ImageData;

            Preferences.Supported.CheckFormat(format);

            var dxFocus = doFocus;
            var dyFocus = parameter.InvertFocus ? -doFocus : doFocus;

            var sumData = new float[img.Stride * h];

            for (var i = 0; i < sumData.Length; i++)
            {
                sumData[i] = dt[i] * invNormLightFields;
            }

            var result = UnmanagedImage.Create(img.Width, img.Height, format);

            dt = (byte *)result.ImageData;

            var xMax = x0Focus + doField;
            var yMin = y0Focus - doField;
            var yMax = y0Focus + doField;

            for (var x = x0Focus - doField; x <= xMax; x++)
            {
                for (var y = yMin; y <= yMax; y++)
                {
                    if (x == x0Focus &&
                        y == y0Focus)
                    {
                        continue;
                    }

                    var view = DoGetUnmanImage(x, y);
                    if (view == null)
                    {
                        return(null);
                    }

                    if (view.PixelFormat != format)
                    {
                        throw new BadImageFormatException(nameof(view));
                    }

                    try {
                        IntegrateImage(
                            view,
                            sumData,
                            dxFocus * (x - x0Focus),
                            dyFocus * (y - y0Focus),
                            invNormLightFields
                            );
                    } catch (OperationCanceledException) {
                        return(null);
                    }
                }
            }

            for (var i = 0; i < n; i++)
            {
                dt[i] = sumData[i].ClampToByte();
            }

            //      throw new NotSupportedException("Not tested use of (float).LimitToByte(), or is (float).ToBase256() correct?");
            return(result);
        }