I
I
IA-NE-ROBOT2017-04-26 14:16:17
Graphic arts
IA-NE-ROBOT, 2017-04-26 14:16:17

C# - Find a pixel/group of pixels in a Bitmap?

Hello, there is a certain bitmap in which you need to find a pixel with a certain color. Searching by enumeration:
for (int w = 0; w < bmap.Width; w++)
{
for (int h = 0; h < bmap.Height; h++)
{
clr_ = bmap.GetPixel(w, h).R + bmap. GetPixel(w, h).G + bmap.GetPixel(w, h).B;
.....
}
}
On a 1920x1080 image, it takes about 5 seconds. How can you speed up? It is also interesting if the search is not for 1 pixel, but for a group (sprite) ...

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
IA-NE-ROBOT, 2017-05-01
@IA-NE-ROBOT

yes, lockbits. The finished solution was here: www.cyberforum.ru/windows-forms/thread1045100.html

P
Peter, 2017-04-26
@petermzg

Use the Bitmap.LockBits
How to apply method here

A
Alexander Ter, 2017-04-26
@alexsandr0000

Break the bitmap into sectors and process each sector in a separate thread (ThreadPool, Parallel or Task with the launch of child tasks and the ability to cancel when found) + as mentioned above, useBitmap.LockBits.

F
Forkfant, 2017-04-30
@Forkfant

At least do not call bmap.GetPixel(w, h)three times in a row. It's better to change the code like this:

Color color = bmap.GetPixel(w, h);
clr_ = color.R + color.G + color.B;

And it's even more correct to use Bitmap.LockBits as Peter suggested.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question