示例#1
0
        private void ImportTrainingSet()
        {
            // Create an instance of the open file dialog box.
            var openFileDialog = new OpenFileDialog
            {
                Filter      = @"Txt Files (.txt)|*.txt",
                FilterIndex = 1
            };

            // Call the ShowDialog method to show the dialog box.
            var userClickedOk = openFileDialog.ShowDialog();

            // Process input if the user clicked OK.
            if (userClickedOk == DialogResult.OK)
            {
                FeaturesViewModel.RemoveAllFeatures();

                using (var file = new StreamReader(openFileDialog.FileName))
                {
                    int numLandcoverTypes = int.Parse(file.ReadLine());
                    Dictionary <int, LandcoverTypeViewModel> landCoverTypes = new Dictionary <int, LandcoverTypeViewModel>();
                    for (int i = 0; i < numLandcoverTypes; ++i)
                    {
                        int    id    = int.Parse(file.ReadLine());
                        string name  = file.ReadLine();
                        Color  color = (Color)ColorConverter.ConvertFromString(file.ReadLine());
                        LandcoverTypeViewModel landcoverType = new LandcoverTypeViewModel(id, name, color);
                        landCoverTypes.Add(id, landcoverType);
                    }


                    int numLayers = int.Parse(file.ReadLine());
                    var layers    = new List <CreateLayerViewModel>();
                    for (int i = 0; i < numLayers; ++i)
                    {
                        string        path = file.ReadLine();
                        bool          contrastEnhancement = bool.Parse(file.ReadLine());
                        SatelliteType satelliteType       = (SatelliteType)Enum.Parse(typeof(SatelliteType), file.ReadLine());
                        bool          isRed            = bool.Parse(file.ReadLine());
                        bool          isGreen          = bool.Parse(file.ReadLine());
                        bool          isBlue           = bool.Parse(file.ReadLine());
                        double        minCutPercentage = double.Parse(file.ReadLine());
                        double        maxCutPercentage = double.Parse(file.ReadLine());
                        layers.Add(new CreateLayerViewModel(path, contrastEnhancement, satelliteType, isRed, isGreen, isBlue, minCutPercentage, maxCutPercentage));;
                    }

                    var missingLayers = layers.Where(s => _mainWindowViewModel.Layers.All(b => b.Path != s.Path)).ToList();

                    // Check if some bands have to be added.
                    if (missingLayers.Count > 0)
                    {
                        var dialog = new AddLayersDialog();

                        if (dialog.ShowImportMissingBandsDialog(missingLayers) == true && dialog.DialogViewModel.Layers.Count > 0)
                        {
                            var loadBandsTask = new LoadBandsTask(_mainWindowViewModel, dialog.DialogViewModel);
                            _mainWindowViewModel.ExecuteLongRunningTask(loadBandsTask);
                        }
                        else
                        {
                            // Abort import
                            return;
                        }
                    }

                    _mainWindowViewModel.LandcoverTypes = landCoverTypes.ToImmutableDictionary();
                    _mainWindowViewModel.SelectedLandCoverTypeViewModel = _mainWindowViewModel.LandcoverTypes.Values.FirstOrDefault();

                    string featureLine;

                    while ((featureLine = file.ReadLine()) != null)
                    {
                        string[] positionTypeIntensities = featureLine.Split(' ');
                        string   position      = positionTypeIntensities[0];
                        string[] coordinates   = position.Split(',');
                        var      landCoverType = _mainWindowViewModel.LandcoverTypes.Values.ToList().FindIndex(t => t.Id == int.Parse(positionTypeIntensities[1]));
                        var      intensities   = positionTypeIntensities[2].Split(';').Select(ushort.Parse).ToArray();

                        FeaturesViewModel.AddFeature(new ClassifiedFeatureVectorViewModel(new ClassifiedFeatureVector(landCoverType, new FeatureVector(intensities), new Point(double.Parse(coordinates[0]), double.Parse(coordinates[1])))));
                    }
                }
            }
        }
        private void ApplyMajorityFilter()
        {
            NotBlocking = false;


            var filtered = new int[_predictionHeight][];


            var majorityFilter = Task.Factory.StartNew(() => Parallel.For(0, _predictionHeight, y =>
            {
                var line      = new int[_predictionWidth];
                var neighbors = new int[Enum.GetValues(typeof(LandcoverTypeViewModel)).Length - 1];

                for (var x = 0; x < _predictionWidth; ++x)
                {
                    if (y > 0 && x > 0 && y < _predictionHeight - 1 && x < _predictionWidth - 1)
                    {
                        for (var i = 0; i < neighbors.Length; ++i)
                        {
                            neighbors[i] = 0;
                        }

                        neighbors[_classification[y - 1][x - 1]]++;
                        neighbors[_classification[y - 1][x + 0]]++;
                        neighbors[_classification[y - 1][x + 1]]++;
                        neighbors[_classification[y + 0][x - 1]]++;
                        neighbors[_classification[y + 0][x + 0]]++;
                        neighbors[_classification[y + 0][x + 1]]++;
                        neighbors[_classification[y + 1][x - 1]]++;
                        neighbors[_classification[y + 1][x + 0]]++;
                        neighbors[_classification[y + 1][x + 1]]++;

                        var biggest      = neighbors[0];
                        var biggestIndex = 0;
                        for (var index = 0; index < neighbors.Length; index++)
                        {
                            if (neighbors[index] > biggest)
                            {
                                biggest      = neighbors[index];
                                biggestIndex = index;
                            }
                        }

                        line[x] = biggestIndex;
                    }
                    else
                    {
                        line[x] = _classification[y][x];
                    }
                }

                filtered[y] = line;
            }));

            majorityFilter.ContinueWith(t =>
            {
                _classification = filtered;

                var stride    = _predictionWidth * 4;
                var size      = _predictionHeight * stride;
                var imageData = new byte[size];

                var types = SimpleIoc.Default.GetInstance <MainWindowViewModel>().LandcoverTypes;

                Parallel.For(0, _predictionWidth, x =>
                {
                    for (var y = 0; y < _predictionHeight; ++y)
                    {
                        var index = 4 * y * _predictionWidth + 4 * x;
                        LandcoverTypeViewModel typeViewModel = types[_classification[y][x]];
                        var color            = typeViewModel.Color;
                        imageData[index + 0] = color.B;
                        imageData[index + 1] = color.G;
                        imageData[index + 2] = color.R;
                        imageData[index + 3] = color.A;
                    }
                });

                Application.Current.Invoke(() =>
                {
                    ClassificationOverlay = BitmapSource.Create(_predictionWidth, _predictionHeight, 96, 96,
                                                                PixelFormats.Bgra32, null, imageData, stride);
                    NotBlocking        = true;
                    ProgressVisibility = Visibility.Hidden;
                });
            });
        }
示例#3
0
 public FeatureByTypeViewModel(LandcoverTypeViewModel landCoverType)
 {
     LandCoverType = landCoverType;
     Features      = new ObservableCollection <ClassifiedFeatureVectorViewModel>();
 }
示例#4
0
 public ObservableCollection <ClassifiedFeatureVectorViewModel> GetFeaturesByType(LandcoverTypeViewModel typeViewModel)
 {
     return(FeaturesByType.First(f => f.LandCoverType == typeViewModel).Features);
 }
示例#5
0
 public ClassifiedFeatureVectorViewModel(ClassifiedFeatureVector classifiedFeatureVector)
 {
     ClassifiedFeatureVector = classifiedFeatureVector;
     FeatureTypeViewModel    = MainWindowViewModel.Default.LandcoverTypes.Values.ToList()[ClassifiedFeatureVector.FeatureClass];
 }