Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fast image processing in C#

0 views
Skip to first unread message

Michael A. Covington

unread,
Mar 14, 2007, 8:27:37 PM3/14/07
to
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.


sherifffruitfly

unread,
Mar 14, 2007, 9:53:19 PM3/14/07
to
On Mar 14, 5:27 pm, "Michael A. Covington"

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

Michael A. Covington

unread,
Mar 14, 2007, 11:01:38 PM3/14/07
to
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.


Michael C

unread,
Mar 15, 2007, 12:07:27 AM3/15/07
to
"Michael A. Covington" <lo...@ai.uga.edu.for.address> wrote in message
news:u947C5qZ...@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.
>
>


sherifffruitfly

unread,
Mar 18, 2007, 4:32:38 PM3/18/07
to
On Mar 14, 9:07 pm, "Michael C" <nos...@nospam.com> wrote:
> "Michael A. Covington" <l...@ai.uga.edu.for.address> wrote in messagenews:u947C5qZ...@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/FAQ-DOTNET-Unmanaged-Unsafe-Code-Differences

Message has been deleted

Michael C

unread,
Mar 18, 2007, 6:39:49 PM3/18/07
to
"Michael A. Covington" <lo...@ai.uga.edu.for.address> wrote in message
news:uuow18Za...@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


Michael A. Covington

unread,
Mar 18, 2007, 7:40:21 PM3/18/07
to

"Michael C" <nos...@nospam.com> wrote in message
news:O%23r9v0aa...@TK2MSFTNGP04.phx.gbl...

> "Michael A. Covington" <lo...@ai.uga.edu.for.address> wrote in message
> news:uuow18Za...@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.


Michael C

unread,
Mar 18, 2007, 8:03:59 PM3/18/07
to
"Michael A. Covington" <lo...@ai.uga.edu.for.address> wrote in message
news:OC1NNbba...@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


Michael A. Covington

unread,
Mar 19, 2007, 9:51:44 AM3/19/07
to
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?


Michael C

unread,
Mar 19, 2007, 6:34:10 PM3/19/07
to
"Michael A. Covington" <lo...@ai.uga.edu.for.address> wrote in message
news:eT%23682ia...@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


Michael A. Covington

unread,
Mar 19, 2007, 8:56:48 PM3/19/07
to

"Michael C" <nos...@nospam.com> wrote in message
news:eFxpPWna...@TK2MSFTNGP02.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.

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


Michael C

unread,
Mar 19, 2007, 10:43:22 PM3/19/07
to
"Michael A. Covington" <lo...@ai.uga.edu.for.address> wrote in message
news:e4UYlqo...@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. :-)
>
>


Michael A. Covington

unread,
Mar 19, 2007, 11:02:39 PM3/19/07
to

"Michael C" <nos...@nospam.com> wrote in message
news:eEtKahpa...@TK2MSFTNGP06.phx.gbl...

> "Michael A. Covington" <lo...@ai.uga.edu.for.address> wrote in message
> news:e4UYlqo...@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.


Rao Muhammad Rafique

unread,
Jul 17, 2007, 1:18:57 AM7/17/07
to
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:
rao2...@yahoo.com

Rao Muhammad Rafique

unread,
Jul 17, 2007, 1:21:09 AM7/17/07
to
0 new messages