Python - 双线性图像插值

标签 python image-processing numpy

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

我看到的问题对我帮助不大 ( 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=

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

最佳答案

与其重新发明轮子,不如推荐scipy.ndimage.interpolation.zoom , scipy.misc.imresizemahotas.zoom ?您可以选择插值阶数,其中 1 是线性的。

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

关于Python - 双线性图像插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25060379/

相关文章:

python - Python 将 3 channel rgb 彩色图像更改为 1 channel 灰色的速度有多快?

python - 在 Python 中检测 OHLC 数据中的模式

Python:非常大的稀疏矩阵中的 lil_matrix 与 csr_matrix

matlab - 使用canny方法进行一定程度的边缘检测

python - 如何使用 python 的性别化包来处理大数据集?

android - BufferedImage 安卓端口

python - numpy 数组 reshape 添加维度

python - 提高性能 - 符号函数应用于 numpy 数组的每一行

python - Django 和 PostgreSQL "No Fixtures Found"

python - Pandas 在特定级别将行添加到多索引