Python - bilinear image interpolation

I am trying to write a Python function that takes an image as input and interpolates a bilinear image to resize the image. I had a reasonable success, as the image changed, but the process introduces black holes in the output, which I can not understand how and why they are there.

The questions I saw didn’t help me much ( Simple, efficient bilinear interpolation of images in numpy and python )

The code:

def img_interp(img, scale = 1.5): angle_rad = pi * angle_deg / 180.0; rows, cols, colours = img.shape n_rows = int(round(rows * scale, 0)) n_cols = int(round(cols * scale, 0)) enlarged_img = np.ones((n_rows, n_cols, colours)) for i in range(n_rows - 1): for j in range(n_cols - 1): x_coord = j / scale y_coord = i / scale xc = int(ceil(x_coord)) xf = int(floor(x_coord)) yc = int(ceil(y_coord)) yf = int(floor(y_coord)) W_xc = xc - x_coord W_xf = x_coord - xf W_yc = yc - y_coord W_yf = y_coord - yf enlarged_img[i, j, :] = 255 - np.around(W_xc * (W_yc * img[yf, xf, :] + W_yf * img[yc, xf, :]) + W_xf * (W_yc * img[yf, xc, :] + W_yf * img[yc, xc, :]), 0) return enlarged_img 

Image results: https://www.dropbox.com/s/ji0frbzcuyxd11u/results.png?m=

There are probably better ways to do this than what I did, but I would really appreciate it if someone could take a look and tell me what I did wrong or what else I need to do. Thanks!

0
python numpy image-processing
source share
2 answers

Instead of reinventing the wheel, can I recommend scipy.ndimage.interpolation.zoom , scipy.misc.imresize or mahotas.zoom ? You get a selection of interpolation orders, with 1 being linear.

As for why it doesn't work, if your x_coord or y_coord turned out to be integers, then the weights will be zero.

+3
source share

Any reason not to use any of the excellent image processing libraries available for Python? For example, OpenCV has cv2.resize , which performs bilinear interpolation by default. Scipy has scipy.misc.imresize .

+1
source share

All Articles