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
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 ImageMedicalLib import changeFormat 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]]) if image.dtype == 'uint8': image = changeFormat.uint2double(image) image_norm = 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 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 ImageMedicalLib import changeFormat 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]]) if image.dtype == 'uint8': image = changeFormat.uint2double(image) image_norm = image dL1 = ndimage.convolve(image_norm, L1) dL2 = ndimage.convolve(image_norm, L2) return dL1, dL2
@author: Andre """ #%% Importando módulos import numpy as np import imageio from ImageMedicalLib import changeFormat, info, filters import cv2 import os #%% 1 - Ultrassom bebe cf = os.getcwd() # current folder I1_path = os.path.join(cf, 'ImagensAulas', 'Ultrassom.pgm') # endereco da imagem I1 = changeFormat.uint2double(imageio.imread(I1_path)) rect = cv2.selectROI(I1) # selecionando região de interesse com openCV # Selecionar a região e dar enter 2x cv2.waitKey(0) cv2.destroyAllWindows() c1, c2 = rect[0], rect[0] + rect[2] # coluna minima e coluna maxima l1, l2 = rect[1], rect[0] + rect[3] # linha minima e linha maxima meanRect = np.mean(I1[l1:l2, c1:c2]) stdRect = np.std(I1[l1:l2, c1:c2]) #%% Filtro de Lee (essa demora) image = I1 kernelLength = 7 newImage = np.zeros(image.shape) w = np.zeros((kernelLength, kernelLength), dtype=int)
#%% 8 - Maximo, minimo e media da imagem maximum = I1.max() minimum = I1.min() meanI1 = int(I1.mean()) #%% 9 - Exibindo imagem plt.figure() plt.imshow(I1) plt.set_cmap('gray') plt.axis('off') plt.title('Raio X Torax') plt.colorbar() plt.show() #%% 10 - Convertendo para double I1double = changeFormat.uint2double(I1) newMax = I1double.max() newMin = I1double.min() newMean = I1double.mean() newStd = I1double.std() newMedian = np.median(I1double) plt.figure() plt.imshow(I1double) plt.set_cmap('gray') plt.axis('off') plt.title('Raio X Torax double') plt.colorbar() plt.show() Idouble50x50 = I1double[50, 50]
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 = { 'imri': changeFormat.im2uint8(imri), 'f_blur': changeFormat.im2uint8(f_blur), 'f_sharpened': changeFormat.im2uint8(f_sharpened) } info.showImageStyle(1, 3, images, ['imri', 'f_blur', 'f_sharpened']) #%% 5 - Gradientes x e y dx = np.asarray([[1, -1]]) dy = np.asarray([[1], [-1]]) stent = imageio.imread(r'ImagensAulas\Stent.pgm')