AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

cann't make AForge.NET Framework work correctly

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

cann't make AForge.NET Framework work correctly

Postby dave_bulac » Sun Sep 27, 2015 8:54 am

Hello guys. I am working on my project in Windows Forms and cann't make this code work correctly.

I want to compare two Bitmap. One of them is in File : "C:\\Users\\Dave\\Documents\\Visual Studio 2012\\Projects\\Bura\\kartebi\\Kartebi" and the second I am getting from Screenshot. . I have made two Class. First is "Matching" class, which is comparing two Bitmap Images and uses "AForge.NET Framework". If image A contains Image B is return true, otherwise is return false; And the second Class is "imageCompare", in which i am creating string array "mis", there I have all image pathss which are in "Kartebi" Folder. Than I am creating one more array "bitfor ", where are all images in Format "Format24bppRgb". At least I am comparing with Matching.bp2(Bitmap largeImage, Bitmap template), if it is true I am adding in "add" array address of this Image. This "Compare" Method return Array. In this array is written all pathes which are part of Bitmap Image. But this code donn't works. It cann't identifi which Image which Bitmap contains. But in all Arrays Infromation is written correctly, for example in "mis","bitfor". Everything is working correctly till "Matching.bp2(bitmap, bitfor[i])" Can anyone Help me with this ?

Here is a Code :

Code: Select all
public static class imageCompare
    {
        public static string[] Compare(Bitmap bitmap)
        {
            string[] mis = Directory.GetFiles("C:\\Users\\Dave\\Documents\\Visual Studio 2012\\Projects\\Bura\\kartebi\\Kartebi", "*.png");
           
            string[] add = new string[mis.Length];
           
            Bitmap[] bpp = new Bitmap[36];

            Bitmap[] bitfor = new Bitmap[36];
            //
            for (int i = 0; i < bitfor.Length; i++)
            {
                bpp[i] = (Bitmap)System.Drawing.Image.FromFile(mis[i]);
                bitfor[i] = Form1.ConvertToFormat(bpp[i], System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            }

            for (int i = 0; i < bitfor.Length; i++)
            {
                if (Matching.bp2(bitmap, bitfor[i]))
                    add[i] = mis[i];
                else
                    add[i] = null;
            }
            return add;
           
        }
    }
public static class Matching
    {
        public static bool bp2(this Bitmap template, Bitmap bmp)
        {
            const int divisor = 4;
            const int epsilon = 10;

            ExhaustiveTemplateMatching etm = new ExhaustiveTemplateMatching(0.9f);
            TemplateMatch[] tm = etm.ProcessImage(new ResizeNearestNeighbor(template.Width / divisor, template.Height / divisor).Apply(template),
                                                  new ResizeNearestNeighbor(bmp.Width / divisor, bmp.Height / divisor).Apply(bmp));
            if (tm.Length == 1)
            {
                Rectangle tempRect = tm[0].Rectangle;

                if (Math.Abs(bmp.Width / divisor - tempRect.Width) < epsilon && Math.Abs(bmp.Height / divisor - tempRect.Height) < epsilon)
                {
                    return true;
                }
            }
            return false;
        }
    }


I call this Method :
Code: Select all
string[] path = new string[36];
                Bitmap sc = bp(Convert.ToInt32(textBox4.Text), Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox6.Text) - Convert.ToInt32(textBox4.Text), Convert.ToInt32(textBox7.Text) - Convert.ToInt32(textBox5.Text));
                Bitmap sc2 = ConvertToFormat(bp(Convert.ToInt32(textBox4.Text), Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox6.Text) - Convert.ToInt32(textBox4.Text), Convert.ToInt32(textBox7.Text) - Convert.ToInt32(textBox5.Text)), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                path = imageCompare.Compare(sc2);
dave_bulac
 
Posts: 3
Joined: Sun Sep 27, 2015 8:46 am



Return to AForge.NET Framework