Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have a method, that is used to extract difference from parent image. It works fine, but the problem is, it eats more time. Please tell me a faster way..!!!
 
My Method:
public static Bitmap GetDifferences(this Image img1, Image img2)
{
    Bitmap parentImage = (Bitmap)img1.GetGrayScaleVersion();
    Bitmap comparer = (Bitmap)img2.GetGrayScaleVersion();
    Bitmap difference = (Bitmap)img2;
 
    difference.MakeTransparent(Color.Violet);
    for (int y = 0; y < img1.Height; y++)
    {
        for (int x = 0; x < img1.Width; x++)
        {
            if ((parentImage.GetPixel(x, y).R - comparer.GetPixel(x, y).R) == 0)
            {
                difference.SetPixel(x, y, Color.Transparent);
            }
        }
    }
 
    return difference;
}
 
Dependency Method:
public static Image GetGrayScaleVersion(this Image original)
{
    //http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale
    //create a blank bitmap the same size as original
    Bitmap newBitmap = new Bitmap(original.Width, original.Height);
 
    //get a graphics object from the new image
    Graphics g = Graphics.FromImage(newBitmap);
 
    //create some image attributes
    ImageAttributes attributes = new ImageAttributes();
 
    //set the color matrix attribute
    attributes.SetColorMatrix(ColorMatrix);
 
    //draw the original image on the new image
    //using the grayscale color matrix
    g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
        0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
 
    //dispose the Graphics object
    g.Dispose();
 
    return newBitmap;
 
}
Posted 30-Jan-13 4:14am
Edited 30-Jan-13 4:17am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Simple. Don't ever use GetPixel/SetPixel. They're VERY slow compared to doing the same operations accessing the pixel data arrays.
 
Search the articles for "Image processing for dummies" and you'll find a bunch of articles on how to do it, MUCH faster.
  Permalink  
Comments
Balaganesan Ravichandran at 31-Jan-13 4:06am
   
Thanks Dave, Your reply really helps me to figure out the issue, I`m still not getting your point fully (Processing with pixel data arrays).
But Bitmap.LockBits did works for me. Now function runs 2x faster. Still not statisfied.. :-(
Dave Kreskowiak at 31-Jan-13 12:51pm
   
It should run a LOT faster than that, so I think you need to re-read those articles and rethink how your doing what it is you are.
 
Without seeing your code, it's impossible to tell you what to do.

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

  Print Answers RSS
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 BillWoodruff 159
4 Afzaal Ahmad Zeeshan 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100