By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

Fast image processing in C#

P: n/a
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.
Mar 15 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
On Mar 14, 5:27 pm, "Michael A. Covington"
<l...@ai.uga.edu.for.addresswrote:
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.
All of the little I've seen uses Unsafe code.

http://www.codeproject.com/csharp/unsafe_prog.asp
(comments)

http://www.codeproject.com/csharp/quickgrayscale.asp

Mar 15 '07 #2

P: n/a
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.
Mar 15 '07 #3

P: n/a
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:u9**************@TK2MSFTNGP03.phx.gbl...
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.
>

Mar 15 '07 #4

P: n/a
On Mar 14, 9:07 pm, "Michael C" <nos...@nospam.comwrote:
"Michael A. Covington" <l...@ai.uga.edu.for.addresswrote in messagenews:u9**************@TK2MSFTNGP03.phx.gbl. ..
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.
unsafe != unmanaged, right?

http://www.programmersheaven.com/2/F...de-Differences

Mar 18 '07 #5

P: n/a
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.
Mar 18 '07 #6

P: n/a
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:uu**************@TK2MSFTNGP05.phx.gbl...
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
Mar 18 '07 #7

P: n/a

"Michael C" <no****@nospam.comwrote in message
news:O%****************@TK2MSFTNGP04.phx.gbl...
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:uu**************@TK2MSFTNGP05.phx.gbl...
>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.
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.
>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.
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.
Mar 18 '07 #8

P: n/a
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:OC**************@TK2MSFTNGP05.phx.gbl...
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
Mar 19 '07 #9

P: n/a
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?
Mar 19 '07 #10

P: n/a
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:eT****************@TK2MSFTNGP05.phx.gbl...
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
Mar 19 '07 #11

P: n/a

"Michael C" <no****@nospam.comwrote in message
news:eF**************@TK2MSFTNGP02.phx.gbl...
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:eT****************@TK2MSFTNGP05.phx.gbl...
>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).
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).
Mar 20 '07 #12

P: n/a
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:e4*************@TK2MSFTNGP05.phx.gbl...
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. :-)
>

Mar 20 '07 #13

P: n/a

"Michael C" <no****@nospam.comwrote in message
news:eE**************@TK2MSFTNGP06.phx.gbl...
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:e4*************@TK2MSFTNGP05.phx.gbl...
>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?
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.
Mar 20 '07 #14

P: n/a
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:
ra*******@yahoo.com
Jul 17 '07 #15

P: n/a
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:
ra*******@yahoo.com
Jul 17 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.