// 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; } }
// Outlines capture public static OutlineParameters ConfirmCapture() { try { OutlineParameters op; using (IPcLink link = HPPC.CreateLink()) { using (IPcMoment moment = link.CaptureMoment()) { IPcPicture picture = link.ExtractPicture(moment); IPcOutline outline = link.ExtractOutline(moment); op = PictureHandling.SavePicture(picture, outline); outline.Dispose(); picture.Dispose(); moment.Dispose(); } link.Dispose(); } return(op); } catch (Exception exception) { Console.WriteLine("\t\t*****An error occurred*****\n\n{0}{1}\n\nExit now, or this console will automatically exit in 15 seconds.", ToolBox.TimeStamp(), exception.Message); ToolBox.AppExceptionExit(); return(null); } }
// Several pictures extracted by index internal static object[] getSamples(List <string> folders, List <int> indexes, List <PcPhysicalPoint> locations, List <System.Drawing.Point> sizes) { try { using (IPcLink link = HPPC.CreateLink()) { object[] returned; using (IPcMoment moment = link.CaptureMoment()) { IPcPicture picture = link.ExtractPicture(moment); IPcOutline outlines = link.ExtractOutline(moment); returned = PictureHandling.SaveSamples(picture, outlines, folders, indexes, locations, sizes); outlines.Dispose(); picture.Dispose(); moment.Dispose(); } link.Dispose(); return(returned); } } catch (Exception exception) { Console.WriteLine("\t\t*****An error occurred*****\n\n{0}{1}\n\nExit now, or this console will automatically exit in 15 seconds.", ToolBox.TimeStamp(), exception.Message); ToolBox.AppExceptionExit(); return(null); } }
// Picture by index capture public static void Capture(Task t, bool repetition) { try { using (IPcLink link = HPPC.CreateLink()) { using (IPcMoment moment = link.CaptureMoment()) { IPcPicture picture = link.ExtractPicture(moment); IPcOutline outline = link.ExtractOutline(moment); if (!repetition) { PictureHandling.SaveIndexedImage(picture, outline, t); } else { PictureHandling.SaveIndexedImageRep(picture, outline, t); } outline.Dispose(); picture.Dispose(); moment.Dispose(); } link.Dispose(); } } catch (Exception exception) { Console.WriteLine("\t\t*****An error occurred*****\n\n{0}{1}\n\nExit now, or this console will automatically exit in 15 seconds.", ToolBox.TimeStamp(), exception.Message); ToolBox.AppExceptionExit(); } }
// Picture by index capture public static void Capture(Task t) { using (IPcLink link = HPPC.CreateLink()) { using (IPcMoment moment = link.CaptureMoment()) { IPcPicture picture = link.ExtractPicture(moment); PictureHandling.SaveIndexedImage(picture, t); } } }
// 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; } }
public static Texture2D captureFrame() { const int DOWN_SAMPLE_RATE = 1; int FRAME_WIDTH = 2048; Texture2D frame; IPcLink link = HPPC.CreateLink(); IPcMoment moment = link.CaptureMoment(); IPcPicture picture = link.ExtractPicture(moment); Bitmap bmp = SproutExtension.createBitmap(picture.Image); GraphicsUnit units = GraphicsUnit.Pixel; RectangleF rect = bmp.GetBounds(ref units); int width = (int)rect.Width / DOWN_SAMPLE_RATE, height = (int)rect.Height / DOWN_SAMPLE_RATE; frame = new Texture2D(FRAME_WIDTH, FRAME_WIDTH); int xOffset = (width / 2) - (FRAME_WIDTH / 2); int yOffset = (height / 2) - (FRAME_WIDTH / 2); UnityEngine.Color[] colors = new UnityEngine.Color[FRAME_WIDTH * FRAME_WIDTH]; for (int x = 0; x < FRAME_WIDTH; x++) { for (int y = 0; y < FRAME_WIDTH; y++) { System.Drawing.Color sysColor = bmp.GetPixel(xOffset + x, yOffset + y); Color32 uc32 = new Color32(sysColor.R, sysColor.B, sysColor.G, sysColor.A); //UnityEngine.Color unityColor = new UnityEngine.Color((float)sysColor.R / 255f, (float)sysColor.G / 255f, (float)sysColor.B / 255f, (float)sysColor.A / 255f); int index = (FRAME_WIDTH - 1 - y) * FRAME_WIDTH + x; if (index < colors.Length && index >= 0) { colors[index] = uc32; } else { Debug.Log("Index out of range."); } } } // test - only take a square of pixels in middle of screen frame.SetPixels(colors); frame.Apply(); TextureScale.Point(frame, 1024, 1024); return(frame); }
// 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++; }
private static PcImage getImage(int index, IPcPicture pic) { int counter = 0; foreach (IPcPicture img in pic.Children) { if (counter == index) { return(img.Image); } counter++; } return(null); }
// 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); }
// Several pictures extracted by index internal static List <Image> getSamples(List <string> folders, List <int> indexes) { try { using (IPcLink link = HPPC.CreateLink()) { using (IPcMoment moment = link.CaptureMoment()) { IPcPicture picture = link.ExtractPicture(moment); List <Image> img = PictureHandling.SaveSamples(picture, folders, indexes); return(img); } } } catch (Exception exception) { Console.WriteLine("\t\t*****An error occurred*****\n\n{0}{1}\n\nExit now, or this console will automatically exit in 15 seconds.", ToolBox.TimeStamp(), exception.Message); ToolBox.AppExceptionExit(); return(null); } }
// 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); }
/*private object ConvertIPcPicture(IPcPicture picture) { var result = new { Image = ConvertToBase64(picture.Image), Children = (from c in picture.Children select ConvertIPcPicture(c)).ToArray(), PhysicalBoundaries = picture.PhysicalBoundaries, PixelDensity = picture.PixelDensity, SkewAngle = double.IsNaN(picture.SkewAngle) ? 0.0 : picture.SkewAngle }; return result; }*/ //todo: move to a different class private object ConvertIPcPicture(IPcPicture picture) { var result = new { //Image = SaveImage(picture.Image), Image = ConvertToBase64(picture.Image), Children = (from c in picture.Children select ConvertIPcPicture(c)).ToArray(), PhysicalBoundaries = picture.PhysicalBoundaries, PixelDensity = picture.PixelDensity, SkewAngle = double.IsNaN(picture.SkewAngle) ? 0.0 : picture.SkewAngle }; return result; }
// 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(); } }