Fast image processing in C#

  • Thread starter Michael A. Covington
  • Start date
M

Michael A. Covington

Any thoughts about how to implement image processing algorithms to run fast
in C#?

Using GetPixel to access every pixel from a Bitmap seems to be rather
time-consuming.
 
M

Michael A. Covington

Thanks. After posting my message I realized there were several examples on
The Code Project.

I'm going to look at them. Unsafe code might be the right thing to use.
 
M

Michael C

Michael A. Covington said:
Thanks. After posting my message I realized there were several examples
on The Code Project.

I'm going to look at them. Unsafe code might be the right thing to use.

Depends on what you're doing. Converting to grayscale for example can be
done all in managed with similar speed.
 
M

Michael A. Covington

Here's what I wound up creating:

www.ai.uga.edu/mc/CovingtonImageProcessing.zip

It's only moderately fast, but fast enough for my purposes. I ended up using
LockBits and Marshal.Copy to copy the image data into an array, then doing
the opposite after processing. That way I don't have to do a method call on
every single pixel, especially when doing things like convolutions.

This is released to the public domain on the condition that I'm not expected
to support it! I plan to do some image processing experiments of my own
using this program as a platform.

Thanks to all who made useful suggestions.
 
M

Michael C

Michael A. Covington said:
Here's what I wound up creating:

www.ai.uga.edu/mc/CovingtonImageProcessing.zip

It's only moderately fast, but fast enough for my purposes. I ended up
using LockBits and Marshal.Copy to copy the image data into an array, then
doing the opposite after processing.

There is no reason to do this in c#, it slows the whole process down.
That way I don't have to do a method call on every single pixel,
especially when doing things like convolutions.

I don't see how it makes any difference. If you're doing image processing
you'll have to look at every pixel whether it's in an array or not.

Michael
 
M

Michael A. Covington

Michael C said:
There is no reason to do this in c#, it slows the whole process down.

Are you saying I shouldn't use C# or shouldn't use LockBits and
Marshal.Copy? I know it's possible to use pointers also.
I don't see how it makes any difference. If you're doing image processing
you'll have to look at every pixel whether it's in an array or not.

It makes a tremendous difference not to have to call GetPixel every time I
want to see the value of a pixel, particularly when doing matrix
convolutions.

Anyhow, the program is now fast enough for my purposes.
 
M

Michael C

Michael A. Covington said:
Are you saying I shouldn't use C# or shouldn't use LockBits and
Marshal.Copy? I know it's possible to use pointers also.

You should use C# and LockBits but not Marshal.Copy. As you've suggested you
should use pointers instead. If you're unfamiliar it's fairly simple:

byte* ptr= (byte*)Data.Scan0;
int offset = data.stride - data.width * 3;//for 24bit bitmap
for(y = 0; y < height; y++, ptr += offset)
{
for(x = 0; x > width; x++, ptr += 3)
{
byte r = data*;
//change r, then
data* = r
}
}

Michael
 
M

Michael A. Covington

Noted. I was aware of both techniques but didn't know they'd take
appreciably different amounts of time. In your experience, how much
difference does it make?
 
M

Michael C

Michael A. Covington said:
Noted. I was aware of both techniques but didn't know they'd take
appreciably different amounts of time. In your experience, how much
difference does it make?

I'm not sure as I haven't timed it. I'd imagine it would be fairly
significant as you are copying the entire bitmap twice and then doing array
lookups instead of direct pointer access. There'd be a fair amount of checks
added to the array lookup and pretty much none for the pointer. But if you
need to avoid unsafe code then the arrays could be useful (assuming they do
in fact avoid unsafe code).

Michael
 
M

Michael A. Covington

Michael C said:
I'm not sure as I haven't timed it. I'd imagine it would be fairly
significant as you are copying the entire bitmap twice and then doing
array lookups instead of direct pointer access. There'd be a fair amount
of checks added to the array lookup and pretty much none for the pointer.
But if you need to avoid unsafe code then the arrays could be useful
(assuming they do in fact avoid unsafe code).

Ah. My goal is in fact to deliver the image to the user in an ordinary int
array. The program is a research test bed for developing graphics
algorithms, so I need to express the algorithms as simply as possible and
run them with checking in place.

I thought you were suggesting copying the image to the array using pointers
instead of using Marshal.Copy (which is already very fast).
 
M

Michael C

Michael A. Covington said:
Ah. My goal is in fact to deliver the image to the user in an ordinary
int array. The program is a research test bed for developing graphics
algorithms, so I need to express the algorithms as simply as possible and
run them with checking in place.

Why are you doing it that way?
I thought you were suggesting copying the image to the array using
pointers instead of using Marshal.Copy (which is already very fast).

No, you won't get any faster if you do the copy. I was suggesting
eliminating the copy. :)
 
M

Michael A. Covington

Michael C said:
Why are you doing it that way?

As I said, to test algorithms. I am interested in the computation, not (at
this stage) the fastest implementation of it. It may end up implemented
some totally different way.
 
R

Rao Muhammad Rafique

Hi dear,

you can use BitmapData class along with LocBits() and UnLock() methods.also
with unsafe context and pointers.that make the image processing fast.

further detail contact:
(e-mail address removed)
 
R

Rao Muhammad Rafique

Hi dear,

you can use BitmapData class along with LocBits() and UnLock() methods.also
with unsafe context and pointers.that make the image processing fast.

further detail contact:
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top