在3D numpy阵列中查找局部极值

时间:2019-03-01 12:40:13

标签: python numpy matrix scipy max

我正在尝试检测图像比例空间中的局部最小值和最大值。 Scale Space只是一个3D numpy数组,其中前2个维对应于图像像素值,第3个维对应于其比例尺(在每个比例中,我都使用高斯滤波器对图像进行滤波,使其更加模糊)。

这是我计算比例空间的方式:

def scalespace (image, size = 5, laplacian = True, fact = np.sqrt(2)):

    """Returns 3d numpy array containing scale space of image
    Each step corresponds to the gaussian filter applied with a step = sqrt(2)      
    The first scale corresponds to the original image.
    """

    scale_space = np.zeros((image.shape[0],image.shape[1],size))
    image_copy = image.copy()
    scale_space[:,:,0] = image_copy

    for s in range(1,size):

        #convolve on previous scale
        filtered_image = ndi.filters.gaussian_filter(scale_space[:,:,s-1], sigma = fact*np.sqrt(s))
        if laplacian:
            filtered_image = ndi.filters.laplace(filtered_image)

        scale_space[:,:,s] = filtered_image

    return scale_space

现在,我必须找到局部最小值和最大值(我正在进行斑点检测)。对于单个图像(2D情况),我发现它非常简单,但是现在,我还需要考虑像素值在所有比例上是否为最大值/最小值,我发现这很困难。在2d情况下,我是这样的:

if len(image.shape) == 2:

    min_x,min_y = scipy.signal.argrelextrema(image, np.less, order = n_order, axis = 0) 
    min_2d = [(j,i) for i,j in zip(min_x,min_y)]
    max_x,max_y = scipy.signal.argrelextrema(image, np.greater, order = n_order, axis = 0) 
    max_2d = [(j,i) for i,j in zip(max_x,max_y)]

    #check on the other direction
    min_x,min_y = scipy.signal.argrelextrema(image, np.less, order = n_order, axis = 1) 
    new_min_2d = [(j,i) for i,j in zip(min_x,min_y) if (j,i) in min_2d]
    max_x,max_y = scipy.signal.argrelextrema(image, np.greater, order = n_order, axis = 1) 
    new_max_2d = [(j,i) for i,j in zip(max_x,max_y) if (j,i) in max_2d]

我一直在尝试使其适应3D空间,但这只是一团糟,我觉得必须有一种更简单的方法来做到这一点?有想法吗?除了您知道的此scipy功能之外,还有其他资源吗?

谢谢

0 个答案:

没有答案