Example #1
0
        /// <param name="scale">
        /// If the convolution is 0 to 255 or -255 to 255, the scale should be 1.
        /// If the convolution is 0 to 1 or -1 to 1, the scale should be 255.
        /// If the convolution's abs(max) is not 255 and you want the full range of colors, scale should be 255 / abs(max).
        /// </param>
        public static byte[][] GetColors(Convolution2D result, ConvolutionResultNegPosColoring negPosColoring, double scale = 1d, bool forcePos_WhiteBlack = false)
        {
            byte[][] retVal;

            if (result.IsNegPos)
            {
                switch (negPosColoring)
                {
                    case ConvolutionResultNegPosColoring.BlackWhite:
                        #region negpos - BlackWhite

                        retVal = result.Values.Select(o =>
                        {
                            double rgbDbl = Math.Round(Math.Abs(o * scale));

                            if (rgbDbl < 0) rgbDbl = 0;
                            else if (rgbDbl > 255) rgbDbl = 255;

                            byte rgb = Convert.ToByte(rgbDbl);
                            return new byte[] { 255, rgb, rgb, rgb };
                        }).
                        ToArray();

                        #endregion
                        break;

                    case ConvolutionResultNegPosColoring.WhiteBlack:
                        #region negpos - WhiteBlack

                        retVal = result.Values.Select(o =>
                        {
                            double rgbDbl = Math.Round(Math.Abs(o * scale));

                            if (rgbDbl < 0) rgbDbl = 0;
                            else if (rgbDbl > 255) rgbDbl = 255;

                            byte rgb = Convert.ToByte(255 - rgbDbl);
                            return new byte[] { 255, rgb, rgb, rgb };
                        }).
                        ToArray();

                        #endregion
                        break;

                    case ConvolutionResultNegPosColoring.Gray:
                        #region negpos - Gray

                        retVal = result.Values.Select(o =>
                        {
                            double offset = ((o * scale) / 255d) * 127d;
                            double rgbDbl = 128 + Math.Round(offset);

                            if (rgbDbl < 0) rgbDbl = 0;
                            else if (rgbDbl > 255) rgbDbl = 255;

                            byte rgb = Convert.ToByte(rgbDbl);
                            return new byte[] { 255, rgb, rgb, rgb };
                        }).
                        ToArray();

                        #endregion
                        break;

                    case ConvolutionResultNegPosColoring.RedBlue:
                        #region negpos RedBlue

                        byte[] back = new byte[] { 255, 255, 255, 255 };

                        retVal = result.Values.Select(o =>
                        {
                            double rgbDbl = Math.Round(o * scale);

                            if (rgbDbl < -255) rgbDbl = -255;
                            else if (rgbDbl > 255) rgbDbl = 255;

                            byte alpha = Convert.ToByte(Math.Abs(rgbDbl));

                            byte[] fore = null;
                            if (rgbDbl < 0)
                            {
                                fore = new byte[] { alpha, 255, 0, 0 };
                            }
                            else
                            {
                                fore = new byte[] { alpha, 0, 0, 255 };
                            }

                            return UtilityWPF.OverlayColors(new[] { back, fore });
                        }).
                        ToArray();

                        #endregion
                        break;

                    default:
                        throw new ApplicationException("Unknown ConvolutionResultNegPosColoring: " + negPosColoring.ToString());
                }
            }
            else
            {
                // Positive only
                if (forcePos_WhiteBlack || negPosColoring == ConvolutionResultNegPosColoring.WhiteBlack)
                {
                    #region 0 to 1 - whiteblack

                    retVal = result.Values.Select(o =>
                    {
                        double rgbDbl = Math.Round(o * scale);

                        if (rgbDbl < 0) rgbDbl = 0;
                        else if (rgbDbl > 255) rgbDbl = 255;

                        byte rgb = Convert.ToByte(255 - rgbDbl);
                        return new byte[] { 255, rgb, rgb, rgb };
                    }).
                    ToArray();

                    #endregion
                }
                else
                {
                    #region 0 to 1 - blackwhite

                    retVal = result.Values.Select(o =>
                    {
                        double rgbDbl = Math.Round(o * scale);

                        if (rgbDbl < 0) rgbDbl = 0;
                        else if (rgbDbl > 255) rgbDbl = 255;

                        byte rgb = Convert.ToByte(rgbDbl);
                        return new byte[] { 255, rgb, rgb, rgb };
                    }).
                    ToArray();

                    #endregion
                }
            }

            return retVal;
        }
Example #2
0
        public static BitmapSource GetBitmap(Convolution2D conv, ConvolutionResultNegPosColoring negPosColoring = ConvolutionResultNegPosColoring.RedBlue, double? absMaxValue = 255, bool forcePos_WhiteBlack = false)
        {
            double scale = GetColorScale(conv, absMaxValue);

            byte[][] colors = GetColors(conv, negPosColoring, forcePos_WhiteBlack: forcePos_WhiteBlack);

            return UtilityWPF.GetBitmap(colors, conv.Width, conv.Height);
        }
Example #3
0
        public static BitmapSource GetBitmap_Aliased(Convolution2D conv, int sizeMult = 20, ConvolutionResultNegPosColoring negPosColoring = ConvolutionResultNegPosColoring.RedBlue, double? absMaxValue = null, bool forcePos_WhiteBlack = false)
        {
            double scale = GetColorScale(conv, absMaxValue);

            byte[][] colors = GetColors(conv, negPosColoring, scale, forcePos_WhiteBlack);

            return UtilityWPF.GetBitmap_Aliased(colors, conv.Width, conv.Height, conv.Width * sizeMult, conv.Height * sizeMult);
        }
        private static void ApplyExtract_Draw_RightImage(Grid grid, Convolution2D imageConv, ConvolutionResultNegPosColoring edgeColor)
        {
            Image image = new Image()
            {
                Source = Convolutions.GetBitmap(imageConv, edgeColor),
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                ToolTip = string.Format("{0}x{1}", imageConv.Width, imageConv.Height),
            };

            Grid.SetColumn(image, 2);
            Grid.SetRow(image, 0);
            grid.Children.Add(image);
        }
        private static void ApplyExtract_Draw_LeftImage(Grid grid, Convolution2D imageConv, ConvolutionBase2D preFilter, ConvolutionResultNegPosColoring edgeColor)
        {
            string tooltip = string.Format("{0}x{1}", imageConv.Width, imageConv.Height);
            if (preFilter != null)
            {
                tooltip = preFilter.Description + "\r\n" + tooltip;
            }

            Image image = new Image()
            {
                Source = Convolutions.GetBitmap(imageConv, edgeColor),
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                ToolTip = tooltip,
            };

            Grid.SetColumn(image, 0);
            Grid.SetRow(image, 0);
            grid.Children.Add(image);
        }
        private async static void ApplyExtract_DoIt(Grid grid, FeatureRecognizer_Image image, FeatureRecognizer_Extract extract, FeatureRecognizer_Extract_Sub sub, ConvolutionResultNegPosColoring edgeColor, ContextMenu contextMenu)
        {
            // Source image
            BitmapSource bitmap;
            if (sub.InputWidth == image.Bitmap.PixelWidth && sub.InputHeight == image.Bitmap.PixelHeight)
            {
                bitmap = image.Bitmap;
            }
            else
            {
                bitmap = UtilityWPF.ResizeImage(image.Bitmap, sub.InputWidth, sub.InputHeight);
            }

            Convolution2D imageConv = ((BitmapCustomCachedBytes)UtilityWPF.ConvertToColorArray(bitmap, false, Colors.Transparent)).ToConvolution();

            // Convolute, look for matches
            var results = await ApplyExtract_DoIt_Task(imageConv, extract, sub);

            #region Show results

            // Left Image
            ApplyExtract_Draw_LeftImage(grid, results.ImageConv, extract.PreFilter, edgeColor);

            // Right Image
            if (results.Filtered != null)
            {
                ApplyExtract_Draw_RightImage(grid, results.Filtered, edgeColor);
            }

            // Matches
            if (results.Matches != null && results.Matches.Length > 0)
            {
                ApplyExtract_Draw_Matches(grid, results.Matches, results.Filtered.Size, sub, contextMenu);
            }

            #endregion
        }