Bilateral filtering

import numpy as np
import cv2
import time

def my_padding(src, pad_shape, pad_type='zero'):
    (h, w) = src.shape
    (p_h, p_w) = pad_shape
    pad_img = np.zeros((h+2*p_h, w+2*p_w))
    pad_img[p_h:p_h+h, p_w:p_w+w] = src

    if pad_type == 'repetition':
        print('repetition padding')
        #up
        pad_img[:p_h, p_w:p_w+w] = src[0, :]
        #down
        pad_img[p_h+h:, p_w:p_w+w] = src[h-1, :]

        #left
        pad_img[:,:p_w] = pad_img[:, p_w:p_w + 1]
        #right
        pad_img[:, p_w+w:] = pad_img[:, p_w+w-1:p_w+w]

    return pad_img

def my_filtering(src, filter, pad_type='zero'):
    (h, w) = src.shape
    (f_h, f_w) = filter.shape
    src_pad = my_padding(src, (f_h//2, f_w//2), pad_type)
    dst = np.zeros((h, w))

    for row in range(h):
        for col in range(w):
            val = np.sum(src_pad[row:row+f_h, col:col+f_w] * filter)
            dst[row, col] = val

    return dst

def my_get_Gaussian2D_mask(msize, sigma=1):
    y, x = np.mgrid[-(msize // 2):(msize // 2) + 1, -(msize // 2):(msize // 2) + 1]

    
    gaus2D =   1 / (2 * np.pi * sigma**2) * np.exp(-(( x**2 + y**2 )/(2 * sigma**2)))
    
    gaus2D /= np.sum(gaus2D)

    return gaus2D

def my_normalize(src):
    dst = src.copy()
    dst *= 255
    dst = np.clip(dst, 0, 255)
    return dst.astype(np.uint8)

def add_gaus_noise(src, mean=0, sigma=0.1):
    #src : 0 ~ 255, dst : 0 ~ 1
    dst = src/255
    h, w = dst.shape
    noise = np.random.normal(mean, sigma, size=(h, w))
    dst += noise
    return my_normalize(dst)

def my_bilateral(src, msize, sigma, sigma_r, pos_x, pos_y, pad_type='zero'):
    
    (h, w) = src.shape
    m_s = (msize-1)//2
    img_pad = my_padding(src, (m_s//2, m_s//2), pad_type)
    dst = np.zeros((h,w))

    y, x = np.mgrid[-(msize // 2):(msize // 2) + 1, -(msize // 2):(msize // 2) + 1]

    for i in range(h):
        print('\r%d / %d ...' %(i,h), end="")
        for j in range(w):
            mask = np.exp(-((x-j)**2 + (y-i)**2)/(2*sigma**2))
            mask /= np.sum(mask)

            if i==pos_y and j == pos_x:
                print()
                print(mask.round(4))
                img = img_pad[i:i+5, j:j+5]
                img = cv2.resize(img, (200, 200), interpolation = cv2.INTER_NEAREST)
                img = my_normalize(img)

            dst[i, j] = np.sum(img_pad[i:i+msize, j:j+msize]*mask)

    return dst



if __name__ == '__main__':
    start = time.time()
    src = cv2.imread('Lenna.png', cv2.IMREAD_GRAYSCALE)
    np.random.seed(seed=100)

    pos_y = 0
    pos_x = 0
    src_noise = add_gaus_noise(src, mean=0, sigma=0.1)
    src_noise = src_noise/255

    dst = my_bilateral(src_noise, 5, 10, 0.1, pos_x, pos_y)
    dst = my_normalize(dst)

    gaus2D = my_get_Gaussian2D_mask(5 , sigma = 1)
    dst_gaus2D= my_filtering(src_noise, gaus2D)
    dst_gaus2D = my_normalize(dst_gaus2D)

    cv2.imshow('original', src)
    cv2.imshow('gaus noise', src_noise)
    cv2.imshow('my gaussian', dst_gaus2D)
    cv2.imshow('my bilateral', dst)
    tital_time = time.time() - start
    print('\ntime : ', tital_time)
    if tital_time > 25:
        print('please change code')
    cv2.waitKey()
    cv2.destroyAllWindows()

I would like to make a code that performs bilateral filtering.

The other functions are the completed part,
I’d like to make a code that performs bilateral filtering by modifying only the function my_bilatera and main.
but

line 101, in <module>
    src_noise = add_gaus_noise(src, mean=0, sigma=0.1)
line 56, in add_gaus_noise
    dst = src/255
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

There are errors.
I’m probably getting an error due to the size of the image during the mask filter process, how do I fix it?
The path where the image is stored was performed without any problems.
I would really appreciate it if you could tell me how to modify it.
I uploaded it because I couldn’t process it quickly due to lack of knowledge.
I will study more if you let me know.
thak you…

no, you’re getting an error because src is None. please use a debugger and figure out when that should be set correctly, but is set to None. look at imread(). learn about the “current working directory” and relative paths. run print(os.getcwd()) in your code and consider what it says.

also crosspost: