private int FindBestMatch(Bitmap model_image, Bitmap[] candidates, List <int> used_indices)
        {
            double best_similarity = 0;
            int    best_index      = 0;

            AForge.Imaging.ExhaustiveTemplateMatching template_matching = new AForge.Imaging.ExhaustiveTemplateMatching(0);

            for (int i = 0; i < candidates.Length; i++)
            {
                if (used_indices.Contains(i))
                {
                    continue;
                }
                AForge.Imaging.TemplateMatch[] result;
                try
                {
                    result = template_matching.ProcessImage(model_image, candidates[i]);
                }
                catch (Exception)
                {
                    return(0);
                }
                double similarity = result[0].Similarity;
                if (i == 0 || similarity > best_similarity)
                {
                    best_similarity = similarity;
                    best_index      = i;
                }
            }
            return(best_index);
        }
Esempio n. 2
0
        private void PickFishAndRubbish()
        {
            var bmp     = new Bitmap(AppInvoke.GetImage());
            var bmpShow = bmp.Clone() as Image;
            var g       = Graphics.FromImage(bmpShow);

            //灰度化
            bmp = Grayscale.CommonAlgorithms.BT709.Apply(bmp);
            //反色
            new Invert().ApplyInPlace(bmp);
            //二值化
            new Threshold(120).ApplyInPlace(bmp);
            //保存模板以用于识别
            //bmp.Save("template.bmp");
            //fish
            var matchArray = new AForge.Imaging.ExhaustiveTemplateMatching(0.8f).ProcessImage(bmp, dicTemplate["fish"]);

            foreach (var m in matchArray)
            {
                g.FillRectangle(Brushes.Red, m.Rectangle);
                MoveAndClick(m.Rectangle.X + m.Rectangle.Width / 2, m.Rectangle.Y + m.Rectangle.Height / 2, 0);
            }
            //Rubbish
            matchArray = new AForge.Imaging.ExhaustiveTemplateMatching(0.88f).ProcessImage(bmp, dicTemplate["banana"]);
            foreach (var m in matchArray)
            {
                g.FillRectangle(Brushes.Yellow, m.Rectangle);
                MoveAndClick(m.Rectangle.X + m.Rectangle.Width / 2, m.Rectangle.Y + m.Rectangle.Height / 2, 0);
            }
            matchArray = new AForge.Imaging.ExhaustiveTemplateMatching(0.88f).ProcessImage(bmp, dicTemplate["bag"]);
            foreach (var m in matchArray)
            {
                g.FillRectangle(Brushes.Green, m.Rectangle);
                MoveAndClick(m.Rectangle.X + m.Rectangle.Width / 2, m.Rectangle.Y + m.Rectangle.Height / 2, 0);
            }
            matchArray = new AForge.Imaging.ExhaustiveTemplateMatching(0.88f).ProcessImage(bmp, dicTemplate["can"]);
            foreach (var m in matchArray)
            {
                g.FillRectangle(Brushes.Green, m.Rectangle);
                MoveAndClick(m.Rectangle.X + m.Rectangle.Width / 2, m.Rectangle.Y + m.Rectangle.Height / 2, 0);
            }
            this.Invoke(new Action(() =>
            {
                this.picModify.Image = bmpShow;
            }));
        }
Esempio n. 3
0
        static void Main(String[] args)
        {
            if (args.Count() == 0)
            {
                return;
            }

            String normalizedDir = args[0] + NORMALIZED;

            Directory.CreateDirectory(normalizedDir);

            List <String>        paths          = Directory.GetFiles(args[0], "*.*", SearchOption.TopDirectoryOnly).ToList();
            List <ScannedBitmap> scannedBitmaps = new List <ScannedBitmap>();
            List <UInt64>        exceptionIds   = GetExceptionIds();

            int scannedCount        = 0;
            int scannedCountCreated = 0;

            // get paths and bitmaps off disk
            foreach (String path in paths)
            {
                UInt64?exceptionId = HasNumber(path);

                if (exceptionId == null ||
                    (exceptionIds != null && exceptionIds.Contains((UInt64)exceptionId)))
                {
                    continue;
                }

                ScannedBitmap scannedBitmap = new ScannedBitmap(normalizedDir + Path.GetFileName(path), null);

                if (!File.Exists(scannedBitmap.Path))
                {
                    using (Image image = Image.FromFile(path))
                    {
                        scannedBitmap.Bitmap = PreProcessor.ImageUtil.ResizeImage(image, 256, 256);

                        // save normalized
                        scannedBitmap.Bitmap.Save(scannedBitmap.Path, ImageFormat.Jpeg);
                    }

                    ++scannedCountCreated;
                }
                else
                {
                    using (Image image = Image.FromFile(scannedBitmap.Path))
                    {
                        scannedBitmap.Bitmap = new Bitmap(image);
                    }

                    ++scannedCount;
                }

                scannedBitmaps.Add(scannedBitmap);
            }

            Log("====");
            Log(String.Format("==== New Run, {0}, {1} of {2}", args[1], scannedBitmaps.Count, paths.Count));
            Log("====");

            // iterate through bitmaps for psnr
            AForge.Imaging.ExhaustiveTemplateMatching tm = new AForge.Imaging.ExhaustiveTemplateMatching(0);

            using (List <ScannedBitmap> .Enumerator outer = scannedBitmaps.GetEnumerator())
            {
                while (outer.MoveNext())
                {
                    String pathOuter = Path.GetFileName(outer.Current.Path);
                    UInt64?outerId   = HasNumber(pathOuter);

                    using (List <ScannedBitmap> .Enumerator inner = outer)
                    {
                        while (inner.MoveNext())
                        {
                            String pathInner = Path.GetFileName(inner.Current.Path);
                            UInt64?innerId   = HasNumber(pathInner);

                            using (Bitmap outer24 = PreProcessor.ImageUtil.ConvertToFormat(outer.Current.Bitmap, PixelFormat.Format24bppRgb))
                            {
                                using (Bitmap inner24 = PreProcessor.ImageUtil.ConvertToFormat(inner.Current.Bitmap, PixelFormat.Format24bppRgb))
                                {
                                    AForge.Imaging.TemplateMatch[] matchings = tm.ProcessImage(outer24, inner24);

                                    if (matchings[0].Similarity > Convert.ToDouble(args[1]))
                                    {
                                        Log(String.Format("{0}\t{1}\t{2}\t{3}", pathOuter, (outerId == null) ? 0 : outerId,
                                                          pathInner, (innerId == null) ? 0 : innerId));
                                    }
                                }
                            }
                        }
                    }

                    if (outerId != null)
                    {
                        ExceptionLog((UInt64)outerId);
                    }
                }
            }

            paths          = null;
            scannedBitmaps = null;

            Console.WriteLine("Done");

            Console.ReadKey();
        }