首页 > 解决方案 > 如何在给定坐标参考的情况下插入未知像素?

问题描述

我只是将 Python 用于计算机视觉。我需要大量关于使用 Python 在图像中进行像素插值的参考资料。

我有一个如下所示的 RGB 图像。在那张照片中,我有一个白色的未知像素。(x0, y0)我想使用沿蓝线的颜色强度信息从该点开始插入未知像素。(x0, y0)有人可以提供代码示例以使用任何插值技术进行点插值吗?输入是坐标颜色参考x,y = np.where(np.all(img == [0,0,255],axis=2))和像素坐标的集合(x0, y0)。输出是(x0, y0)已插值的像素。

在此处输入图像描述

标签: pythonimage-processingcomputer-visioninterpolation

解决方案


我将使用一堆函数来解决这个问题。

首先对于给定的坐标(x_i, y_i),我在一个特定的大小窗口中获得它两侧的像素坐标列表 =window_size

IE

def derive_adjacent_coords(coord, window_size):
    # Coord is a tuple (row, column)

    assert (window_size % 2) != 0, "Window size must be odd"
    mid_idx = (window_size - 1) / 2)

    return [(r, coord[1]) for r in range(coord[0] - mid_idx, coord[0] + mid_idx + 1)]

然后我将使用以下函数提取与图像中这些位置相对应的值:

def derive_adjacent_pixels(list_of_coord_tuples, image):
    # Image is your image, list_of_coord_tuples is the output of the first function
    return [ image[*i] for i in list_of_coord_tuples]

上面的函数将返回一个列表。然后我将插入列表:

from scipy.interpolate import interp1d
import math

def interpol(list):
    # list is the output of the derive_adjacent_pixels(list_of_coord_tuples, image) function
    x = [i for i in range len(list)]
    interpolated_list = interp1d(x, list, kind='cubic')
    return interpolated_list[int(math.floor(len(interpolated_list)/2))]

你必须用我上面展示的这种方法对图像的每个波段进行不同的插值。

您必须在未知像素坐标列表(元组列表)上迭代这些函数,然后为图像中的每个波段单独迭代。


推荐阅读