Good day, gentlemen! The essence of the question is as follows: I process the photo pixel by pixel:

Bitmap bit = new Bitmap(StartImage); Size size = bit.Size; for (int y = 0; y < size.Height; y++) for (int x = 0; x < size.Width; x++) { Color c = bit.GetPixel(x, y); ... Некоторые действия с пикселем ... } 

It took the opportunity to exclude some areas from the processing area. Well, I thought that, supposedly, a garbage question: now let's input an array of points that we don’t need to process, but before getting a pixel and processing it

 if (ExceptPoints.Contains(new Point(x,y))) 

and it's in the hat! But it was not there. The method began to work just to hell slowly (and when I selected half of the entire image for testing, I didn’t wait for the result for 4 minutes). I tried to implement multithreading to solve this problem. But I almost burned down the CPU (99% of the load). In general, you need to somehow optimize this thing (or implement non-cluttered multithreading), but there’s no idea how to do this. So I appeal to you for advice on this difficult matter!

PS - exclusion areas are initially set in circles with a random radius. But what to check, whether the point enters one of the circles, what to check if it is among the array containing all points of these circles, one thing comes out in time

  • 2
    Strangely, checking for entry into the circle should be pretty quick. Try to put points in HashSet<Point> , the search in it should be almost instant. - VladD
  • 3
    If you need really fast processing, forget about the slow snail GetPixel , and use LockBits (there’s an example below). - VladD
  • @VladD, thank you very much! Understood! The usual image processing time fell from 3 seconds to less than a tenth of a second! And thanks to your advice with HashSet, regardless of the number of elements inside it, the processing speed does not increase at all! - Kir_Antipov
  • @Kir_Antipov: Great! Then make out as an answer. - VladD

1 answer 1

There are two ways to accelerate.

First, instead of checking for occurrences in the List<Point> (or Point[] ), which is linear in the number of checked points, collect points in a HashSet<Point> that has a very fast (O (1)) check for occurrence.

Secondly, for working with images it is best to refuse very slow GetPixel / SetPixel . Instead, it makes sense to get raw data using LockBits (there is a good example in the MSDN article) and work with them.