No account yet ?
Tested in Anaconda and Python 3.7
from __future__ import division import cv2 import numpy as np # Nearest neighbor interpolation def nn_interpolate(image, scale_factor): # Extract size (rows, cols, channels) = image.shape scaled_height = rows * scale_factor scaled_weight = cols * scale_factor # Compute ratio row_ratio = rows / scaled_height col_ratio = cols / scaled_weight row_position = np.floor(np.arange(scaled_height) * row_ratio).astype(int) column_position = np.floor(np.arange(scaled_weight) * col_ratio).astype(int) # Initialize scaled image scaled_image = np.zeros((scaled_height, scaled_weight, 3), np.uint8) for i in range(scaled_height): for j in range(scaled_weight): scaled_image[i, j] = image[row_position[i], column_position[j]] return scaled_image # Read image image = cv2.imread('fruits.jpg') nn = nn_interpolate(image, 2) cv2.imwrite('original.jpg', image) cv2.imwrite('Nearest-neighbor-interpolation.jpg', nn)
image-scaling-methods - GitHub
Original image
Nearest neighbor / Voisin le plus proche
Welcome, my name is Eric Soupet and I am the administrator of the site elodees.com. elodees.com is a state of the art of Artificial Intelligence and aims to be collaborative, you can now offer content such as articles, events, tutorials, ... so don't hesitate !
Platform images credit : Pixabay - Pixabay License | Pexels - Pexels License