def ref_region( img: np.ndarray, selem: Any = disk(5), sigma: int = 3, opening_se: np.ndarray = np.ones((10, 10)), closing_se: np.ndarray = np.ones((5, 5)), verbose: bool = False ): """ """ # Perform histogram equalisation : _img_eq = rank.equalize(img, selem=selem) # Perform edge detection : _edges = canny(_img_eq, sigma=3) _filled = ndi.binary_fill_holes(_edges) # Morphological processing : _eroded = utils.closing( utils.opening(np.float64(_filled), opening_se), closing_se ) if verbose: utils.side_by_side(img, _img_eq, title1="Original", title2="Histogram Equalised") #plt.title('Lol') utils.side_by_side(_img_eq, _filled, title1="Histogram Equalised", title2="Canny Edge Detection + Filled image") #plt.title('Lal') utils.side_by_side(_filled, _eroded, title1="Canny Edge Detection + Filled image", title2="Opening, closing") #plt.title('Lel') return _eroded
def opening_mask(m, img=None): # we give some margin marg = 50 aux = np.zeros((m.shape[0] + marg*2, m.shape[1] + marg*2)) aux[marg:marg+m.shape[0], marg:marg+m.shape[1]] = m # relative to the shape of the mask to compute opening val = min(m.shape) size = (min(int(0.1 * val), 100), min(int(0.1*val), 100)) aux = opening(aux, size=size) return aux[marg:marg+m.shape[0], marg:marg+m.shape[1]], None
def main(): img = cv2.imread('lena.bmp', 0) # img is now a 512 x 512 numpy.ndarray if not os.path.exists('Task-1'): os.makedirs('Task-1') if not os.path.exists('Task-2'): os.makedirs('Task-2') if not os.path.exists('Task-3'): os.makedirs('Task-3') if not os.path.exists('Task-4'): os.makedirs('Task-4') if not os.path.exists('Task-5'): os.makedirs('Task-5') # Generate and output an image with # additive white Gaussian noise # with amplitude = 10 img_gauss_10 = generate_Gaussian_noise(img, 0, 1, 10) cv2.imwrite('Task-1/lena.gaussian.10.bmp', img_gauss_10) # Generate and output an image with # additive white Gaussian noise # with amplitude = 30 img_gauss_30 = generate_Gaussian_noise(img, 0, 1, 30) cv2.imwrite('Task-1/lena.gaussian.30.bmp', img_gauss_30) # Generate and output an image with # salt-and-pepper noise with # threshold = 0.05 img_sp_05 = generate_salt_and_pepper_noise(img, 0, 1, 0.05) cv2.imwrite('Task-2/lena.sp.05.bmp', img_sp_05) # Generate and output an image with # salt-and-pepper noise with # threshold = 0.1 img_sp_10 = generate_salt_and_pepper_noise(img, 0, 1, 0.1) cv2.imwrite('Task-2/lena.sp.10.bmp', img_sp_10) # Run 3x3 box filter on the image # with white Gaussian noise with # amplitude = 10 img_gauss_10_box_3 = box_filter(img_gauss_10, 3) cv2.imwrite('Task-3/lena.gaussian.10.box.3x3.bmp', img_gauss_10_box_3) # Run 3x3 box filter on the image # with white Gaussian noise with # amplitude = 30 img_gauss_30_box_3 = box_filter(img_gauss_30, 3) cv2.imwrite('Task-3/lena.gaussian.30.box.3x3.bmp', img_gauss_30_box_3) # Run 3x3 box filter on the image # with salt-and-pepper noise with # threshold = 0.05 img_sp_05_box_3 = box_filter(img_sp_05, 3) cv2.imwrite('Task-3/lena.sp.05.box.3x3.bmp', img_sp_05_box_3) # Run 3x3 box filter on the image # with salt-and-pepper noise with # threshold = 0.1 img_sp_10_box_3 = box_filter(img_sp_10, 3) cv2.imwrite('Task-3/lena.sp.10.box.3x3.bmp', img_sp_10_box_3) # Run 5x5 box filter on the image # with white Gaussian noise with # amplitude = 10 img_gauss_10_box_5 = box_filter(img_gauss_10, 5) cv2.imwrite('Task-3/lena.gaussian.10.box.5x5.bmp', img_gauss_10_box_5) # Run 5x5 box filter on the image # with white Gaussian noise with # amplitude = 30 img_gauss_30_box_5 = box_filter(img_gauss_30, 5) cv2.imwrite('Task-3/lena.gaussian.30.box.5x5.bmp', img_gauss_30_box_5) # Run 5x5 box filter on the image # with salt-and-pepper noise with # threshold = 0.05 img_sp_05_box_5 = box_filter(img_sp_05, 5) cv2.imwrite('Task-3/lena.sp.05.box.5x5.bmp', img_sp_05_box_5) # Run 5x5 box filter on the image # with salt-and-pepper noise with # threshold = 0.1 img_sp_10_box_5 = box_filter(img_sp_10, 5) cv2.imwrite('Task-3/lena.sp.10.box.5x5.bmp', img_sp_10_box_5) # Run 3x3 median filter on the image # with white Gaussian noise with # amplitude = 10 img_gauss_10_med_3 = median_filter(img_gauss_10, 3) cv2.imwrite('Task-4/lena.gaussian.10.median.3x3.bmp', img_gauss_10_med_3) # Run 3x3 median filter on the image # with white Gaussian noise with # amplitude = 30 img_gauss_30_med_3 = median_filter(img_gauss_30, 3) cv2.imwrite('Task-4/lena.gaussian.30.median.3x3.bmp', img_gauss_30_med_3) # Run 3x3 median filter on the image # with salt-and-pepper noise with # threshold = 0.05 img_sp_05_med_3 = median_filter(img_sp_05, 3) cv2.imwrite('Task-4/lena.sp.05.median.3x3.bmp', img_sp_05_med_3) # Run 3x3 median filter on the image # with salt-and-pepper noise with # threshold = 0.1 img_sp_10_med_3 = median_filter(img_sp_10, 3) cv2.imwrite('Task-4/lena.sp.10.median.3x3.bmp', img_sp_10_med_3) # Run 5x5 median filter on the image # with white Gaussian noise with # amplitude = 10 img_gauss_10_med_5 = median_filter(img_gauss_10, 5) cv2.imwrite('Task-4/lena.gaussian.10.median.5x5.bmp', img_gauss_10_med_5) # Run 5x5 median filter on the image # with white Gaussian noise with # amplitude = 30 img_gauss_30_med_5 = median_filter(img_gauss_30, 5) cv2.imwrite('Task-4/lena.gaussian.30.median.5x5.bmp', img_gauss_30_med_5) # Run 5x5 median filter on the image # with salt-and-pepper noise with # threshold = 0.05 img_sp_05_med_5 = median_filter(img_sp_05, 5) cv2.imwrite('Task-4/lena.sp.05.median.5x5.bmp', img_sp_05_med_5) # Run 5x5 median filter on the image # with salt-and-pepper noise with # threshold = 0.1 img_sp_10_med_5 = median_filter(img_sp_10, 5) cv2.imwrite('Task-4/lena.sp.10.median.5x5.bmp', img_sp_10_med_5) # Use octagon as kernel and set the orgin is at the center kernel = [ [-2, -1], [-2, 0], [-2, 1], [-1, -2], [-1, -1], [-1, 0], [-1, 1], [-1, 2], [0, -2], [0, -1], [0, 0], [0, 1], [0, 2], [1, -2], [1, -1], [1, 0], [1, 1], [1, 2], [2, -1], [2, 0], [2, 1] ] # closing followed by opening on all noisy images img_gauss_10_close_open = opening(closing(img_gauss_10, kernel), kernel) img_gauss_30_close_open = opening(closing(img_gauss_30, kernel), kernel) img_sp_05_close_open = opening(closing(img_sp_05, kernel), kernel) img_sp_10_close_open = opening(closing(img_sp_10, kernel), kernel) cv2.imwrite('Task-5/lena.gaussian.10.close.open.bmp', img_gauss_10_close_open) cv2.imwrite('Task-5/lena.gaussian.30.close.open.bmp', img_gauss_30_close_open) cv2.imwrite('Task-5/lena.sp.05.close.open.bmp', img_sp_05_close_open) cv2.imwrite('Task-5/lena.sp.10.close.open.bmp', img_sp_10_close_open) # opening followed by closing on all noisy images img_gauss_10_open_close = closing(opening(img_gauss_10, kernel), kernel) img_gauss_30_open_close = closing(opening(img_gauss_30, kernel), kernel) img_sp_05_open_close = closing(opening(img_sp_05, kernel), kernel) img_sp_10_open_close = closing(opening(img_sp_10, kernel), kernel) cv2.imwrite('Task-5/lena.gaussian.10.open.close.bmp', img_gauss_10_open_close) cv2.imwrite('Task-5/lena.gaussian.30.open.close.bmp', img_gauss_30_open_close) cv2.imwrite('Task-5/lena.sp.05.open.close.bmp', img_sp_05_open_close) cv2.imwrite('Task-5/lena.sp.10.open.close.bmp', img_sp_10_open_close)
def opening_mask(m): # relative to the shape of the mask to compute opening val = min(m.shape) size = (min(int(0.1 * val), 100), min(int(0.1 * val), 100)) return opening(m, size=size)
# In[9]: binaria = binarise(x) plt.imshow(binaria, cmap='gray') # In[10]: help(opening) # # Opening # In[11]: kernel = np.ones((10, 10)) side_by_side(binaria, opening(binaria, kernel), title1='Original', title2=f'opening() with Kernel {kernel.shape}') # In[12]: kernel = np.ones((10, 10)) side_by_side(binaria, cv.morphologyEx(binaria, cv.MORPH_OPEN, kernel), title1='Original', title2=f'cv.MORPH_OPEN with Kernel {kernel.shape}') # In[13]: kernel = np.ones((2, 50)) side_by_side(binaria,