<jupyter_start><jupyter_text>Resize images<jupyter_code>"""
pascale.jpg --- enhancement
stacey.jpg --- noise
jason.jpg --- noise
michael.jpg --- detail 3 iteration
ricardo.jpg --- detail 3 iteration
daniel.jpg --- enhancement
"""
imagepath = "daniel.jpg"
img = cv2.imread(imagepath)
#cv2.imshow('Image', img)
#cv2.waitKey()
print(img.shape)
res = cv2.resize(img,(0,0),fx=0.8,fy=0.8, interpolation = cv2.INTER_AREA)
print(res.shape)
cv2.imshow('Resized Image', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite("sample1.jpg",res)<jupyter_output><empty_output><jupyter_text>add noise<jupyter_code>def add_gaussian_noise(image, sigma):
    '''Add Gaussian noise to an image.
       image : gray level image, pixel intensity : 0 ~255
       simga : float, standard deviation, controls level of noise 
       mu(mean) = 0.0 , float
    '''
    mu = 0.0 
    # Draw random samples from a normal (Gaussian) distribution
    noise = np.random.normal(mu, sigma, (image.shape))
    # generate noisy image
    noise_image = image + noise
    # clip the values to the interval 0 ~ 255
    noise_image = np.clip( noise_image, 0, 255)
    # convert 'float' to 'uint8'
    noise_image = noise_image.astype('uint8')
    return noise_image
def add_salt_and_pepper(image, threshold):
    '''Add " Salt & Pepper " noise to an image.
       image : gray-level image, pixel intensity : 0 ~ 255
       threshold : probability that controls level of noise
    '''
    # np.random.rand : create random values in a given shape
    # random samples from uniform distribution over [0,1)
    # rnd : ndarray, random values
    rnd = np.random.rand(image.shape[0], image.shape[1], image.shape[2])
    noise_image = image.copy()
    noise_image[ rnd < threshold] = 0
    noise_image[ rnd > (1- threshold)] = 255
    return noise_image
# add gaussian noise to an image with simga(standard deviation) = 10
imagepath = "sample2.jpg"
img = cv2.imread(imagepath)
noise_img = add_gaussian_noise(img, 10)
noise_img2 = add_gaussian_noise(noise_img, 20)
cv2.imshow('Noise Image', noise_img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('smaple2_gauss10_20.jpg',noise_img2)
# add salt-and-pepper noise to an image with threshold = 0.01
imagepath = "sample3.jpg"
img = cv2.imread(imagepath)
noise_img = add_salt_and_pepper(img, 0.01)
noise_img2 = add_salt_and_pepper(noise_img , 0.03)
cv2.imshow('Noise Image', noise_img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('smaple3_sp01_03.jpg',noise_img2)<jupyter_output><empty_output><jupyter_text>image enhancement<jupyter_code>def modified_unshark_masking(img, smooth_img,c):
    ''' 
    img : gray level image
    c: constant, 3/5(0.6) ~ 5/6(0.833)
    img_avg : image after using 3x3 average filter 
    if ( c < 3/5 or c > 5/6 ):
        print('Use constant c between 3/5 and 5/6')
        return -1
        
    '''
    #output_image = (c/(2*c-1)) * img - ((1-c)/(2*c-1)) * smooth_img
    output_image = c* img - (1-c) * smooth_img
    print(output_image.dtype)
    output_image[ output_image < 0] = 0
    output_image[ output_image > 255 ] = 255
    return  output_image.astype(np.uint8)
"""
pascale.jpg --- enhancement
stacey.jpg --- noise
jason.jpg --- noise
michael.jpg --- detail 3 iteration
ricardo.jpg --- detail 3 iteration
daniel.jpg --- enhancement
"""

imagepath ="sample6.jpg"
kernelsize = 3
iteration =1

I = cv2.imread(imagepath)
M= I.copy()
for i in range(iteration):
    print('Iteration: ', str(i+1))
    M,localmax, localmin, maxenvelope, minenvelope = EdgePreservingSmooth(M,kernelsize)
    kernelsize += 4
    print('')

I_YUV = cv2.cvtColor(I,cv2.COLOR_BGR2YUV)
M_YUV = cv2.cvtColor(M,cv2.COLOR_BGR2YUV)
D = I_YUV[:,:,0]-M_YUV[:,:,0]

# Make the grey scale image have three channels
grey_3_channel = cv2.cvtColor(D, cv2.COLOR_GRAY2BGR)
numpy_horizontal = np.hstack(( I, M, grey_3_channel))
cv2.imshow('Edge-preserving Smooth Result', numpy_horizontal)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imwrite("sample6_smooth.jpg",numpy_horizontal)
enhance_img1 = modified_unshark_masking(I, M,0.9)
enhance_img2 = modified_unshark_masking(I, M,1.1)
numpy_horizontal = np.hstack(( I,M,enhance_img1, enhance_img2))
cv2.imshow('Enhancement', numpy_horizontal)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imwrite("sample6_enhancement09_11.jpg", numpy_horizontal)<jupyter_output><empty_output><jupyter_text>remove noise with 2 iterations<jupyter_code>"""
pascale.jpg --- enhancement
stacey.jpg --- noise
jason.jpg --- noise
michael.jpg --- detail 3 iteration
ricardo.jpg --- detail 3 iteration
daniel.jpg --- enhancement
"""
"""
smaple2_gauss10_20.jpg
smaple2_sp01_03.jpg
smaple3_gauss10_20.jpg
smaple3_sp01_03.jpg
"""

imagepath ="smaple3_sp01_03.jpg"
kernelsize = 3
iteration =2

Mlist = []

I = cv2.imread(imagepath)
M= I.copy()
for i in range(iteration):
    print('Iteration: ', str(i+1))
    M,localmax, localmin, maxenvelope, minenvelope = EdgePreservingSmooth(M,kernelsize)
    Mlist.append(M)
    kernelsize += 4
    print('')

I_YUV = cv2.cvtColor(I,cv2.COLOR_BGR2YUV)
M_YUV = cv2.cvtColor(M,cv2.COLOR_BGR2YUV)
D = I_YUV[:,:,0]-M_YUV[:,:,0]

# Make the grey scale image have three channels
grey_3_channel = cv2.cvtColor(D, cv2.COLOR_GRAY2BGR)
numpy_horizontal = np.hstack(( I, M, grey_3_channel))
cv2.imshow('Edge-preserving Smooth Result', numpy_horizontal)
cv2.waitKey()
cv2.destroyAllWindows()
# (M*255).astype(np.uint8)
# normalize(Mlist[0]) normalize(Mlist[1])
numpy_horizontal = np.hstack(( I,Mlist[0], Mlist[1]))
cv2.imshow('Noise Removal', numpy_horizontal)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imwrite("sample3_sp_remove.jpg", numpy_horizontal)<jupyter_output><empty_output><jupyter_text>3 iterations with details<jupyter_code>"""
pascale.jpg --- enhancement
stacey.jpg --- noise
jason.jpg --- noise
michael.jpg --- detail 3 iteration
ricardo.jpg --- detail 3 iteration
daniel.jpg --- enhancement
"""


imagepath ="sample5.jpg"
kernelsize = 3
iteration =3

Mlist = []
Dlist = []

I = cv2.imread(imagepath)
M= I.copy()
for i in range(iteration):
    print('Iteration: ', str(i+1))
    M,localmax, localmin, maxenvelope, minenvelope = EdgePreservingSmooth(M,kernelsize)
    Mlist.append(M)
    
    I_YUV = cv2.cvtColor(I,cv2.COLOR_BGR2YUV)
    M_YUV = cv2.cvtColor(M,cv2.COLOR_BGR2YUV)
    D = I_YUV[:,:,0]-M_YUV[:,:,0]
    # Make the grey scale image have three channels
    grey_3_channel = cv2.cvtColor(D, cv2.COLOR_GRAY2BGR)
    Dlist.append(grey_3_channel)
    
    kernelsize += 4
    print('')

#I_YUV = cv2.cvtColor(I,cv2.COLOR_BGR2YUV)
#M_YUV = cv2.cvtColor(normalize(M),cv2.COLOR_BGR2YUV)
D = I_YUV[:,:,0]-M_YUV[:,:,0]

# Make the grey scale image have three channels
#grey_3_channel = cv2.cvtColor(D, cv2.COLOR_GRAY2BGR)
numpy_horizontal = np.hstack(( I, M, Dlist[-1]))
cv2.imshow('Edge-preserving Smooth Result', numpy_horizontal)
cv2.waitKey()
cv2.destroyAllWindows()
# (M*255).astype(np.uint8)
#  normalize(Mlist[0]) normalize(Mlist[1]) normalize(Mlist[2])
Zero = np.zeros(I.shape, dtype=I.dtype)
numpy_horizontal1 = np.hstack(( I,Mlist[0], Mlist[1],Mlist[2]))
numpy_horizontal2 = np.hstack((Zero,Dlist[0],Dlist[1],Dlist[2]))
result = np.vstack((numpy_horizontal1, numpy_horizontal2))
cv2.imshow('3 Iterations', result)
cv2.waitKey()
cv2.destroyAllWindows()
cv2.imwrite("sample5_3iterations.jpg", result)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1421535093720071, 'fraction_numerical': 0.04090846901080859, 'mean_word_length': 4.662277304415182, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '100722', 'license': 'MIT', 'n_tokens_mistral': 2975, 'n_tokens_neox': 2823, 'n_words': 563, 'path': '0623 final submit.ipynb', 'repo_name': 'elaina03/Edge-preserving-Multiscale-Image-Decomposition-based-on-Local-Extrema', 'size': '7309'}