I need to make an affine transformation and afterwards use nearest neighbor interpolation whereas holding the identical dimensions for enter and output photos. I take advantage of for instance the scaling transformation T= [[2,0,0],[0,2,0],[0,0,1]]. Any concept how can I fill the black pixels with nearest neighbor ? I tryied giving them the min worth of neighbors’ intensities. For ex. if a pixel has neighbors [55,22,44,11,22,55,23,231], I give it the worth of min depth: 11. However the outcome is just not something clear..
import numpy as np
from matplotlib import pyplot as plt
#Importing the unique picture and init the output picture
img = plt.imread('/house/left/Desktop/computerVision/SET1/mind0030slice150_101x101.png',0)
outImg = np.zeros_like(img)
# Dimensions of the enter picture and output picture (the identical dimensions)
(width , peak) = (img.form[0], img.form[1])
# Initialize the transformation matrix
T = np.array([[2,0,0], [0,2,0], [0,0,1]])
# Make an array with enter picture (x,y) coordinations and add [0 0 ... 1] row
coords = np.indices((width, peak), 'uint8').reshape(2, -1)
coords = np.vstack((coords, np.zeros(coords.form[1], 'uint8')))
output = T @ coords
# Arrays of x and y coordinations of the output picture throughout the picture dimensions
x_array, y_array = output[0] ,output[1]
indices = np.the place((x_array >= 0) & (x_array < width) & (y_array >= 0) & (y_array < peak))
# Remaining coordinations of the output picture
fx, fy = x_array[indices], y_array[indices]
# Remaining output picture after the affine transformation
outImg[fx, fy] = img[fx, fy]
The enter picture is:
The output picture after scaling is: