示例#1
0
        public void CarveSingle(Direction direction)
        {
            var seam = SeamCarvingHelper.GetSeamOfLowestEnergy(_energyMatrix, _fitnessMatrix, direction);

            ImageMatrix.RemoveSeam(seam);
            _energyMatrix.RemoveSeam(seam);
            _fitnessMatrix.RemoveSeam(seam);

            if (direction == Direction.Width)
            {
                ImageMatrix.ReuseAsSubset(ImageMatrix.Width - 1, ImageMatrix.Height);
                _energyMatrix.ReuseAsSubset(_energyMatrix.Width - 1, _energyMatrix.Height);
                _fitnessMatrix.ReuseAsSubset(_fitnessMatrix.Width - 1, _fitnessMatrix.Height);
            }
            else
            {
                ImageMatrix.ReuseAsSubset(ImageMatrix.Width, ImageMatrix.Height - 1);
                _energyMatrix.ReuseAsSubset(_energyMatrix.Width, _energyMatrix.Height - 1);
                _fitnessMatrix.ReuseAsSubset(_fitnessMatrix.Width, _fitnessMatrix.Height - 1);
            }
        }
        private async void OpenImage()
        {
#if DEBUG
            var file = @"D:\test.jpg";
#else
            var dialog = new OpenFileDialog
            {
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                DefaultExt       = ".jpeg",
                Filter           = "Image files|*.jpg;*.jpeg;*.png;*.gif|All files (*.*)|*.*"
            };

            var dialogResult = dialog.ShowDialog();

            if (dialogResult != true)
            {
                return;
            }
            var file = dialog.FileName;
#endif

            _isLoading = true;
            try
            {
                var image = await Task.Run(() =>
                {
                    using (var fileStream = File.Open(file, FileMode.Open))
                    {
                        var img = new BitmapImage();
                        img.BeginInit();
                        img.CacheOption  = BitmapCacheOption.OnLoad;
                        img.StreamSource = fileStream;
                        img.EndInit();
                        img.Freeze();

                        return(img);
                    }
                });

                InputImage  = image;
                OutputImage = image;

                var imageMatrix          = ImageMatrixFactory.FromBitmapSource(InputImage, OptimizeMode.Width);
                var grayscaleImageMatrix = ImageMatrixFactory.Empty <byte>(imageMatrix.Width, imageMatrix.Height, imageMatrix.OptimizeMode);
                imageMatrix.ToGrayScale(grayscaleImageMatrix);
                var energyMatrix = ImageMatrixFactory.Empty <byte>(imageMatrix.Width, imageMatrix.Height, imageMatrix.OptimizeMode);
                SeamCarvingHelper.ComputeEnergyMapBySobel(grayscaleImageMatrix, energyMatrix);

                _energyMatrix               = energyMatrix;
                EnergyFunctionImage         = energyMatrix.ToBitmapSource();
                EnergyFunctionWithBiasImage = EnergyFunctionImage;

                InputImageInfoStr = $"Width: {image.PixelWidth}, Height: {image.PixelHeight}";

                DimensionPixels = CarveWidth ? image.PixelWidth : image.PixelHeight;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            finally
            {
                _isLoading = false;
                ((RelayCommand)OpenImageCommand).RaiseCanExecuteChanged();
            }
        }