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
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
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
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
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
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
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'])
cf = os.getcwd() # current folder squarePulse_path = os.path.join(cf, 'ImagensAulas', 'PulsoQuadrado1.pgm') # endereco da imagem squarePulse = imageio.imread(squarePulse_path) 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 squarePulseFFT = np.fft.fft2(squarePulse) squarePulseABS = imUtils.im2uint8(np.abs(squarePulseFFT)) squarePulseFFTshift = np.fft.fftshift(squarePulseFFT) squarePulseABS2 = imUtils.im2uint8(np.abs(squarePulseFFTshift)) logFFT = np.log(1 + squarePulseFFTshift) logFFTABS = imUtils.im2uint8(np.abs(logFFT)) plt.figure() plt.get_current_fig_manager().window.showMaximized() fig = plt.gcf() fig.canvas.set_window_title('Square Pulse FFT') plt.subplot(131) plt.imshow(squarePulseABS) plt.set_cmap('gray') plt.axis('off') plt.subplot(132) plt.imshow(squarePulseABS2)