Croxa
Croxa

Reputation: 81

Finding a path from a thick line

I am trying to find or think of an algorithm that finds a path from a thick line. I think the images make easier to understand what I am trying to do.

Thick line without path

Thick line with past

Given is a 2D array as the picture with values 0 and 1 and I am trying to find the nodes of the lines. Has anybody an idea?

Upvotes: 0

Views: 147

Answers (2)

floupinette
floupinette

Reputation: 312

I recommend using the most famous python library for image treatment : Pillow. https://python-pillow.org/

Some questions to orientate you :

  • Is it really black and white source image ? If No, the first step will be to make each pixel of this picture either black or white (pillow proposes this feature)
  • Is the width of the white pattern constant (i.e. always 15 pixels) ? If No, your program will first need to scan first the whole picture and then to guess the pattern. If Yes, you can guess the pattern while scanning the picture.

But what means "scanning the picture" ? That's the key question. You could check all lines of pixels (from the first line till the last line, and for each line, from left to right), each time you encounter a white pixel you record its coordinates and you record how many white pixels are aside this first white pixel. Doing that, you will get a table where all white pixels are located. Then, it's more about mathemtics than about programming.

Upvotes: 0

MrSmith42
MrSmith42

Reputation: 10151

You could follow the contour and nibble away pixel by pixel (checking that the connectivity stays intact).

If you cannot remove any more pixels, you have a 1 pixel line as wanted.

But the line will most likely have very few long linear segments (unlike in your example)

Upvotes: 1

Related Questions