Python:非最大抑制

Python:非最大抑制,python,numpy,scipy,itk,Python,Numpy,Scipy,Itk,我目前正在寻找一个3D非最大抑制滤波器。有这样的吗 sitk中有一个cannyEdgeDetection过滤器,它应该包括非最大抑制,但我需要它作为独立的 编辑: 我发现了非最大化抑制的2D实现,但我不知道如何在3D中实现: def gradient(im): # Sobel operator op1 = np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]]) op2 = np.array([[-1, -2, -1],[ 0, 0,

我目前正在寻找一个3D非最大抑制滤波器。有这样的吗

sitk中有一个cannyEdgeDetection过滤器,它应该包括非最大抑制,但我需要它作为独立的

编辑:

我发现了非最大化抑制的2D实现,但我不知道如何在3D中实现:

def gradient(im):
    # Sobel operator
    op1 = np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]])
    op2 = np.array([[-1, -2, -1],[ 0,  0,  0],[ 1,  2,  1]])
    kernel1 = np.zeros(im.shape)
    kernel1[:op1.shape[0], :op1.shape[1]] = op1
    kernel1 = np.fft.fft2(kernel1)

    kernel2 = np.zeros(im.shape)
    kernel2[:op2.shape[0], :op2.shape[1]] = op2
    kernel2 = np.fft.fft2(kernel2)

    fim = np.fft.fft2(im)
    Gx = np.real(np.fft.ifft2(kernel1 * fim)).astype(float)
    Gy = np.real(np.fft.ifft2(kernel2 * fim)).astype(float)

    G = np.sqrt(Gx**2 + Gy**2)
    Theta = np.arctan(Gy, Gx) * 180 / np.pi
    return G, Theta    

def nonMaximumSuppression(image):
    det, phase = self.gradient(image)
    gmax = np.zeros(det.shape)
    for i in range(gmax.shape[0]):
        for j in range(gmax.shape[1]):
            if phase[i][j] < 0:
                phase[i][j] += 360

            if ((j+1) < gmax.shape[1]) and ((j-1) >= 0) and ((i+1) < gmax.shape[0]) and ((i-1) >= 0):
            # 0 degrees
                if (phase[i][j] >= 337.5 or phase[i][j] < 22.5) or (phase[i][j] >= 157.5 and phase[i][j] < 202.5):
                    if det[i][j] >= det[i][j + 1] and det[i][j] >= det[i][j - 1]:
                        gmax[i][j] = det[i][j]
            # 45 degrees
                if (phase[i][j] >= 22.5 and phase[i][j] < 67.5) or (phase[i][j] >= 202.5 and phase[i][j] < 247.5):
                    if det[i][j] >= det[i - 1][j + 1] and det[i][j] >= det[i + 1][j - 1]:
                        gmax[i][j] = det[i][j]
            # 90 degrees
                if (phase[i][j] >= 67.5 and phase[i][j] < 112.5) or (phase[i][j] >= 247.5 and phase[i][j] < 292.5):
                    if det[i][j] >= det[i - 1][j] and det[i][j] >= det[i + 1][j]:
                        gmax[i][j] = det[i][j]
            # 135 degrees
                if (phase[i][j] >= 112.5 and phase[i][j] < 157.5) or (phase[i][j] >= 292.5 and phase[i][j] < 337.5):
                    if det[i][j] >= det[i - 1][j - 1] and det[i][j] >= det[i + 1][j + 1]:
                        gmax[i][j] = det[i][j]
    return gmax
def梯度(im):
#索贝尔算子
op1=np.数组([[-1,0,1],-2,0,2],-1,0,1]]
op2=np.数组([[-1,-2,-1],[0,0,0],[1,2,1]]
内核1=np.零(im.形状)
内核1[:op1.shape[0],:op1.shape[1]]=op1
kernel1=np.fft.fft2(kernel1)
内核2=np.零(im.形状)
内核2[:op2.shape[0],:op2.shape[1]]=op2
kernel2=np.fft.fft2(kernel2)
fim=np.fft.fft2(im)
Gx=np.real(np.fft.ifft2(kernel1*fim)).astype(float)
Gy=np.real(np.fft.ifft2(kernel2*fim)).astype(float)
G=np.sqrt(Gx**2+Gy**2)
θ=np.arctan(Gy,Gx)*180/np.pi
返回G,θ
def非最大化抑制(图像):
det,相位=自梯度(图像)
gmax=np.零(det.shape)
对于范围内的i(gmax.shape[0]):
对于范围内的j(gmax.shape[1]):
如果阶段[i][j]<0:
相位[i][j]+=360
如果((j+1)=0)和((i+1)=0:
#零度
如果(阶段[i][j]>=337.5或阶段[i][j]<22.5)或(阶段[i][j]>=157.5和阶段[i][j]<202.5):
如果det[i][j]>=det[i][j+1]和det[i][j]>=det[i][j-1]:
gmax[i][j]=det[i][j]
#45度
如果(阶段[i][j]>=22.5和阶段[i][j]<67.5)或(阶段[i][j]>=202.5和阶段[i][j]<247.5):
如果det[i][j]>=det[i-1][j+1]和det[i][j]>=det[i+1][j-1]:
gmax[i][j]=det[i][j]
#90度
如果(阶段[i][j]>=67.5和阶段[i][j]<112.5)或(阶段[i][j]>=247.5和阶段[i][j]<292.5):
如果det[i][j]>=det[i-1][j]和det[i][j]>=det[i+1][j]:
gmax[i][j]=det[i][j]
#135度
如果(阶段[i][j]>=112.5和阶段[i][j]<157.5)或(阶段[i][j]>=292.5和阶段[i][j]<337.5):
如果det[i][j]>=det[i-1][j-1]和det[i][j]>=det[i+1][j+1]:
gmax[i][j]=det[i][j]
返回gmax