コード例 #1
0
ファイル: Program.cs プロジェクト: WayneWBerry/MTGCardCV
        private static bool Process(MTGCard card, string dir)
        {
            bool hashName = false;

            Guid   cardId   = Guid.Parse(card.Id.Substring(0, 32));
            string fileName = string.Format(@"c:\temp\cards\{0}.bmp", cardId);

            using (Mat image = new Mat(fileName, LoadImageType.Color))
            {
                Console.WriteLine(string.Format("Processing: {0}", fileName));

                CardImage cardImage = new CardImage(cardId, image, angle: 0, cardFrame: card.Set.CardFrame);

                using (Mat setIcon = cardImage.FindSetIcons().FirstOrDefault())
                {
                    if (setIcon != null)
                    {
                        string fullName;

                        if (hashName == true)
                        {
                            string hash;
                            using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
                            {
                                ImageConverter converter = new ImageConverter();
                                var            byteArray = (byte[])converter.ConvertTo(setIcon.Bitmap, typeof(byte[]));
                                hash = Convert.ToBase64String(sha1.ComputeHash(byteArray));
                                hash = hash.Replace("=", "").Replace("-", "").Replace("+", "").Replace("\\", "").Replace("/", "").ToUpper().Substring(0, 8);
                            }

                            fullName = string.Format(@"{0}{1}-{2}.bmp", dir, card.Set.Code, hash);
                        }
                        else
                        {
                            fullName = string.Format(@"{0}{1}-{2}.bmp", dir, cardId, card.Set.CardFrame);
                        }

                        setIcon.Bitmap.Save(fullName);

                        return(true);
                    }
                    else
                    {
                        var savedColor = Console.ForegroundColor;
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(string.Format("Unable To Process: {0}", fileName));
                        Console.ForegroundColor = savedColor;
                    }
                }
            }

            return(false);
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: WayneWBerry/MTGCardCV
        private void ReImage(Guid cardId, Mat image)
        {
            Cursor.Current = Cursors.WaitCursor;

            try
            {
                CardImage.ImageEvent += CardImage_ImageEvent;

                /*
                 * var image = cameraController.CaptureAsync(focus: 110).Result;
                 * pictureBox1.Image = image.Bitmap;
                 *
                 * image.Bitmap.Save(@"c:\temp\test.bmp");
                 */

                CardField cardField = new CardField(image);

                if (cardField != null)
                {
                    Mat contourImage = cardField.GetContouredImage(angle: 0, cannyParameter: new CannyParam(250, 125, true, 3));
                    if (contourImage != null)
                    {
                        pictureBox7.Image = contourImage.Bitmap;
                        tabPage7.Text     = "Field Contoured";
                    }

                    CardImage cardImage = cardField.FindCards(cardId).FirstOrDefault();

                    if (cardImage != null)
                    {
                        Mat setIconImage = cardImage.FindSetIcons().FirstOrDefault();
                        if (setIconImage != null)
                        {
                            pictureBox8.Image = setIconImage.Bitmap;
                            tabPage8.Text     = "Set Icon";
                        }

                        pictureBox1.Image = cardImage.GetRotatedImage().Bitmap;
                        tabPage1.Text     = "Card Image";

                        Mat cardGreyImage = cardImage.GetGreyImage();
                        if (cardGreyImage != null)
                        {
                            pictureBox6.Image = cardGreyImage.Bitmap;
                            tabPage6.Text     = "Grey Image";
                        }

                        Mat cardContourImage = cardImage.GetContouredImage(angle: 0, cannyParameter: new CannyParam(250, 125, true, 3));
                        if (cardContourImage != null)
                        {
                            pictureBox2.Image = cardContourImage.Bitmap;
                            tabPage2.Text     = "Card Contour";
                        }

                        CardTitleImage titleImage = cardImage.GetCardTitleImage();

                        if (titleImage != null)
                        {
                            pictureBox3.Image = titleImage.GetImage().Bitmap;
                            tabPage3.Text     = "Title Image";
                        }
                    }
                }
            }
            finally
            {
                CardImage.ImageEvent -= CardImage_ImageEvent;
                Cursor.Current        = Cursors.Default;
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            int count   = 0;
            int success = 0;

            foreach (string fileName in Directory.GetFiles(@"C:\Temp\cards", "*.bmp"))
            {
                count++;

                using (Mat image = new Mat(fileName, LoadImageType.Color))
                {
                    try
                    {
                        Console.WriteLine(string.Format("Processing: {0}", fileName));

                        FileInfo info   = new FileInfo(fileName);
                        var      guid   = info.Name.Substring(0, info.Name.Length - 4);
                        Guid     cardId = Guid.Parse(guid);

                        CardField cardField = new CardField(image);

                        Stopwatch stopWatch = Stopwatch.StartNew();

                        CardImage cardImage = cardField.FindCards(cardId).FirstOrDefault();

                        stopWatch.Stop();

                        if (cardImage != null)
                        {
                            Console.WriteLine(string.Format("Found: {0} ({1:0.0}%) in {2} seconds", cardImage.GetName(), cardImage.GetNameSurity(), stopWatch.Elapsed.Seconds));
                            success++;

                            try
                            {
                                // Write Out The Image Files For Debugging
                                CardImage.ImageEvent += CardImage_ImageEvent;

                                using (Mat setIcon = cardImage.FindSetIcons().FirstOrDefault())
                                {
                                    if (setIcon != null)
                                    {
                                        WriteSetIcon(cardId, setIcon);
                                    }
                                }
                            }
                            finally
                            {
                                CardImage.ImageEvent -= CardImage_ImageEvent;
                            }
                        }
                        else
                        {
                            // Failed

                            var savedColor = Console.ForegroundColor;
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine(string.Format("Unable To Process: {0}", fileName));
                            Console.ForegroundColor = savedColor;

                            try
                            {
                                // Do It Again And Write Out The Image Files For Debugging
                                CardImage.ImageEvent += CardImage_ImageEvent;
                                cardField.FindCards(cardId).ToArray();
                            }
                            finally
                            {
                                CardImage.ImageEvent -= CardImage_ImageEvent;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
            }

            Console.WriteLine("{0}/{1} {2}%", success, count, ((double)success / (double)count) * 100.0);
            Console.ReadLine();
        }