E
E
egorkozelskij2017-10-05 20:06:58
Algorithms
egorkozelskij, 2017-10-05 20:06:58

How to find all borders of objects described pixel by pixel?

Hello!
There is a two-dimensional array with pixels of two colors.
White - unoccupied space
Black - obstacles
Obstacles are some kind of shapes - squares, circles, etc.
I need to find only the border pixels of all the shapes in a fairly fast time.
Prompt good algorithms?
While doing this:
I run through all the black pixels and check if it has at least one black neighbor. If there is, then this is the boundary pixel.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2017-10-05
@kttotto

If this is a two-dimensional array, where each pixel has coordinates, then why not take the indexes of the first black and the last black from each row.
In C# it would look like this

var arr = new bool[100][];
var contour = new List<List<int>>();

for (var y = 0; y < 100; y++)
{
  var firstX = Array.IndexOf(arr[y], true);
  var lastX = Array.LastIndexOf(arr[y], true);

  if(firstX != -1)
     contour.Add(new List<int> { firstX, y });

  if (lastX != -1)
     contour.Add(new List<int> { lastX, y });
 }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question