0

I am trying to create a matrix whose elements are the distances to a curve that I have defined (code below):

spiral curve in a matrix

I want to perform some manipulation on this image which gives me a matrix containing all of the minimum Euclidean distances between that point and any point on the spiral.

I have tried using scipy's ndimage.distance_transform_edt like so:

import scipy.ndimage as ndi
transformed = ndi.distance_transform_edt(spiral())

But the output does not give me what I'm looking for!

Does anyone know how to generate this matrix?

Code for spiral generation below:

import numpy as np
import matplotlib.pyplot as plt

def pol2cart(rho, phi):
    # https://stackoverflow.com/questions/20924085/python-conversion-between-coordinates
    x = rho * np.cos(phi)
    y = rho * np.sin(phi)
    return(y, x)

def spiral():

    C = 0.15
    phi = np.linspace(6, 540, 1000)
    rho =  (1 - C * np.log(phi - 5))

    # Now convert back to x, y coordinates
    y, x = pol2cart(rho, np.deg2rad(phi))

    # Center the spiral so we can see it better.
    x -= x.min()
    y -= y.min()
    x += 1
    y += 1.5

    m = np.zeros((100, 100))

    for i in range(len(x)):
        try:

            # Include some scaling factor to increase the size of the curve
            m[int(x[i]*30), int(y[i]*30)] = 1
        except IndexError:
            continue

    return m

plt.imshow(spiral())
HAL 9001
  • 2,835
  • 1
  • 13
  • 22
Clemson
  • 418
  • 4
  • 16

1 Answers1

2

According to this stackoverflow discussion on scipy.ndi.distance_transform_edt(), the function will compute the nearest Euclidean distances for elements of a nonzero matrix to the zero elements.

The issue is that your spiral() function returns a matrix that is nonzero (exactly equal to 1) where the curve exists and 0 everywhere else. To fix this:

import scipy.ndimage as ndi
# The original spiral curve, with 1's where the curve is defined, else 0
s = spiral()
# Transformed data: 0's representing the curve, with 1's everywhere else
TS= 1-s
transformed = ndi.distance_transform_edt(TS)

These efforts result in the following plot:

l2 distances to spiral

HAL 9001
  • 2,835
  • 1
  • 13
  • 22