Home > Software engineering >  What is this interpolation called?
What is this interpolation called?

Time:07-10

I'm working on images, let's say that i have a row of the image matrix that has the values:

[1, 2, 3, 4, 5, 6, 7]

I want to resize this image using interpolation so that the row becomes something like:

[1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7]

Could someone tell me what is this interpolation technique called, and how can i possibly use it? I tried PIL.Image.resize resampling filters but they don't give me the results i'm looking for.

Thank you in advance!

CodePudding user response:

This doesn't look like an interpolation but rather a repetition.

You can use a custom repeater and numpy.repeat:

a = np.array([1, 2, 3, 4, 5, 6, 7])
MAX, r = divmod(a.shape[0], 2)
rep = np.arange(1, MAX r 1).astype(int)
rep = np.r_[rep[r:][::-1], rep]

out = np.repeat(a, rep)

output: array([1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7])

N-dimensional

a = np.arange(20).reshape(4, 5)

def custom_repeat(arr, axis=0):
    MAX, r = divmod(arr.shape[axis], 2)
    rep = np.arange(1, MAX r 1).astype(int)
    rep = np.r_[rep[r:][::-1], rep]
    return np.repeat(arr, rep, axis=axis)
    
custom_repeat(custom_repeat(a, axis=0), axis=1)

output:

array([[ 0,  0,  0,  1,  1,  2,  3,  3,  4,  4,  4],
       [ 0,  0,  0,  1,  1,  2,  3,  3,  4,  4,  4],
       [ 5,  5,  5,  6,  6,  7,  8,  8,  9,  9,  9],
       [10, 10, 10, 11, 11, 12, 13, 13, 14, 14, 14],
       [15, 15, 15, 16, 16, 17, 18, 18, 19, 19, 19],
       [15, 15, 15, 16, 16, 17, 18, 18, 19, 19, 19]])

CodePudding user response:

Maybe this is what you're looking for

def my_procedure(current_list: list, index: int) -> list:
    index_copy = index
    table = []
    status_add = True

    for i in current_list:
        for _ in range(index):
            table.append(i)
        if status_add:
            index -= 1
        else:
            index  = 1
        if index == 1 or index == index_copy:
            status_add = not status_add
    return table


x = [1, 2, 3, 4, 5, 6, 7]

print(my_procedure(x, 4))
  • Related