// Save an image given by a task internal static void SaveIndexedImage(IPcPicture picture, Task t) { string dir = Path.Combine(t.getFolder(), @"Captures\"); ToolBox.EnsureDirectoryExists(dir); int i = 0; foreach (IPcPicture image in picture.Children) { if (i == t.getIndex()) { string fileAndPath = Path.Combine(dir, DateTime.Now.ToString("MM-dd-yyyy_hh.mm.ss" + "_" + marker) + ".bmp"); ToolBox.SaveProcessedImage(image.Image, fileAndPath); List <Uri> l = new List <Uri>(); t.setCaptures(l); Uri u = new Uri(fileAndPath, UriKind.Relative); l.Add(u); t.getCaptureWindow().DrawImage(); ++i; } ++i; } }
// Save all objects in a picture separately public static void SaveAllImages(IPcPicture picture) { ToolBox.EnsureDirectoryExists(_saveDirectory); int i = 1; foreach (IPcPicture image in picture.Children) { string fileAndPath = Path.Combine(_saveDirectory, "Object_" + i + ".bmp"); ToolBox.SaveProcessedImage(image.Image, fileAndPath); ++i; } }
// This saves the parent-level image of IPcPicture. public static void SaveMatImage(IPcPicture picture) { _saveDirectory = Path.Combine(ToolBox.defaultFilePath, @"Pictures\" + DateTime.Now.ToString("MM-dd-yyyy_hh.mm.ss" + "_" + marker)); ToolBox.EnsureDirectoryExists(_saveDirectory); PcImage image = picture.Image; string fileAndPath = Path.Combine(_saveDirectory, "MatImage.bmp"); ToolBox.SaveProcessedImage(image, fileAndPath); marker++; }
// Save an image given by a task internal static void SaveIndexedImage(IPcPicture picture, IPcOutline outline, Task t) { string dir = Path.Combine(t.getFolder(), @"Captures\"); ToolBox.EnsureDirectoryExists(dir); int i = 0; foreach (IPcPicture image in picture.Children) { if (i == t.getIndex()) { // Check wether if a match is achieved between the expected and the obtained object. If not, a second capture is attempted // This is needed because of Sprout's capture errors, that often lead to wrong object identification if (ConfirmMatch(outline, i, t)) { string fileAndPath = Path.Combine(dir, DateTime.Now.ToString("MM-dd-yyyy_hh.mm.ss" + "_" + marker) + ".bmp"); ToolBox.SaveProcessedImage(image.Image, fileAndPath); List <Uri> l = new List <Uri>(); t.setCaptures(l); Uri u = new Uri(fileAndPath, UriKind.Relative); l.Add(u); App.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle, new Action(() => t.getCaptureWindow().DrawImage(false))); //t.getCaptureWindow().DrawImage(false); ++i; } else { MomentCapture.Capture(t, true); } } i++; } }
// Handle pictures given by and index to CapturePreviews internal static List <System.Windows.Controls.Image> SaveSamples(IPcPicture picture, List <string> folders, List <int> indexes) { List <System.Windows.Controls.Image> imgs = new List <System.Windows.Controls.Image>(); int i = 0; for (int j = 0; j < folders.Count; j++) { i = 0; foreach (IPcPicture image in picture.Children) { if (i == indexes[j]) { string dir = Path.Combine(folders[j], @"Captures\"); ToolBox.EnsureDirectoryExists(dir); string fileAndPath = Path.Combine(dir, DateTime.Now.ToString("MM-dd-yyyy_hh.mm.ss" + "_" + marker) + ".bmp"); ToolBox.SaveProcessedImage(image.Image, fileAndPath); Uri u = new Uri(fileAndPath, UriKind.Relative); System.Windows.Controls.Image im = new System.Windows.Controls.Image(); BitmapImage src = new BitmapImage(); src.BeginInit(); src.UriSource = u; src.CacheOption = BitmapCacheOption.OnLoad; src.EndInit(); im.Source = src; im.Stretch = Stretch.Uniform; im.Stretch = Stretch.Uniform; imgs.Insert(0, im); } i++; } } return(imgs); }
private void saveButton_Click(object sender, RoutedEventArgs e) { string s = labelTextBox.Text; System.Windows.Controls.Button b = (System.Windows.Controls.Button)sender; if (s.Length < 1) { System.Windows.MessageBox.Show("Please introduce a valid label"); return; } FolderBrowserDialog sfd = new FolderBrowserDialog(); DialogResult res = sfd.ShowDialog(); if (res == DialogResult.OK && !string.IsNullOrWhiteSpace(sfd.SelectedPath)) { string loc = System.IO.Path.Combine(sfd.SelectedPath, "classificationOutput"); ToolBox.EnsureDirectoryExists(loc); string imageName = System.IO.Path.Combine(loc, "savedColony.bmp"); string fileName = System.IO.Path.Combine(loc, "label.txt"); foreach (object child in StackPanel2.Children) { System.Windows.Controls.Image img = (System.Windows.Controls.Image)child; Bitmap bmpOut = getBitmapFromControlsImage(img); bmpOut.Save(imageName); } string[] content = new string[] { "Colony saved for future trainings", "Map position found to be the closest: " + b.Uid.ToString(), "Label set: " + s }; File.AppendAllLines(fileName, content); labelTextBox.Text = ""; undefinedCanvas.Visibility = Visibility.Hidden; } }
// Saves the outlines public static OutlineParameters SavePicture(IPcPicture picture, IPcOutline parentOutline) { _saveDirectory = Path.Combine(ToolBox.defaultFilePath, @"Pictures\" + "ConfirmDirectory"); ToolBox.EnsureDirectoryExists(_saveDirectory); ToolBox.EnsureDirectoryExists(ToolBox.defaultFilePath); PcImage image = picture.Image; confirmPath = Path.Combine(_saveDirectory, "confirmPicture" + marker2 + ".bmp"); ToolBox.SaveProcessedImage(image, confirmPath); marker2++; List <Point> outlineBoundaries = new List <Point>(); Point globalPicSize = new Point(Convert.ToInt32(GetOutlineWidth(parentOutline)), Convert.ToInt32(GetOutlineHeight(parentOutline))); foreach (IPcOutline outline in parentOutline.Children) { if (GetOutlineHeight(outline) > 50 && GetOutlineWidth(outline) > 50) { outlineBoundaries.Add(new Point(Convert.ToInt32(GetOutlineWidth(outline)), Convert.ToInt32(GetOutlineHeight(outline)))); } } List <PcPhysicalPoint> pictureLocation = new List <PcPhysicalPoint>(); foreach (IPcPicture pic in picture.Children) { PcPhysicalPoint loc = new PcPhysicalPoint(pic.PhysicalBoundaries.Location.X * (pic.PixelDensity.X), pic.PhysicalBoundaries.Location.Y * (pic.PixelDensity.Y)); pictureLocation.Add(loc); } OutlineParameters op = new OutlineParameters(pictureLocation, outlineBoundaries, globalPicSize); return(op); }
// Handle pictures given by and index to CapturePreviews internal static object[] SaveSamples(IPcPicture picture, IPcOutline outline, List <string> folders, List <int> indexes, List <PcPhysicalPoint> locations, List <System.Drawing.Point> sizes) { List <System.Windows.Controls.Image> imgs = new List <System.Windows.Controls.Image>(); List <int> results = new List <int>(); List <IPcOutline> outlines = new List <IPcOutline>(); int i = 0; bool found = false; for (int j = 0; j < folders.Count; j++) { i = 0; foreach (IPcPicture image in picture.Children) { if (i == indexes[j]) { if (ConfirmMatch(locations[j], sizes[j], outline, i)) { string dir = Path.Combine(folders[j], @"Captures\"); ToolBox.EnsureDirectoryExists(dir); string fileAndPath = Path.Combine(dir, DateTime.Now.ToString("MM-dd-yyyy_hh.mm.ss" + "_" + marker) + ".bmp"); ToolBox.SaveProcessedImage(image.Image, fileAndPath); Uri u = new Uri(fileAndPath, UriKind.Relative); System.Windows.Controls.Image im = new System.Windows.Controls.Image(); BitmapImage src = new BitmapImage(); src.BeginInit(); src.UriSource = u; src.CacheOption = BitmapCacheOption.OnLoad; src.EndInit(); im.Source = src; im.Stretch = Stretch.Uniform; im.Stretch = Stretch.Uniform; imgs.Insert(0, im); outlines.Insert(0, getOutline(i, outline)); // No problem results.Insert(0, 0); found = true; } if (!found) { List <int> locationDifferences = new List <int>(); foreach (IPcOutline outlineChild in outline.Children) { if (ConfirmSize(outlineChild, sizes[j])) { locationDifferences.Add(FindCloser(outline, locations[j])); } else { locationDifferences.Add(Int32.MaxValue); } } int[] minIndex = new int[] { Int32.MaxValue, Int32.MaxValue }; for (int n = 0; n < locationDifferences.Count; n++) { if (locationDifferences[n] < minIndex[0]) { minIndex[0] = locationDifferences[n]; minIndex[1] = n; } } // An object has found inside the minimum location difference if (minIndex[0] < AdvancedOptions._nLocationThreshold) { string dir = Path.Combine(folders[j], @"Captures\"); ToolBox.EnsureDirectoryExists(dir); string fileAndPath = Path.Combine(dir, DateTime.Now.ToString("MM-dd-yyyy_hh.mm.ss" + "_" + marker) + ".bmp"); ToolBox.SaveProcessedImage(image.Image, fileAndPath); Uri u = new Uri(fileAndPath, UriKind.Relative); System.Windows.Controls.Image im = new System.Windows.Controls.Image(); BitmapImage src = new BitmapImage(); src.BeginInit(); src.UriSource = u; src.CacheOption = BitmapCacheOption.OnLoad; src.EndInit(); im.Source = src; im.Stretch = Stretch.Uniform; im.Stretch = Stretch.Uniform; imgs.Insert(0, im); outlines.Insert(0, getOutline(i, outline)); results.Insert(0, 0); } // No object in the expected range: we capture a deplaced one and inform the user else if (minIndex[0] < Int32.MaxValue) { string dir = Path.Combine(folders[j], @"Captures\"); ToolBox.EnsureDirectoryExists(dir); string fileAndPath = Path.Combine(dir, DateTime.Now.ToString("MM-dd-yyyy_hh.mm.ss" + "_" + marker) + ".bmp"); ToolBox.SaveProcessedImage(image.Image, fileAndPath); Uri u = new Uri(fileAndPath, UriKind.Relative); System.Windows.Controls.Image im = new System.Windows.Controls.Image(); BitmapImage src = new BitmapImage(); src.BeginInit(); src.UriSource = u; src.CacheOption = BitmapCacheOption.OnLoad; src.EndInit(); im.Source = src; im.Stretch = Stretch.Uniform; im.Stretch = Stretch.Uniform; imgs.Insert(0, im); outlines.Insert(0, getOutline(i, outline)); results.Insert(0, 1); } // The object is not present in the mat as sizes don't match: stop capture process and inform the user else { imgs.Insert(0, null); outlines.Insert(0, null); results.Insert(0, 2); } } } i++; } } object[] returnable = new object[3]; returnable[0] = imgs; returnable[1] = outlines; returnable[2] = results; return(returnable); }
public static void SaveIndexedImageRep(IPcPicture picture, IPcOutline outline, Task t) { // If we are repeating the capture, an error has occured. We will thus try to match the // outline with other indexed objects present in the mat screen, looking for a user made location mistake string dir = Path.Combine(t.getFolder(), @"Captures\"); ToolBox.EnsureDirectoryExists(dir); List <int> locationDifferences = new List <int>(); // In order to acknowledge an object as the targeted one, it's size must match with the expected. // If several objects were to match the size, we shall choose thee who is closer to the expected location foreach (IPcOutline outlineChild in outline.Children) { if (ConfirmSize(outlineChild, t)) { locationDifferences.Add(FindCloser(outline, t)); } else { locationDifferences.Add(Int32.MaxValue); } } int[] minIndex = new int[] { Int32.MaxValue, Int32.MaxValue }; for (int i = 0; i < locationDifferences.Count; i++) { if (locationDifferences[i] < minIndex[0]) { minIndex[0] = locationDifferences[i]; minIndex[1] = i; } } // An object has found inside the minimum location difference if (minIndex[0] < AdvancedOptions._nLocationThreshold) { string fileAndPath = Path.Combine(dir, DateTime.Now.ToString("MM-dd-yyyy_hh.mm.ss" + "_" + marker) + ".bmp"); ToolBox.SaveProcessedImage(getImage(minIndex[1], picture), fileAndPath); List <Uri> l = new List <Uri>(); t.setCaptures(l); Uri u = new Uri(fileAndPath, UriKind.Relative); l.Add(u); App.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => t.getCaptureWindow().DrawImage(false))); //t.getCaptureWindow().DrawImage(false); } // No object in the expected range: we capture a deplaced one and inform the user else if (minIndex[0] < Int32.MaxValue) { string fileAndPath = Path.Combine(dir, DateTime.Now.ToString("MM-dd-yyyy_hh.mm.ss" + "_" + marker) + ".bmp"); ToolBox.SaveProcessedImage(getImage(minIndex[1], picture), fileAndPath); List <Uri> l = new List <Uri>(); t.setCaptures(l); Uri u = new Uri(fileAndPath, UriKind.Relative); l.Add(u); App.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() => t.getCaptureWindow().DrawImage(true))); //t.getCaptureWindow().DrawImage(true); t.setLocation(getOutlineLocation(minIndex[1], outline)); t.setSize(getOutlineSize(minIndex[1], outline)); } // The object is not present in the mat as sizes don't match: stop capture process and inform the user else { t.getCaptureWindow().CaptureError(); } }