コード例 #1
0
        // 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);
        }
コード例 #2
0
        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();
            }
        }