0
votes

I want to make an affine transformation and afterwards use nearest neighbor interpolation while keeping the same dimensions for input and output images. I use for example the scaling transformation T= [[2,0,0],[0,2,0],[0,0,1]]. Any idea how can I fill the black pixels with nearest neighbor ? I tryied giving them the min value of neighbors' intensities. For ex. if a pixel has neighbors [55,22,44,11,22,55,23,231], I give it the value of min intensity: 11. But the result is not anything clear..

import numpy as np
from matplotlib import pyplot as plt

#Importing the original image and init the output image
img = plt.imread('/home/left/Desktop/computerVision/SET1/brain0030slice150_101x101.png',0)
outImg = np.zeros_like(img)

# Dimensions of the input image and output image (the same dimensions)
(width , height) = (img.shape[0], img.shape[1])

# Initialize the transformation matrix
T = np.array([[2,0,0], [0,2,0], [0,0,1]])

# Make an array with input image (x,y) coordinations and add [0 0 ... 1] row
coords = np.indices((width, height), 'uint8').reshape(2, -1)
coords = np.vstack((coords, np.zeros(coords.shape[1], 'uint8')))

output = T @ coords

# Arrays of x and y coordinations of the output image within the image dimensions
x_array, y_array = output[0] ,output[1]
indices = np.where((x_array >= 0) & (x_array < width) & (y_array >= 0) & (y_array < height))

# Final coordinations of the output image
fx, fy = x_array[indices], y_array[indices]

# Final output image after the affine transformation
outImg[fx, fy] = img[fx, fy]

The input image is:

enter image description here

The output image after scaling is:

enter image description here

2

2 Answers

0
votes

well you could simply use the opencv resize function

import cv2 
new_image = cv2.resize(image, new_dim, interpolation=cv.INTER_AREA)

it'll do the resize and fill in the empty pixels in one go

more on cv2.resize

0
votes

If you need to do it manually, then you could simply detect dark pixels in resized image and change their value to mean of 4 neighbour pixels (for example - it depends on your required alghoritm) See: nereast neighbour, bilinear, bicubic, etc.