0

What I'm trying to do is generate multiple lines on a binary image based on a length and angle. Then return all of the row/column values along with pixel values underneath those lines and place them into a python list. To generate those lines I wrote a function that outputs start and end coordinates of each line. Using these coordinates I want to generate the lines and extract the values.

To extract values from a horizontal line from pixel (0,1) to (3,1) I can do:

a = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
pixels = a[1, 0:3]

or vertical:

pixels = a[0:3, 1]

which returns an array of all the pixel values underneath that line:

array([3, 4, 5])
array([1, 4, 7])

How could I apply this method on lines with an angle? so with an x1,y1 and x2,y2? These return (syntax) errors:

a([0,0], [2,2])
a([0,0]:[2,2])
a[0,0:2,2]

I'm looking for something similiar as 'improfile' in Matlab. Many thanks for your help!

cf2
  • 581
  • 1
  • 7
  • 17
  • 1
    duplicate of http://stackoverflow.com/questions/7878398/how-to-extract-an-arbitrary-line-of-values-from-a-numpy-array ? – Ed Smith May 09 '16 at 12:52
  • Thank you for the link. I had not found that topic yet, very helpful! – cf2 May 10 '16 at 11:50

1 Answers1

1

You can use scikits-image's draw module and the draw.line method:

>>> from skimage.draw import line
>>> y0 = 1; x0 = 1; y1 = 10; x1 = 10;
>>> rr, cc = line(y0, x0, y1, x1)

rr and cc will contain row and column indexes of the values from which the line passes. You can access those values as:

>>> values = img[rr, cc]

Assuming img is the name of your image.

Note that this implementation does not offer interpolation or subpixel accuracy for angles different from 45 degree intervals. It will create a discrete stepped line between points A and B that passes through whole pixels.

Imanol Luengo
  • 15,366
  • 2
  • 49
  • 67