Пример #1
0
def LeeFilter(image, kernelLength, show=False):
    import numpy as np
    import cv2
    from medImUtils import imUtils

    image = imUtils.im2double(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:
        imUtils.showImageStyle(1, 2, {
            'I1': imUtils.im2uint8(image),
            'Imean': imUtils.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 medImUtils import imUtils
    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': imUtils.im2uint8(H)}
        imUtils.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 medImUtils import imUtils
    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': imUtils.im2uint8(H)}
        imUtils.showImageStyle(1, 1, images, ['Butter Mask'])
    return H
Пример #4
0
def imnoise(image, variance, show=False):
    """
    Adicionar ruído gaussiano na imagem
    
    INPUTS:
        image: imagem na qual será adicionado o ruído
        
        variance: variancia da distribuição gaussiana
        
        show: parâmetro para a imagem com ruído ser exibida (padrão = False)
        
    OUTPUTS:
        Inoisy: imagem com ruído
    """
    import numpy as np
    from medImUtils import imUtils
    noisy = 1 * np.random.normal(0, variance**0.5, image.shape)
    Inoisy = imUtils.im2uint8(np.clip((image + noisy), 0, 1))
    if show:
        imUtils.showImageStyle(1, 1, {'Inoisy': Inoisy},
                               ['Gaussian Noise Var: ' + str(variance)])
    return Inoisy
Пример #5
0
def imageIFFT(imageFFT, show=False):
    """
    IFFT de imagem
    
    INPUTS:
        imageFFT: imagem no domínio da frequencia
        
        show: parametro para exibição da imagem no domínio espacial (padrão: False)
    OUTPUTS:
        Ifft: imagem no dominio espacial
        
        IfftABS: valor absoluto da imagem no dominio espacial
    """
    import numpy as np
    from medImUtils import imUtils
    Ifft = np.fft.ifftshift(imageFFT)
    Ifft = np.fft.ifft2(Ifft)
    IfftABS = imUtils.im2uint8(np.abs(Ifft))
    if show:
        images = {'IfftABS': IfftABS}
        imUtils.showImageStyle(1, 1, images, ['IfftABS'])
    return Ifft, IfftABS, IfftABS
Пример #6
0
def imageFFT(image, show=False):
    """
    FFT de imagem
    
    INPUTS:
        image: imagem original para ser passada para o dominio da frequencia
        
        show: parametro para exibição da imagem no domínio da frequencia (padrão: False)
    OUTPUTS:
        fftimage: imagem no dominio da frequencia
        
        fftshift: shift do dominio da frequencia
    """
    import numpy as np
    from medImUtils import imUtils
    fftimage = np.fft.fft2(image)
    fftshift = np.fft.fftshift(fftimage)
    absfft = imUtils.im2uint8(np.abs(fftshift))
    if show:
        images = {'absfft': absfft}
        imUtils.showImageStyle(1, 1, images, ['absfft'])
    return fftimage, fftshift, absfft
Пример #7
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)

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

#%% DESAFIO
sobel_Image = imUtils.im2double(imUtils.sobel(I1))
meanI1 = imUtils.im2double(imUtils.meanFilterFast(I1, 11))
challenge = meanI1 + sobel_Image * (I1 - meanI1)
imUtils.showImageStyle(1, 2, {
    'I1': imUtils.im2uint8(I1),
    'challenge': imUtils.im2uint8(challenge)
}, ['Original', 'Challenge'])
Пример #8
0
H_ideal = imUtils.idealfilter(m, n, fc)
mamoFFT_filtered = mamoFFTshift * H_ideal
mamoIFFT, mamoIFFTshift, mamoIdeal = imUtils.imageIFFT(mamoFFT_filtered)

H_gaussian = imUtils.gaussianFilter2D(m, n, fc)
mamoFFT_filtered = mamoFFTshift * H_gaussian
mamoIFFT, mamoIFFTshift, mamoGaussian = imUtils.imageIFFT(mamoFFT_filtered)

H_butter = imUtils.butterFilter2D(m, n, fc, 2)
mamoFFT_filtered = mamoFFTshift * H_butter
mamoIFFT, mamoIFFTshift, mamoButter = imUtils.imageIFFT(mamoFFT_filtered)

imUtils.showImageStyle(
    1, 4, {
        'mamo': mamo,
        'mamoIdeal': mamoIdeal,
        'mamoGaussian': mamoGaussian,
        'mamoButter': mamoButter
    }, ['mamo', 'Ideal', 'Gaussian', 'Butter'])

#%% 2 - Filtragens Passa alta
mamo = imageio.imread(mamo_path)

mamoFFT, mamoFFTshift, mamoFFTabs = imUtils.imageFFT(mamo)
m, n, fc = mamoFFTshift.shape[0], mamoFFTshift.shape[1], 0.2

H_ideal = imUtils.idealfilter(m, n, fc, filterType='HP')
mamoFFT_filtered = mamoFFTshift * H_ideal
mamoIFFT, mamoIFFTshift, mamoIdeal = imUtils.imageIFFT(mamoFFT_filtered)

H_gaussian = imUtils.gaussianFilter2D(m, n, fc, filterType='HP')
Пример #9
0
plt.axis('off')
plt.show()

#%% 5a - Filtragem de mamo e stent
mamo_path = os.path.join(cf, 'ImagensAulas',
                         'Mamography.pgm')  # endereco da imagem
mamo = imageio.imread(mamo_path)

stent_path = os.path.join(cf, 'ImagensAulas',
                          'Stent.pgm')  # endereco da imagem
stent = imageio.imread(stent_path)

filtroMamo = imUtils.ffilter(mamo, 0.1)
filtroStent = imUtils.ffilter(stent, 0.1)

mamoFFT, mamoFFTshift, absMamo = imUtils.imageFFT(mamo, False)
mamoIFFT, mamoIFFTshift, mamoIFFTabs = imUtils.imageIFFT(
    mamoFFTshift * filtroMamo, False)
imUtils.showImageStyle(1, 2, {
    'mamo': mamo,
    'mamoIFFTshift': mamoIFFTshift
}, ['mamo', 'mamo filtered'])

stentFFT, stentFFTshift, absStent = imUtils.imageFFT(stent, False)
stentIFFT, stentIFFTshift, stentIFFTabs = imUtils.imageIFFT(
    stentFFTshift * filtroStent, False)
imUtils.showImageStyle(1, 2, {
    'stent': stent,
    'stentIFFTshift': stentIFFTshift
}, ['stent', 'stent filtered'])  # para plotar do meu jeito