<jupyter_start><jupyter_text>Setup playground<jupyter_code>import numpy as np
import matplotlib.pyplot as plt

from scipy import ndimage

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Playing with noise generators<jupyter_code>nimg = np.random.normal(0,1,size=[100,100])
uimg = np.random.uniform(-1,1,size=[100,100])
pimg = np.random.poisson(5,size=[100,100])

plt.figure(figsize=[15,8])

plt.subplot(1,3,1)
plt.imshow(nimg)

plt.subplot(1,3,2)
plt.imshow(uimg)

plt.subplot(1,3,3)
plt.imshow(pimg)<jupyter_output><empty_output><jupyter_text>Playing with filters<jupyter_code>gimg=ndimage.filters.gaussian_filter(nimg,1.0)
fimg=ndimage.filters.uniform_filter(nimg,3)
mimg=ndimage.filters.median_filter(nimg,3)
aimg=ndimage.filters.convolve(nimg,[[1,2,1],[2,4,2],[1,2,1]])

plt.figure(figsize=[15,13])
plt.subplot(2,2,1)
plt.imshow(gimg)
plt.title('Gaussian')

plt.subplot(2,2,2)
plt.imshow(fimg)
plt.title('Uniform')

plt.subplot(2,2,3)
plt.imshow(fimg)
plt.title('Median')

plt.subplot(2,2,4)
plt.imshow(fimg)
plt.title('Convolve (binomial)')<jupyter_output><empty_output><jupyter_text>Demonstrating edge blurringWe want to observe what happens with edges when filtering  Make test dataThe test data is a wedge structure with increasing gap.<jupyter_code>img=np.zeros([100,100])
img[0:50,:]=1
for i in range(img.shape[0]) :
    img[(51+int(i/10)):img.shape[0],i]=1
    
plt.imshow(img,cmap='gray')<jupyter_output><empty_output><jupyter_text>Evaluate under different conditions - Add noise of different strength - Apply different uniform filter kernels - Observe what happens on thresholded data  Make noisy image<jupyter_code>SNR=4
sigma=1/SNR 

noise=np.random.normal(0,sigma,img.shape)
nimg=img+noise
plt.imshow(nimg)
threshold = 0.5 # Threshold for segmentation

fig=plt.figure(figsize=[15,13])
for i in range(3) :
    if (i==0) :
        plt.subplot(3,3,i+1)
        plt.imshow(0.5<nimg)
        plt.title('Thresholded noisy image')
    
    plt.subplot(3,3,3+i+1)
    N=3+i*2
    fimg=ndimage.filters.uniform_filter(nimg,N)
    plt.imshow(fimg)

    plt.subplot(3,3,6+i+1)
    plt.imshow(threshold<fimg)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1554524361948956, 'fraction_numerical': 0.049651972157772624, 'mean_word_length': 6.591549295774648, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '207628', 'license': 'Apache-2.0', 'n_tokens_mistral': 960, 'n_tokens_neox': 887, 'n_words': 128, 'path': 'Exercises/03-ImageEnhancement/02-Demonstration.ipynb', 'repo_name': 'kimdon-ethz/Quantitative-Big-Imaging-2022', 'size': '2155'}