Hướng dẫn gaussian 1d filter python - Gaussian 1d filter python

Bạn đã phạm một lỗi trong mã của mình:

Trước khi nhân g với y_sel, y_sel không tập trung.

Lý do tại sao y_sel nên được tập trung là vì chúng tôi muốn thêm sự khác biệt tương đối có trọng số của Gaussian vào mục tại trung tâm. Nếu bạn nhân trực tiếp g với y_sel, không chỉ các giá trị của các mục lân cận trong cửa sổ, mà cả giá trị của mục nhập trung tâm sẽ được Gaussian cân. Điều này chắc chắn sẽ thay đổi đáng kể các giá trị hàm.

Dưới đây là giải pháp của tôi bằng cách sử dụng

def g_func[xx]:
    
    std = len[xx]
    mean = np.mean[xx]
    return 1 / [std*np.sqrt[2*np.pi]] * np.exp[-1/2*[[xx-mean]/std]**2]


def get_convolution[array,half_window_size]:
    
    array = np.concatenate[[np.repeat[array[0],half_window_size],
                            array,
                            np.repeat[array[-1],half_window_size]]]
    window_inds = [list[range[ind-half_window_size,ind+half_window_size+1]] \
                   for ind in range[half_window_size,len[array]-half_window_size]]
    
    return np.take[array,window_inds]

xy = np.load['1D_data.npz']['arr_0']
x, y = xy[:, 0], xy[:, 1]

half_window_size = 4
x_conv = np.apply_along_axis[g_func,axis=1,arr=get_convolution[x,half_window_size=half_window_size]]
y_conv = get_convolution[y,half_window_size=half_window_size]
y_mean = np.mean[y_conv,axis=1]
y_centered = y_conv - y_mean[:,None]
smoothed = np.sum[x_conv*y_centered,axis=1] / [half_window_size*2] + y_mean

fig,ax = plt.subplots[figsize=[10,6]]
ax.plot[x, y, '-k']
ax.plot[x, smoothed, '-r']
0

def g_func[xx, w=1.0]:
    mean = np.mean[xx]
    a = 0.47 * w
    return [1 / a] * np.exp[[[xx-mean] / a] ** 2 * [-np.pi]]


def get_convolution[array,half_window_size]:
    
    array = np.concatenate[[np.repeat[array[0],half_window_size],
                            array,
                            np.repeat[array[-1],half_window_size]]]
    window_inds = [list[range[ind-half_window_size,ind+half_window_size+1]] \
                   for ind in range[half_window_size,len[array]-half_window_size]]
    
    return np.take[array,window_inds]

xy = np.load['1D_data.npz']['arr_0']
x, y = xy[:, 0], xy[:, 1]

half_window_size = 4
x_conv = np.apply_along_axis[g_func,axis=1,arr=get_convolution[x,half_window_size=half_window_size]]
y_conv = get_convolution[y,half_window_size=half_window_size]
y_mean = np.mean[y_conv,axis=1]
y_centered = y_conv - y_mean[:,None]
smoothed = np.sum[x_conv*y_centered,axis=1] / [half_window_size*2] + y_mean

fig,ax = plt.subplots[figsize=[10,6]]
ax.plot[x, y, '-k']
ax.plot[x, smoothed, '-r']

Chạy mã, đầu ra là

CẬP NHẬT

Để thống nhất

def g_func[xx]:
    
    std = len[xx]
    mean = np.mean[xx]
    return 1 / [std*np.sqrt[2*np.pi]] * np.exp[-1/2*[[xx-mean]/std]**2]


def get_convolution[array,half_window_size]:
    
    array = np.concatenate[[np.repeat[array[0],half_window_size],
                            array,
                            np.repeat[array[-1],half_window_size]]]
    window_inds = [list[range[ind-half_window_size,ind+half_window_size+1]] \
                   for ind in range[half_window_size,len[array]-half_window_size]]
    
    return np.take[array,window_inds]

xy = np.load['1D_data.npz']['arr_0']
x, y = xy[:, 0], xy[:, 1]

half_window_size = 4
x_conv = np.apply_along_axis[g_func,axis=1,arr=get_convolution[x,half_window_size=half_window_size]]
y_conv = get_convolution[y,half_window_size=half_window_size]
y_mean = np.mean[y_conv,axis=1]
y_centered = y_conv - y_mean[:,None]
smoothed = np.sum[x_conv*y_centered,axis=1] / [half_window_size*2] + y_mean

fig,ax = plt.subplots[figsize=[10,6]]
ax.plot[x, y, '-k']
ax.plot[x, smoothed, '-r']
1 với
def g_func[xx]:
    
    std = len[xx]
    mean = np.mean[xx]
    return 1 / [std*np.sqrt[2*np.pi]] * np.exp[-1/2*[[xx-mean]/std]**2]


def get_convolution[array,half_window_size]:
    
    array = np.concatenate[[np.repeat[array[0],half_window_size],
                            array,
                            np.repeat[array[-1],half_window_size]]]
    window_inds = [list[range[ind-half_window_size,ind+half_window_size+1]] \
                   for ind in range[half_window_size,len[array]-half_window_size]]
    
    return np.take[array,window_inds]

xy = np.load['1D_data.npz']['arr_0']
x, y = xy[:, 0], xy[:, 1]

half_window_size = 4
x_conv = np.apply_along_axis[g_func,axis=1,arr=get_convolution[x,half_window_size=half_window_size]]
y_conv = get_convolution[y,half_window_size=half_window_size]
y_mean = np.mean[y_conv,axis=1]
y_centered = y_conv - y_mean[:,None]
smoothed = np.sum[x_conv*y_centered,axis=1] / [half_window_size*2] + y_mean

fig,ax = plt.subplots[figsize=[10,6]]
ax.plot[x, y, '-k']
ax.plot[x, smoothed, '-r']
2, đây là một khả năng, ý tưởng là để cho độ lệch chuẩn của Gaussian là
def g_func[xx]:
    
    std = len[xx]
    mean = np.mean[xx]
    return 1 / [std*np.sqrt[2*np.pi]] * np.exp[-1/2*[[xx-mean]/std]**2]


def get_convolution[array,half_window_size]:
    
    array = np.concatenate[[np.repeat[array[0],half_window_size],
                            array,
                            np.repeat[array[-1],half_window_size]]]
    window_inds = [list[range[ind-half_window_size,ind+half_window_size+1]] \
                   for ind in range[half_window_size,len[array]-half_window_size]]
    
    return np.take[array,window_inds]

xy = np.load['1D_data.npz']['arr_0']
x, y = xy[:, 0], xy[:, 1]

half_window_size = 4
x_conv = np.apply_along_axis[g_func,axis=1,arr=get_convolution[x,half_window_size=half_window_size]]
y_conv = get_convolution[y,half_window_size=half_window_size]
y_mean = np.mean[y_conv,axis=1]
y_centered = y_conv - y_mean[:,None]
smoothed = np.sum[x_conv*y_centered,axis=1] / [half_window_size*2] + y_mean

fig,ax = plt.subplots[figsize=[10,6]]
ax.plot[x, y, '-k']
ax.plot[x, smoothed, '-r']
3

def g_func[xx]:
    
    std = len[xx]
    mean = np.mean[xx]
    return 1 / [std*np.sqrt[2*np.pi]] * np.exp[-1/2*[[xx-mean]/std]**2]


def get_convolution[array,half_window_size]:
    
    array = np.concatenate[[np.repeat[array[0],half_window_size],
                            array,
                            np.repeat[array[-1],half_window_size]]]
    window_inds = [list[range[ind-half_window_size,ind+half_window_size+1]] \
                   for ind in range[half_window_size,len[array]-half_window_size]]
    
    return np.take[array,window_inds]

xy = np.load['1D_data.npz']['arr_0']
x, y = xy[:, 0], xy[:, 1]

half_window_size = 4
x_conv = np.apply_along_axis[g_func,axis=1,arr=get_convolution[x,half_window_size=half_window_size]]
y_conv = get_convolution[y,half_window_size=half_window_size]
y_mean = np.mean[y_conv,axis=1]
y_centered = y_conv - y_mean[:,None]
smoothed = np.sum[x_conv*y_centered,axis=1] / [half_window_size*2] + y_mean

fig,ax = plt.subplots[figsize=[10,6]]
ax.plot[x, y, '-k']
ax.plot[x, smoothed, '-r']

Nội phân chính

  • Làm thế nào để bạn tạo một bộ lọc Gaussian trong Python?
  • Bộ lọc Gaussian trong Python là gì?
  • Bộ lọc Gaussian trong học tập sâu là gì?
  • Làm cách nào để áp dụng bộ lọc Gaussian vào ảnh?

Bộ lọc Gaussian được sử dụng để giảm nhiễu trong hình ảnh và cả các chi tiết của hình ảnh. Bộ lọc Gaussian luôn được ưa thích so với bộ lọc hộp.

Các bước liên quan đến việc thực hiện bộ lọc Gaussian từ đầu trên một hình ảnh:

  1. Xác định hàm tích chập được lặp lại trên hình ảnh dựa trên kích thước hạt nhân [bộ lọc Gaussian]. Trong hình bên dưới hình ảnh bên trái biểu thị hình ảnh cũ với hộp màu đỏ là kernel tính toán giá trị từ tất cả chín pixel và chèn vào pixel trung tâm.

def convolution[oldimage, kernel]:
#image = Image.fromarray[image, 'RGB']
image_h = oldimage.shape[0]
image_w = oldimage.shape[1]

kernel_h = kernel.shape[0]
kernel_w = kernel.shape[1]

if[len[oldimage.shape] == 3]:
image_pad = np.pad[oldimage, pad_width=[
\[kernel_h // 2, kernel_h // 2],[kernel_w // 2,
\kernel_w // 2],[0,0]], mode='constant',
\constant_values=0].astype[np.float32]

elif[len[oldimage.shape] == 2]:
image_pad = np.pad[oldimage, pad_width=[
\[kernel_h // 2, kernel_h // 2],[kernel_w // 2,
\kernel_w // 2]], mode='constant', constant_values=0]
\.astype[np.float32]

h = kernel_h // 2
w = kernel_w // 2

image_conv = np.zeros[image_pad.shape]

for i in range[h, image_pad.shape[0]-h]:
for j in range[w, image_pad.shape[1]-w]:
#sum = 0
x = image_pad[i-h:i-h+kernel_h, j-w:j-w+kernel_w]
x = x.flatten[]*kernel.flatten[]
image_conv[i][j] = x.sum[]
h_end = -h
w_end = -w

if[h == 0]:
return image_conv[h:,w:w_end]
if[w == 0]:
return image_conv[h:h_end,w:]

return image_conv[h:h_end,w:w_end]

2. Xác định hàm Gaussian dựa trên kích thước của Sigma [độ lệch chuẩn].

Formula:

def GaussianBlurImage[image, sigma]:
#image = imread[image]
image = Image.open[image]
image = np.asarray[image]
#print[image]
filter_size = 2 * int[4 * sigma + 0.5] + 1
gaussian_filter = np.zeros[[filter_size, filter_size], np.float32]
m = filter_size//2
n = filter_size//2

for x in range[-m, m+1]:
for y in range[-n, n+1]:
x1 = 2*np.pi*[sigma**2]
x2 = np.exp[-[x**2 + y**2]/[2* sigma**2]]
gaussian_filter[x+m, y+n] = [1/x1]*x2

im_filtered = np.zeros_like[image, dtype=np.float32]
for c in range[3]:
im_filtered[:, :, c] = convolution[image[:, :, c], gaussian_filter]

return [im_filtered.astype[np.uint8]]

Ảnh gốc:

Hình ảnh đầu ra cuối cùng sau khi áp dụng bộ lọc Gaussian:

Cảm ơn vì đã đọc.

Làm thế nào để bạn tạo một bộ lọc Gaussian trong Python?

Bộ lọc Gaussian trong Python là gì?Create a function named gaussian_kernel[] , which takes mainly two parameters. The size of the kernel and the standard deviation. array[[-2., -1., 0., 1., 2.]] Then we will create the outer product and normalize to make sure the center value is always 1.

Bộ lọc Gaussian trong Python là gì?

Bộ lọc Gaussian trong học tập sâu là gì?a low pass filter used for reducing noise [high frequency components] and blurring regions of an image. The filter is implemented as an Odd sized Symmetric Kernel [DIP version of a Matrix] which is passed through each pixel of the Region of Interest to get the desired effect.

Bộ lọc Gaussian trong học tập sâu là gì?

Bộ lọc Gaussian được sử dụng để giảm nhiễu trong hình ảnh và cả các chi tiết của hình ảnh. Bộ lọc Gaussian luôn được ưa thích so với bộ lọc hộp.a nonuniform low-pass filter that preserves low spatial frequency and reduces image noise and negligible details in an image.

Làm cách nào để áp dụng bộ lọc Gaussian vào ảnh?

Áp dụng các bộ lọc làm mịn Gaussian vào hình ảnh..

I = Imread ['Cameraman. ....

Hình Imshow [i] Tiêu đề ['Hình ảnh gốc'].

Hình IMSHOW [IBLUR1] Tiêu đề ['Hình ảnh được làm mịn, \ sigma = 2'].

Hình IMSHOW [IBLUR2] Tiêu đề ['Hình ảnh được làm mịn, \ sigma = 4'].

Hình IMSHOW [IBLUR3] Tiêu đề ['Hình ảnh được làm mịn, \ sigma = 8'].

Bài Viết Liên Quan

Chủ Đề