What am I doing wrong? [Image scanning]

T

trant

I am scanning an an input image and mapping certain zones of the image by a
pre-defined color map to a Zone class. Based on a certain value in this zone
I assign a new color to a copy of the input image

IT seems like a simple process, but in computer terms it takes forever. A
few solid seconds at least.

What am I doing wrong here?

Example code:

public class Zone
{
public int someValue;
}

Dictionary<Color, Zone> colorMap = new Dictionary<Color, Zone>();
// fill colorMap with data

long start = DateTime.Now.Ticks;
Bitmap input = (Bitmap)Bitmap.FromFile(inputfile);
Bitmap output = new Bitmap(map.Width, map.Height);
for (y = 0; y < input.Height; y++)
{
for (int x = 0; x < input.Width; x++)
{
Color c = input.GetPixel(x, y);
if (colorMap.ContainsKey(c))
{
Zone zone = colorMap[c];

if (zone.someValue > 0)
{
if (zone.someValue > 5000)
output.SetPixel(x, y, Color.Cyan);
else if (zone.someValue >= 1000)
output.SetPixel(x, y, Color.Yellow);
else
output.SetPixel(x, y, Color.Red);
}
else
output.SetPixel(x, y, Color.Black);
}
else
output.SetPixel(x, y, Color.Black);

}
}
Console.WriteLine("Scanned map in " + (DateTime.Now.Ticks - start) /
TimeSpan.TicksPerMillisecond);


This here took me 50795 milliseconds!!

The image is 5616 x 2160 dimensions. Not a small image but I can go into
something like Paint.NET and perform filters on this thing in a split second.
 
P

Peter Duniho

Jeff said:
Others will give a lengthier response (hopefully), but I'm almost positive
that THIS is what you're doing wrong:


It's dog slow.

I'm not sure a lengthier response is necessary. That's a full
explanation of why it's slow. :)

Of course, the OP may want to know of alternatives. Using the Bitmap
class, the LockBits() method is the way to obtain a copy of the raw data
on which to operate. There is also the BitmapSource class in the
System.Windows.Media.Imaging namespace, which has a CopyPixels() method
that does roughly the same thing.

In either case, the code will have to handle the given pixel format
explicitly, which is more complicated but definitely worth the
performance increase. If you may have a variety of input formats, you
can either check and handle each possible format, or just convert the
image to a single common format your code can handle before processing.

Pete
 
T

trant

Ah, alright!

I am so glad actually because I was worried about how am I going to convince
users to accept this terrible delay! The solutions seems pretty involved but
I will study it and implement. I am just so happy there is a solution!
 
T

trant

OK so check this out, I updated my code to use the recommended BitLocking...
it took me a bit to understand it and replicate my old code into the new way
but I finally got it working just like it used to except now it takes
4234 milliseconds

Now that's something I can deal with :)

Thanks guys!
 

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