Python - 双线性图像插值 [英] Python - Bilinear image interpolation

查看:189
本文介绍了Python - 双线性图像插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Python函数,它将图像作为输入并执行双线性图像插值来调整图像大小。我已经取得了相当的成功,因为图像确实调整了大小,但是这个过程在输出中引入了黑洞,我似乎无法弄清楚它们是如何或为什么存在的。

I'm trying to write a Python function that takes an image as input and performs bilinear image interpolation to resize an image. I've had reasonable success, since the image does get resized, but the process introduces black holes in the output which I can't seem to figure out how or why they're there.

我见过的问题对我没有多大帮助(在numpy和python中简单,高效的双线性插值图像

The questions I've seen haven't helped me much (Simple, efficient bilinear interpolation of images in numpy and python)

代码:

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

图像结果:
https://www.dropbox.com/s/ji0frbzcuyxd11u/results.png?m=

可能有更好的方法来做到这一点,但我真的很感激,如果有人可以看看,告诉我我做错了什么或我还需要做什么。谢谢!

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

推荐答案

我可以推荐 scipy.ndimage.interpolation.zoom ,< a href =http://docs.scipy.org/doc/scipy/reference/generated/scipy.misc.imresize.html#scipy.misc.imresize =nofollow> scipy.misc.imresize 或 mahotas.zoom ?您可以选择插值顺序,其中1是线性的。

Rather than reinventing the wheel, may I recommend scipy.ndimage.interpolation.zoom, scipy.misc.imresize or mahotas.zoom? You get a choice of interpolation orders, with 1 being linear.

至于为什么它不起作用,如果你的x_coord或y_coord碰巧是整数,那么权重将为零。

As to why it isn't working, if your x_coord or y_coord happen to be integer, then the weights will be zero.

这篇关于Python - 双线性图像插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆