예제 #1
0
def LeeFilter(image, kernelLength, show=False):
    import numpy as np
    import cv2
    from ImageMedicalLib import changeFormat, info

    if image.dtype == 'uint8':
        image = changeFormat.uint2double(image)
    rect = cv2.selectROI(image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    c1, c2 = rect[0], rect[0] + rect[2]
    l1, l2 = rect[1], rect[0] + rect[3]
    stdRect = np.std(image[l1:l2, c1:c2])
    newImage = np.zeros(image.shape)
    w = np.zeros((kernelLength, kernelLength), dtype=int)
    w_center = int((w.shape[0]) / 2)
    for indexrow, frow in enumerate(image[:-(w_center * 2)]):
        for indexcolumn, fcolumn in enumerate(frow[:-(w_center * 2)]):
            maskLocal = image[indexrow:1 + indexrow + w_center * 2,
                              indexcolumn:1 + indexcolumn + w_center * 2]
            meanMask = np.mean(maskLocal + w)
            k = np.clip(1 - (stdRect / (0.001 + maskLocal.std())), 0, 1)
            newImage[indexrow + w_center,
                     indexcolumn + w_center] = meanMask + k * (
                         image[indexrow + w_center, indexcolumn + w_center] -
                         meanMask)
    if show:
        info.showImageStyle(
            1, 2, {
                'I1': changeFormat.im2uint8(image),
                'Imean': changeFormat.im2uint8(newImage)
            }, ['Original', 'Lee Filter'])
    return newImage
예제 #2
0
def idealfilter(m, n, fc, filterType='LP', show=False):
    """
    Filtro ideal para dominio da frequencia
    
    INPUTS:
        m: número de linhas
        
        n: número de colunas
        
        fc: frequencia de corte (0 a 1)
                
        filterType: Passa baixa ou passa alta (padrão = LP passa baixa)
        
        show: parametro para exibir filtro criado
    OUTPUTS:
        H: filtro ideal 2D
    """
    import numpy as np
    from ImageMedicalLib import changeFormat, info
    H = np.zeros((m, n), dtype=int)
    centerX, centerY = int(m / 2), int(n / 2)
    Do = fc * (0.5 * (centerX * 0.5 + centerY * 0.5))
    for i in range(H.shape[0]):
        for j in range(H.shape[1]):
            D_uv = ((centerX - i)**2 + (centerY - j)**2)**0.5
            if D_uv <= Do and filterType == 'LP':
                H[i, j] = 1
            elif D_uv >= Do and filterType != 'LP':
                H[i, j] = 1
    if show:
        images = {'H': changeFormat.im2uint8(H)}
        info.showImageStyle(1, 1, images, ['Butter Mask'])
    return H
예제 #3
0
def butterFilter2D(m, n, fc, nPoles, filterType='LP', show=False):
    """
    Filtro butterworth para dominio da frequencia
    
    INPUTS:
        m: número de linhas
        
        n: número de colunas
        
        fc: frequencia de corte (0 a 1)
        
        nPoles: número de polos para filtragem
        
        filterType: Passa baixa ou passa alta (padrão = LP passa baixa)
        
        show: parametro para exibir filtro criado
    OUTPUTS:
        H: filtro butterworth 2D
    """
    import numpy as np
    from ImageMedicalLib import changeFormat, info
    if filterType == 'LP':
        H = np.zeros((m, n))
    else:
        H = np.full((m, n), -1).astype(float)
    centerX, centerY = int(m / 2), int(n / 2)
    Do = fc * (0.5 * (centerX * 0.5 + centerY * 0.5))
    for i in range(m):
        for j in range(n):
            D_uv = ((centerX - i)**2 + (centerY - j)**2)**0.5
            H[i, j] = np.abs(H[i, j] + (1 / (1 + (D_uv / Do)**(2 * nPoles))))
    if show:
        images = {'H': changeFormat.im2uint8(H)}
        info.showImageStyle(1, 1, images, ['Butter Mask'])
    return H
예제 #4
0
#%% 2 - Pulso quadrado
squarePulse = imageio.imread(r'ImagensAulas\PulsoQuadrado1.pgm')
plt.figure()
plt.get_current_fig_manager().window.showMaximized()
fig = plt.gcf()
fig.canvas.set_window_title('Square Pulse')
plt.imshow(squarePulse,cmap='gray',vmin = 0, vmax=255)
plt.axis('off')
plt.show()

#%% 3 - FFT2 Pulso quadrado
fftsquarePulse,fftshiftsquarePulse,absfftsquarePulse = filters.imageFFT(squarePulse)
logFFT = np.log(1+fftshiftsquarePulse)
logFFTABS = np.abs(logFFT)
logFFTABS = changeFormat.im2uint8(changeFormat.imNormalize(logFFTABS))
info.showImageStyle(1,3,{'fftsquarePulse': changeFormat.im2uint8(changeFormat.imNormalize(np.abs(fftsquarePulse))),
                         'absfftsquarePulse':changeFormat.im2uint8(absfftsquarePulse),
                         'logFFTABS':logFFTABS},
                    ['fftsquarePulse','absfftsquarePulse','LogfftsquarePulse'])

#%% 4 - Filtro passa baixas de 10%
H = np.zeros(squarePulse.shape, dtype = int)
centerX, centerY = int((squarePulse.shape[0])/2),int((squarePulse.shape[1])/2)
filter_length = centerX*0.5
for i in range (H.shape[0]):
    for j in range (H.shape[1]):
        if ((centerX - i)**2 + (centerY - j)**2)**0.5 <= filter_length:
            H[i,j] = 1
 
Ffiltrado = changeFormat.im2uint8(absfftsquarePulse)*H
예제 #5
0
kernelLength = 7
newImage = np.zeros(image.shape)
w = np.zeros((kernelLength, kernelLength), dtype=int)
w_center = int((w.shape[0]) / 2)
for indexrow, frow in enumerate(image[:-(w_center * 2)]):
    for indexcolumn, fcolumn in enumerate(frow[:-(w_center * 2)]):
        maskLocal = image[indexrow:1 + indexrow + w_center * 2,
                          indexcolumn:1 + indexcolumn + w_center * 2]
        meanMask = np.mean(maskLocal + w)
        k = np.clip(1 - (stdRect / (0.001 + maskLocal.std())), 0,
                    1)  # minimo é zero e maximo é 1
        newImage[
            indexrow + w_center, indexcolumn + w_center] = meanMask + k * (
                image[indexrow + w_center, indexcolumn + w_center] - meanMask)

info.showImageStyle(1, 2, {
    'I1': changeFormat.im2uint8(I1),
    'newImage': changeFormat.im2uint8(newImage)
}, ['Original', 'Image Filtered'])
#%% Função zero bala Filtro de Lee (esse é mais rapido)
newImage = filters.LeeFilter(I1, 9, True)

#%% DESAFIO
sobel_Image = changeFormat.uint2double(filters.sobel(I1))
meanI1 = changeFormat.uint2double(filters.meanFilterFast(I1, 8))
challenge = meanI1 + sobel_Image * (I1 - meanI1)
info.showImageStyle(1, 2, {
    'I1': changeFormat.im2uint8(I1),
    'challenge': changeFormat.im2uint8(challenge)
}, ['Original', 'Challenge'])
예제 #6
0
plt.subplot(122)
plt.imshow(w_Gauss2D1)
plt.title('Gaussian Filter')
plt.show()

#%% 3 - Convolução mamo e kernel
mamo = imageio.imread(r'ImagensAulas\Mamography.pgm')
w_Gauss2Dnorm = w_Gauss2D / w_Gauss2D.sum()
mamoConv = ndimage.convolve(mamo, w_Gauss2Dnorm)
info.showImageStyle(1,
                    1, {'mamoConv': mamoConv}, ['Mamo Gaussian Filter'],
                    title='Mamo Gaussian Filter')

#%% Função mascara gaussiana
w_Gauss2D = filters.kernelGauss(10, 5, A=1)
info.showImageStyle(1, 1, {'w_Gauss2D': changeFormat.im2uint8(w_Gauss2D)},
                    ['w_Gauss2D'])

#%% Convolucao com mascara gaussiana gerada no item acima
mamoConv = ndimage.convolve(mamo, w_Gauss2D)
info.showImageStyle(1, 1, {'mamoConv': mamoConv}, ['mamoConv'])

#%% 4 -Afiamento de bordas
imri = imageio.imread(r'ImagensAulas\TransversalMRI2.pgm')
imri = changeFormat.uint2double(
    imri)  # Conversão para double para não dar ruim
mask = filters.kernelGauss(8, 3, A=1)
f_blur = ndimage.convolve(imri, mask)
g = imri - f_blur
f_sharpened = (imri + g).clip(0, 1)
images = {