473,378 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Fast image processing in C#

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
15 18683
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
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
"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
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
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
"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

"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
"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
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
"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

"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
"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alexis Gallagher | last post by:
(I tried to post this yesterday but I think my ISP ate it. Apologies if this is a double-post.) Is it possible to do very fast string processing in python? My bioinformatics application needs to...
2
by: Maxwell2006 | last post by:
Hi, I am developing a simple image upload asp.net page and I am looking for a simple image processing component to be able to change the image resolution of jpg images and also reduce the...
10
by: stonny | last post by:
Hi, I am converting my matlab program into C/C++. I need to change some image processing toolbox functions into C/C++, such as edge detection, mathematical morphology. Is there any place that I...
5
by: edurand | last post by:
Hi, We are are pleased to announce the version 3.0 of the image processing library 'Filters'. You can use it in Python, and we have provided tutorials and samples in Python, with for exemple...
0
by: adubra | last post by:
Hi there, I am using a device context (DC) and a buffer to successfully draw to screen. However, when I update the DC at very high frame rate and drag the frame containing the image very quickly...
3
by: birensubudhi | last post by:
hey guys,can anyone tell me what is image processing, how to do it using C. IN KSHITIJ 2007 held at IIT kgp a que by INFOSYS is asked, they hav given an image then cut in different orientation...
5
by: whisk3rs | last post by:
Hello, I have a conceptual question. I need to write a program that will take as input a list of images and then process each image individually (extract useful features from the image) ...
0
by: tavares | last post by:
(Our apologies for cross-posting. We appreciate if you kindly distribute this information by your co- workers and colleagues.) ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.