public CompositeFilter(ConvolutionSet2D set = null, bool isEditor = true)
        {
            InitializeComponent();

            // Selected effect
            _selectEffect = new DropShadowEffect()
            {
                Direction = 0,
                ShadowDepth = 0,
                BlurRadius = 40,
                Color = UtilityWPF.ColorFromHex("FFEB85"),
                Opacity = 1,
            };

            // Context Menu
            _kernelContextMenu = (ContextMenu)this.Resources["kernelContextMenu"];

            // Combobox
            foreach (SetOperationType operation in Enum.GetValues(typeof(SetOperationType)))
            {
                cboPostOperation.Items.Add(operation);
            }
            cboPostOperation.SelectedIndex = 0;

            // Load set passed in
            if (set != null)
            {
                foreach (var child in set.Convolutions)
                {
                    InsertKernel(child);
                }

                cboPostOperation.SelectedValue = set.OperationType;

                lblInstructions.Visibility = Visibility.Collapsed;

                if (!isEditor)
                {
                    btnSave.Visibility = Visibility.Collapsed;
                }
            }

            _initialized = true;

            RefreshStatsPanel();
        }
Example #2
0
        private static Border GetThumbnail_Set(ConvolutionSet2D kernel, int thumbSize, ContextMenu contextMenu, ConvolutionToolTipType tooltipType)
        {
            StackPanel children = new StackPanel()
            {
                Orientation = Orientation.Horizontal,
            };

            int childSize = Convert.ToInt32(thumbSize * .9);
            double secondChildShift = thumbSize * .33;

            foreach (ConvolutionBase2D child in kernel.Convolutions)
            {
                //NOTE: It doesn't work to apply skew transforms to a border that has children.  Instead, make a visual brush out of the child
                Border childCtrl = GetThumbnail(child, childSize, null);
                childCtrl.Margin = new Thickness(0);

                Border actualChild = new Border();
                actualChild.Background = new VisualBrush() { Visual = childCtrl };

                double width = 0;
                double height = 0;
                if (child is Convolution2D)
                {
                    width = ((FrameworkElement)childCtrl.Child).Width;
                    height = ((FrameworkElement)childCtrl.Child).Height;
                }
                else //if(child is ConvolutionSet2D)
                {
                    // There doesn't seem to be a way to get the child composite's size (it's NaN).  Maybe there's a way to force it to do layout?
                    width = thumbSize;
                    height = thumbSize;
                }

                actualChild.Width = width;
                actualChild.Height = height;

                //NOTE: I wanted a trapazoid transform, but that is impossible with a 3x3 matrix.  The only way would be to render in 3D
                TransformGroup tranform = new TransformGroup();
                tranform.Children.Add(new ScaleTransform(.75, 1));
                tranform.Children.Add(new SkewTransform(0, -30));
                actualChild.LayoutTransform = tranform;

                if (children.Children.Count > 0)
                {
                    actualChild.Margin = new Thickness(-secondChildShift, 0, 0, 0);
                }

                actualChild.IsHitTestVisible = false;       // setting this to false so that click events come from the returned border instead of the child

                children.Children.Add(actualChild);
            }

            Border border = new Border()
            {
                Child = children,

                Background = new SolidColorBrush(UtilityWPF.ColorFromHex("28F2B702")),
                BorderBrush = new SolidColorBrush(UtilityWPF.ColorFromHex("F5CC05")),
                BorderThickness = new Thickness(1),
                Margin = new Thickness(6),
                Padding = new Thickness(3),
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                ContextMenu = contextMenu,
                Tag = kernel,
            };

            border.ToolTip = GetToolTip(kernel, tooltipType);

            return border;
        }
Example #3
0
        private static Convolution2D Convolute_Set_MaxOf(Convolution2D image, ConvolutionSet2D kernel, string description)
        {
            #region validate
#if DEBUG
            if (kernel.OperationType != SetOperationType.MaxOf)
            {
                throw new ArgumentException("kernel must be MaxOf: " + kernel.OperationType.ToString());
            }
            else if (kernel.Convolutions.Length < 2)
            {
                throw new ArgumentException("MaxOf kernel set needs at least two children");
            }

            VectorInt firstReduce = kernel.Convolutions[0].GetReduction();

            for (int cntr = 1; cntr < kernel.Convolutions.Length; cntr++)
            {
                VectorInt nextReduce = kernel.Convolutions[cntr].GetReduction();

                if (firstReduce.X != nextReduce.X || firstReduce.Y != nextReduce.Y)
                {
                    throw new ArgumentException("When the operation is MaxOf, then all kernels must reduce the same amount");
                }
            }
#endif
            #endregion

            Convolution2D[] children = kernel.Convolutions.
                Select(o => Convolute(image, o, description)).
                ToArray();

            double[] retVal = new double[children[0].Values.Length];

            for (int index = 0; index < retVal.Length; index++)
            {
                double maxValue = 0;
                double maxAbsValue = 0;

                for (int cntr = 0; cntr < children.Length; cntr++)
                {
                    double abs = Math.Abs(children[cntr].Values[index]);
                    if (abs > maxAbsValue)
                    {
                        maxValue = children[cntr].Values[index];
                        maxAbsValue = abs;
                    }
                }

                retVal[index] = maxValue;
            }

            return new Convolution2D(retVal, children[0].Width, children[0].Height, image.IsNegPos || children.Any(o => o.IsNegPos), description: description);
        }
Example #4
0
        private static Convolution2D Convolute_Set(Convolution2D image, ConvolutionSet2D kernel, string description)
        {
            if (kernel.OperationType == SetOperationType.MaxOf)
            {
                // This is special, because it needs to convolute each child, then pick the values it wants to keep.  Also, each child will need to be the same size
                return Convolute_Set_MaxOf(image, kernel, description);
            }

            Convolution2D retVal = image;

            foreach (ConvolutionBase2D child in kernel.Convolutions)
            {
                retVal = Convolute(retVal, child, description);
            }

            if (kernel.OperationType == SetOperationType.Subtract)
            {
                retVal = Convolutions.Subtract(image, retVal, description);
            }

            return retVal;
        }
Example #5
0
        public static string GetToolTip(ConvolutionSet2D set, ConvolutionToolTipType type)
        {
            StringBuilder retVal = new StringBuilder(100);

            if (!string.IsNullOrEmpty(set.Description))
            {
                retVal.Append(set.Description);
            }

            if (type == ConvolutionToolTipType.Size || type == ConvolutionToolTipType.Size_MinMax && set.Convolutions != null && set.Convolutions.Length > 0)
            {
                if (retVal.Length > 0)
                {
                    retVal.AppendLine();
                }

                VectorInt size = set.GetSize();

                retVal.AppendFormat("{0}x{1}", size.X, size.Y);
            }

            if (type == ConvolutionToolTipType.Size_MinMax)
            {
                retVal.AppendLine();

                double min = set.EnumerateValues().Min();
                double max = set.EnumerateValues().Min();

                retVal.AppendFormat("min: {0}\r\nmax: {1}", min.ToStringSignificantDigits(2), max.ToStringSignificantDigits(2));
            }

            if (retVal.Length == 0)
            {
                return null;        // otherwise there will be a tiny empty tooltip
            }
            else
            {
                return retVal.ToString();
            }
        }
 private void Composite_SaveRequested(object sender, ConvolutionSet2D e)
 {
     try
     {
         AddKernel(e);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
        public MineralIdentifier()
        {
            InitializeComponent();

            this.Background = SystemColors.ControlBrush;

            #region Tab: Single Image

            // Camera Trackball
            _trackball = new TrackBallRoam(_camera);
            _trackball.EventSource = grdViewPort;		//NOTE:  If this control doesn't have a background color set, the trackball won't see events (I think transparent is ok, just not null)
            _trackball.AllowZoomOnMouseWheel = true;
            _trackball.Mappings.AddRange(TrackBallMapping.GetPrebuilt(TrackBallMapping.PrebuiltMapping.MouseComplete));
            _trackball.MouseWheelScale *= .1;
            //_trackball.GetOrbitRadius += new GetOrbitRadiusHandler(Trackball_GetOrbitRadius);

            // Mineral Types
            foreach (MineralType mineral in Enum.GetValues(typeof(MineralType)))
            {
                cboMineral.Items.Add(mineral);
            }
            cboMineral.SelectedIndex = 0;

            #endregion
            #region Tab: Training Data

            #region Mineral Types

            foreach (MineralType mineral in Enum.GetValues(typeof(MineralType)))
            {
                CheckBox mineralCheckbox = new CheckBox()
                {
                    Content = mineral.ToString(),
                    Tag = mineral,
                    Margin = new Thickness(2),
                };

                pnlMineralSelections.Children.Add(mineralCheckbox);
            }

            #endregion
            #region Convolutions

            // Gaussian Subtract
            AddKernel(new ConvolutionSet2D(new[] { Convolutions.GetGaussian(3, 1) }, SetOperationType.Subtract));

            // MaxAbs Sobel
            Convolution2D vert = Convolutions.GetEdge_Sobel(true);
            Convolution2D horz = Convolutions.GetEdge_Sobel(false);
            var singles = new[]
                {
                    new Convolution2D(vert.Values, vert.Width, vert.Height, vert.IsNegPos, 1),
                    new Convolution2D(horz.Values, horz.Width, horz.Height, horz.IsNegPos, 1),
                };

            ConvolutionSet2D set = new ConvolutionSet2D(singles, SetOperationType.MaxOf);
            AddKernel(set);

            #endregion

            #endregion

            _initialized = true;

            cboMineral_SelectionChanged(this, null);
            ResetCamera_Click(this, null);
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.SaveRequested == null)
                {
                    MessageBox.Show("There is no listener to the save event", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                if (_kernels.Count == 0)
                {
                    MessageBox.Show("You need to add kernels before saving", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                SetOperationType operation = (SetOperationType)cboPostOperation.SelectedValue;

                if (operation == SetOperationType.MaxOf)
                {
                    if (_kernels.Count < 2)
                    {
                        MessageBox.Show("MaxOf needs at least two children", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                        return;
                    }

                    VectorInt firstReduce = _kernels[0].GetReduction();

                    for (int cntr = 1; cntr < _kernels.Count; cntr++)
                    {
                        VectorInt nextReduce = _kernels[cntr].GetReduction();

                        if (firstReduce != nextReduce)
                        {
                            MessageBox.Show("When the operation is MaxOf, then all kernels must reduce the same amount", this.Title, MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                    }
                }

                ConvolutionSet2D set = new ConvolutionSet2D(_kernels.ToArray(), operation);

                this.SaveRequested(this, set);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }