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 sobel(image): """ Filtro Sobel INPUTS: image: imagem a ser filtrada OUTPUTS: gradient_sobel: gradiente de Sobel resultante das derivadas x e y """ import numpy as np from medImUtils import imUtils from scipy import ndimage wx = np.asarray([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) wy = np.asarray([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) image_norm = imUtils.im2double(image) dx = ndimage.convolve(image_norm, wx) dy = ndimage.convolve(image_norm, wy) gradient_sobel = np.sqrt(dx**2 + dy**2) return gradient_sobel
def priwitt(image): """ Filtro Priwitt INPUTS: image: imagem a ser filtrada OUTPUTS: gradient_priwitt: gradiente de Priwitt resultante das derivadas x e y """ import numpy as np from medImUtils import imUtils from scipy import ndimage wx = np.asarray([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]) wy = np.asarray([[-1, -1, -1], [0, 0, 0], [1, 1, 1]]) image_norm = imUtils.im2double(image) dx = ndimage.convolve(image_norm, wx) dy = ndimage.convolve(image_norm, wy) gradient_priwitt = np.sqrt(dx**2 + dy**2) return gradient_priwitt
def laplace(image): """ Filtros Laplacianos INPUTS: image: imagem a ser filtrada OUTPUTS: dL1: Laplaciano resultante da máscara [[0,1,0],[1,-4,1],[0,1,0]] dL2: Laplaciano resultante da máscara [[1,1,1],[1,-8,1],[1,1,1]] """ import numpy as np from medImUtils import imUtils from scipy import ndimage L1 = np.asarray([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) L2 = np.asarray([[1, 1, 1], [1, -8, 1], [1, 1, 1]]) image_norm = imUtils.im2double(image) dL1 = ndimage.convolve(image_norm, L1) dL2 = ndimage.convolve(image_norm, L2) return dL1, dL2
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'])