Logo elodees  elodees

A caring AI for a better world













Only alphabetic characters accented or not as well as the space are accepted

Logo IA




Bilinear interpolation





No account yet ?

Sign up to access all content


Tested in Anaconda and Python 3.7

from __future__ import division
import cv2
import numpy as np
 
# Bilinear interpolation
def bilinear_interpolate(image, scale_factor):
 
	(h, w, channels) = image.shape
	h2 = h  * scale_factor
	w2 = w  * scale_factor
	temp = np.zeros((h2, w2, 3), np.uint8)
	x_ratio = float((w - 1)) / w2;
	y_ratio = float((h - 1)) / h2;
	for i in range(1, h2 - 1): 
		for j in range(1 ,w2 - 1):
			x = int(x_ratio * j)
			y = int(y_ratio * i)
			x_diff = (x_ratio * j) - x
			y_diff = (y_ratio * i) - y
			a = image[x, y] & 0xFF
			b = image[x + 1, y] & 0xFF
			c = image[x, y + 1] & 0xFF
			d = image[x + 1, y + 1] & 0xFF
			blue = a[0] * (1 - x_diff) * (1 - y_diff) + b[0] * (x_diff) * (1-y_diff) + c[0] * y_diff * (1 - x_diff)   + d[0] * (x_diff * y_diff)
			green = a[1] * (1 - x_diff) * (1 - y_diff) + b[1] * (x_diff) * (1-y_diff) + c[1] * y_diff * (1 - x_diff)   + d[1] * (x_diff * y_diff)
			red = a[2] * (1 - x_diff) * (1 - y_diff) + b[2] * (x_diff) * (1-y_diff) + c[2] * y_diff * (1 - x_diff)   + d[2] * (x_diff * y_diff)
			temp[j, i] = (blue, green, red)
 
	return temp
 
# Read image
image = cv2.imread('fruits.jpg')
bil = bilinear_interpolate(image, 2)
 
cv2.imwrite('original.jpg', image)
cv2.imwrite('bilinear.jpg', bil)
 


image-scaling-methods - GitHub



Original image

Free image provided by pexel.com



Bilinear / Bilinéaires

Free image provided by pexel.com




Image Interpolation











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