Sign in | Join | Help
Καλωσήλθατε στο StudentGuru!

Creating an Image Processing Library with C# – Part 2

Creating an image Processing Library – Part 1

ASP.NET Demo Application

In the first post we covered the basics of Convolution operation and how it can be applied on an image. I posted an implementation (SafeImageConvolution) of the algorithm together with some helping classes we needed and the Sharpen filter. In this post, I'm going to show a much more efficient implementation of the algorithm. If you apply the Sharpening Filter from the previous post, using the SafeImageConvolution function, on a 1024x768, you will notice that it takes 6-7 seconds for the operation to complete. That’s, at least, unacceptable. The cause of this delay are the two GDI+ functions: Bitmap.GetPixel() and Bitmap.SetPixel().

So, here is a new implementation of the same algorithm. This time, i use pointer arithmetic to iterate through the image.

Let’s see some important points on the above code. First of all, the Bitmap.LockBits() locks the Bitmap to the memory and returns a BitmapData class which contains, among others, the following 2 properties that are later used: Scan0 and Stride. Scan0 is the address (IntPtr) of the first pixel in the bitmap. Stride is slightly more complicated to understand, but basically is the width of each row of pixels, rounded up to a 4-byte boundary. See the following image to better understand it:

Stride and Scan0

If we have a 24bpp (= 3 Bytes per pixel) image with a width of 130 pixels then the actual pixel data on each row are 130 * 3 = 390 bytes, but the stride will be 392 (rounded up to 4-bytes). So we have 2 unused pixels on each row. Based on the above, to get the pixel at (X, Y) we can use the following formula:

pixel = Scan0 + (Y * Stride) + (X * PixelSize);

where PixelSize is the size of each pixel in bytes. This leads me to the second thing i want to explain on the code above.

You can see that i use the methods Bitmap.GetPixelSize() and Bitmap.HasAlpha(). As you may now, the Bitmap class doesn’t include these methods, i have implemented them as Extension methods and they can be found in the BitmapExtensions.cs file.

One last important thing to notice is that the pixels are saved in a BGR format, which means that Blue is the first byte, Green the second and Red the third (with a possible 4th byte for alpha channel).

You can find the whole source code, together with a Windows Forms demo application that uses the library. Feel free to send me any comments you have, suggestions or bugs that you may find.
Download 

Download

Share/Bookmark
Published Κυριακή, 20 Δεκεμβρίου 2009 3:40 μμ by jupiter
Filed under: ,

Comments

# Twitter Trackbacks for Creating an Image Processing Library with C# ??? Part 2 - Jupiter's Blog [studentguru.gr] on Topsy.com

Pingback from  Twitter Trackbacks for                 Creating an Image Processing Library with C# ??? Part 2 - Jupiter's Blog         [studentguru.gr]        on Topsy.com

# re: Creating an Image Processing Library with C# – Part 2

πολύ καλό μπράβο!!!

Τρίτη, 22 Δεκεμβρίου 2009 1:04 πμ by infamous

# re: Creating an Image Processing Library with C# – Part 2

Not bad, but one thing is unacceptable: using image.GetPixelSize() to determine how many bytes there are in a pixel when calculating pixel position, but assuming rgba when applying the convolution. Either hardcode pixelSize = 4, or add another for loop.

Δευτέρα, 8 Μαρτίου 2010 10:42 πμ by Leszek

# re: Creating an Image Processing Library with C# – Part 2

@Leszek: Thanks for your comment. You are right. I created the library just for experimenting and i didn't bother to change this. I plan to upload it on codeplex and will fix this and some other things before i do it!

Δευτέρα, 8 Μαρτίου 2010 11:56 πμ by jupiter

# Image Processing Library – ASP.NET Website Demo

ASP.NET Demo site using Image Processing Library

Σάββατο, 27 Μαρτίου 2010 5:49 μμ by Jupiter's Blog

# Creating an Image Processing Library with C# ??? Part 1 - Jupiter's Blog

Pingback from  Creating an Image Processing Library with C# ??? Part 1 - Jupiter's Blog

Πέμπτη, 20 Μαΐου 2010 11:51 μμ by Creating an Image Processing Library with C# ??? Part 1 - Jupiter's Blog

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Submit