0

Given a 2d array of size (width=x, height=y), where each row contains entries with a height information in meters each. The distance in meters between two horizontally neighbored entries in a row varies by y-position. Thus in one row the distance between two entries is 30m, where in a row above the distance e.g. is 31m.

How do i resample and interpolate the 2d array to have the horizontal distance between pixels equal a given value for each row? If possible, are there multiple options for interpolating?

Carsten Drösser
  • 496
  • 1
  • 4
  • 16
  • Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) showing what you have and what you're trying to get. – jared Jul 23 '23 at 15:29

1 Answers1

0

I think you can solve this with 4-steps:

  1. Calculate the desired number of pixels per row based on your target distance.
  2. Choose an interpolation method(linear, cubic, etc.)
  3. Use NumPy and SciPy libraries for interpolation.
  4. Resample each row using the chosen interpolation method to get the new pixel values.
    import numpy as np
    from scipy import interpolate
    
    original_array = ...  # your 2D array
    
    # Step 1: 
    target_distance = 1.0
    num_pixels_per_row = []

    for row in original_array:
        original_distance = sum(np.diff(row))
        num_pixels = int(original_distance / target_distance)
        num_pixels_per_row.append(num_pixels)
    
    # Step 2:   
    interpolation_method = 'linear'
    
    # Step 3:
    resampled_array = np.zeros((original_array.shape[0],
                                max(num_pixels_per_row)))

    for i, (row, num_pixels) in enumerate(zip(original_array, num_pixels_per_row)):
        x_original = np.arange(row.size)
        x_new = np.linspace(0, row.size - 1, num=num_pixels)
    
        interpolator = interpolate.interp1d(x_original, row,
                                            kind=interpolation_method)
        resampled_array[i] = interpolator(x_new)

Interpolation is an estimation technique, and the accuracy of the resampled data will depend on the characteristics of your original dataset and the chosen interpolation method. It's always a good idea to visualize the results and check if they make sense for your specific application. Additionally, if you have a large dataset, consider using more efficient algorithms or optimizations to speed up the process.