private void OnSaveLatentSvmTrainingSetButtonClick(object sender, RoutedEventArgs e) { FolderBrowserDialog dlg = new FolderBrowserDialog(); HwndSource source = PresentationSource.FromVisual(this) as HwndSource; System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle); if (dlg.ShowDialog(win) != System.Windows.Forms.DialogResult.OK) { return; } string directory = dlg.SelectedPath; using (StreamWriter writer = new StreamWriter(Path.Combine(directory, "all.txt"))) { string colorModelsPath = Path.Combine(directory, "color_model.clr"); this.colorModels.SaveToFile(colorModelsPath); writer.WriteLine(colorModelsPath); int index = 0; foreach (ImageInfo imageInfo in imageInfos) { double scale = this.CalcSegmentedImageScale(imageInfo.Image); Shape scaledShape = imageInfo.Shape.Scale(scale, Vector.Zero); BitmapSource scaledImage = ImageHelper.ResizeImage( imageInfo.Image, (int)Math.Round(imageInfo.Image.PixelWidth * scale), (int)Math.Round(imageInfo.Image.PixelHeight * scale)); Image2D <Color> scaledConvertedImage = ImageHelper.BitmapSourceToImage2D(scaledImage); string shapePath = Path.Combine(directory, string.Format("shape_{0:000}.s", index)); scaledShape.SaveToFile(shapePath); string imagePath = Path.Combine(directory, string.Format("image_{0:000}.png", index)); Image2D.SaveToFile(scaledConvertedImage, imagePath); writer.WriteLine("{0}\t{1}", shapePath, imagePath); ++index; } } }
private void SegmentImage(Action <SimpleSegmentationAlgorithm, double> customSetupStep) { int selectedIndex = this.backgroundImagesListBox.SelectedIndex; BitmapSource originalImage = this.imageInfos[selectedIndex].Image; double scale = CalcSegmentedImageScale(originalImage); BitmapSource scaledImage = ImageHelper.ResizeImage( originalImage, (int)(originalImage.PixelWidth * scale), (int)(originalImage.PixelHeight * scale)); Image2D <Color> image = ImageHelper.BitmapSourceToImage2D(scaledImage); Image2D.SaveToFile(image, "image.png"); // Customize segmentator SimpleSegmentationAlgorithm segmentator = new SimpleSegmentationAlgorithm(); segmentator.ObjectShapeUnaryTermWeight = this.algorithmProperties.ShapeUnaryTermWeight; segmentator.BackgroundShapeUnaryTermWeight = this.algorithmProperties.ShapeUnaryTermWeight; segmentator.ObjectColorUnaryTermWeight = this.algorithmProperties.ColorUnaryTermWeight; segmentator.BackgroundColorUnaryTermWeight = this.algorithmProperties.ColorUnaryTermWeight; segmentator.ColorDifferencePairwiseTermWeight = this.algorithmProperties.BinaryTermWeight; segmentator.ColorDifferencePairwiseTermCutoff = this.algorithmProperties.BrightnessBinaryTermCutoff; segmentator.ShapeEnergyWeight = this.algorithmProperties.ShapeEnergyWeight; // Run custom setup step customSetupStep(segmentator, scale); // Segment SegmentationSolution result = segmentator.SegmentImage(image, this.colorModels); Image2D.SaveToFile(result.Mask, "mask.png"); BitmapSource maskImage = ImageHelper.ResizeImage( ImageHelper.MaskToBitmapSource(result.Mask), originalImage.PixelWidth, originalImage.PixelHeight); this.imageInfos[selectedIndex].SegmentationMask = maskImage; // Show results this.UpdateControlsAccordingToCurrentState(); this.editorTabControl.SelectedIndex = 1; }
private static void TestShapeTermsImpl(string testName, ShapeModel shapeModel, IEnumerable<VertexConstraints> vertexConstraints, IEnumerable<EdgeConstraints> edgeConstraints, Size imageSize) { ShapeConstraints constraintSet = ShapeConstraints.CreateFromConstraints(shapeModel.Structure, vertexConstraints, edgeConstraints); // Get CPU results Image2D<ObjectBackgroundTerm> shapeTermsCpu = new Image2D<ObjectBackgroundTerm>(imageSize.Width, imageSize.Height); CpuShapeTermsLowerBoundCalculator calculatorCpu = new CpuShapeTermsLowerBoundCalculator(); calculatorCpu.CalculateShapeTerms(shapeModel, constraintSet, shapeTermsCpu); Image2D.SaveToFile(shapeTermsCpu, -1000, 1000, String.Format("./{0}_cpu.png", testName)); // Get GPU results Image2D<ObjectBackgroundTerm> shapeTermsGpu = new Image2D<ObjectBackgroundTerm>(imageSize.Width, imageSize.Height); GpuShapeTermsLowerBoundCalculator calculatorGpu = new GpuShapeTermsLowerBoundCalculator(); calculatorGpu.CalculateShapeTerms(shapeModel, constraintSet, shapeTermsGpu); Image2D.SaveToFile(shapeTermsGpu, -1000, 1000, String.Format("./{0}_gpu.png", testName)); // Compare with CPU results for (int x = 0; x < imageSize.Width; ++x) for (int y = 0; y < imageSize.Height; ++y) { Assert.AreEqual(shapeTermsCpu[x, y].ObjectTerm, shapeTermsGpu[x, y].ObjectTerm, 1e-2f); Assert.AreEqual(shapeTermsCpu[x, y].BackgroundTerm, shapeTermsGpu[x, y].BackgroundTerm, 1e-2f); } }
private void OnLearnColorModelButtonClick(object sender, RoutedEventArgs e) { if (this.backgroundImagesListBox.SelectedIndex != -1) { ImageInfo currentImageInfo = this.imageInfos[this.backgroundImagesListBox.SelectedIndex]; currentImageInfo.ColorModelMask = this.colorMaskEditor.GetMask(currentImageInfo.Image.PixelWidth, currentImageInfo.Image.PixelHeight); } List <Color> objectColors = new List <Color>(); List <Color> backgroundColors = new List <Color>(); for (int i = 0; i < this.imageInfos.Count; ++i) { if (this.imageInfos[i].ColorModelMask == null) { continue; } Image2D <Color> image = ImageHelper.BitmapSourceToImage2D(this.imageInfos[i].Image); ExtractObjectBackgroundColorsByMask( image, this.imageInfos[i].ColorModelMask, objectColors, backgroundColors); Image2D.SaveToFile(image, string.Format("image_{0}.png", i)); Image2D.SaveToFile(this.imageInfos[i].ColorModelMask, string.Format("mask_{0}.png", i)); } if (objectColors.Count == 0) { MessageBox.Show("No object pixels specified."); return; } if (backgroundColors.Count == 0) { MessageBox.Show("No background pixels specified."); return; } Helper.Subsample(objectColors, this.algorithmProperties.MaxPixelsToLearnFrom); Helper.Subsample(backgroundColors, this.algorithmProperties.MaxPixelsToLearnFrom); try { GaussianMixtureColorModel objectModel = GaussianMixtureColorModel.Fit( objectColors.Take(this.algorithmProperties.MaxPixelsToLearnFrom), this.algorithmProperties.MixtureComponentCount, this.algorithmProperties.StopTolerance); GaussianMixtureColorModel backgroundModel = GaussianMixtureColorModel.Fit( backgroundColors.Take(this.algorithmProperties.MaxPixelsToLearnFrom), this.algorithmProperties.MixtureComponentCount, this.algorithmProperties.StopTolerance); this.colorModels = new ObjectBackgroundColorModels(objectModel, backgroundModel); this.UpdateControlsAccordingToCurrentState(); } catch (Exception exception) { MessageBox.Show(exception.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error); } }