Я делюсь идеей с массивами помощников, используя простую линейную функцию:
# helper arrays for coordinate system
x = np.ones((100,100))
x[:,:] = np.arange(100)
y = np.ones((100,100))
y[:,:] = 100-np.arange(100).reshape(100,1) # 100- to invert y-axis
# linear function
def linfunc(x, m, n):
return x*m + n
Идея состоит в том, чтобы вызвать линейное преобразование для координат x
, а затем проверить, где результат равендо, меньше или больше, чем y
координаты.Затем результат этого утверждения можно использовать для индексации входных изображений.
#test data:
a = np.ones((100,100))
b = np.zeros((100,100)) + 2
ab_mean = (a+b)/2
Контрольные примеры:
test_line = linfunc(x, 1, 0) # y = x
output = np.zeros_like(a)
output[y>test_line] = a[y>test_line] # assign above line to a
output[y<test_line] = b[y<test_line] # assign below line to b
output[y==test_line] = ab_mean[y==test_line] # assign coords on line to "interpolation"
plt.imshow(output)
![enter image description here](https://i.stack.imgur.com/LBMdu.png)
test_line = linfunc(x, 1, 10) # y = x + 10
output = np.zeros_like(a)
output[y>test_line] = a[y>test_line] # assign above line to a
output[y<test_line] = b[y<test_line] # assign below line to b
output[y==test_line] = ab_mean[y==test_line] # assign coords on line to "interpolation"
plt.imshow(output)
![enter image description here](https://i.stack.imgur.com/xTk1R.png)
test_line = linfunc(x, -4, + 150) # y = -4x + 150
output = np.zeros_like(a)
output[y>test_line] = a[y>test_line] # assign above line to a
output[y<test_line] = b[y<test_line] # assign below line to b
output[y==test_line] = ab_mean[y==test_line] # assign coords on line to "interpolation"
plt.imshow(output)
![enter image description here](https://i.stack.imgur.com/SPuWT.png)