Samuel Bancroft Samuel Bancroft - 1 year ago 62
Python Question

Extract a diagonal cross profile from a 2D Numpy array

If I have a 100x100 2D Numpy array and I want to extract the cross profile that is centred at (50,50) at an angle of 25 degrees (to the horizontal). How can this be achieved?

I have attempted using numpy.diagonal to no avail.

Answer Source

This is what I would do. Assuming your mat is mymat.

import numpy as np

ang = 25

extract = []
for i in range(100):
    j = int(50 + (50 - i) * np.tan(ang * np.pi /180))
    if j<=99 and j>=0:

Numbers should be adjusted for other sizes.