Click here to Skip to main content
16,012,821 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys. I am working on my project in Windows Forms and cann't make this code work correctly.

I want to compare Bitmap image with Images in File "C:\\Users\\Dave\\Documents\\Visual Studio 2012\\Projects\\Bura\\kartebi\\Kartebi" . I have made two Class. First is "Matching" class, which is comparing twi Bitmap Images. 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 path which are in "Kartebi" Folder. Than I am creating one more array "bitfor ", where are all images. 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". Can anyone Help me with this ?

Here is a Code :

C#
    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;
        }
    }
Posted
Updated 26-Sep-15 9:45am
v2

Short answer: Simple, this code don't do what you think it should.

It is either a bug or a misunderstanding.

First, go to author (as suggested in solution 1) and see if you understood what it should do or have a bug.

Second, try to understand what is wrong.
Prepare some sets of pictures that should match and other sets that shouldn't?
Then, run them with the debugger step by step and see when it goes wrong.

Advice: organise your question in a few paragraphs rather a one big.
 
Share this answer
 
Why aren't you asking this question on the AForge support forum ... since that is where 'ExhaustiveTemplateMatching comes from ?

Like this AForge user did: [^].
 
Share this answer
 
Comments
Maciej Los 26-Sep-15 17:58pm    
Short and to the point! +5!
How are you, Bill? Where have you been?
Cheers, Maciej.
BillWoodruff 26-Sep-15 18:32pm    
Hi Maciej, thanks for asking about this flea on a tick in the ear of a mangy homeless dog ! I've been very busy with trying to finish a novel, getting ready for eye surgery, and trying to upgrade my C# skills to a new level. Haven't had the motivation/energy to contribute much to QA here, and plan to focus on publishing some articles. cheers, Bill
Maciej Los 26-Sep-15 18:40pm    
Wow, seems you've been really busy, Bill. What kind of novel you're writing?
BillWoodruff 26-Sep-15 20:49pm    
It's titled: "The Autobiography of a Liar" :) post-modern, surreal. thanks, Bill
Maciej Los 27-Sep-15 6:41am    
The title of your novel is very interesting and alluring. I would like to buy it. Let me know if it will be possible to buy it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900