GAUSSIAN FILTER

ChangSeong Yooยท2023๋…„ 7์›” 16์ผ
0

Vision

๋ชฉ๋ก ๋ณด๊ธฐ
1/5
post-thumbnail

๐Ÿ“์ด ํฌ์ŠคํŠธ์—์„œ Gaussian Filter์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.




์ •์˜

์žก์Œ์„ ์ œ๊ฑฐํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ํ•ด์ฃผ๊ณ  ํ”ฝ์…€๋“ค ๋งˆ๋‹ค ๊ฐ€์ค‘์น˜๋ฅผ ๋‘์–ด ์ค‘์•™์— ์œ„์น˜ํ•œ ํ”ฝ์…€์ด ๊ฐ€์ค‘์น˜๊ฐ€ ๋†’๊ณ  ์ค‘์•™ ํ”ฝ์…€์—์„œ ๋ฉ€์–ด์งˆ์ˆ˜๋ก ๊ฐ€์ค‘์น˜๋ฅผ ์•ฝํ•˜๊ฒŒ ๋‘๋Š” ํ•„ํ„ฐ์ด๋‹ค.


๋ชฉ์ 

  • Gaussian Filter๋Š” ์ด๋ฏธ์ง€์˜ ์žก์Œ์„ ๊ฐ์†Œ์‹œํ‚ค๊ฑฐ๋‚˜ ๋ถ€๋“œ๋Ÿฌ์šด ํšจ๊ณผ๋ฅผ ์ฃผ๋Š” ๋ฐ์— ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ์ฃผ๋ณ€ ํ”ฝ์…€๋“ค๊ณผ์˜ ๊ฐ€์ค‘ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•˜์—ฌ ํ”ฝ์…€ ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” smoothing ํšจ๊ณผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ•„ํ„ฐ์˜ ์ธ๋ฑ์Šค ๋ณ„๋กœ ๊ฐ€์ค‘์น˜๊ฐ€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํ•„ํ„ฐ์˜ ์ค‘์•™์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ๊ฐ€์ค‘์น˜๋ฅผ ๋†’์ด๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•„ํ„ฐ์˜ ๊ฐ€์žฅ์ž๋ฆฌ๋กœ ๊ฐˆ์ˆ˜๋ก ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ์‡„์‹œ์ผœ ์ฃผ๋ณ€์˜ ์žก์Œ์˜ ์˜ํ–ฅ์„ ์ค„์ด๋Š” ํ•„ํ„ฐ์ž…๋‹ˆ๋‹ค.
  • noise ๊ฐ์†Œ: ์ด๋ฏธ์ง€์˜ ์žก์Œ์„ ๊ฐ์†Œ์‹œํ‚ค๋Š” ๋ฐ์— ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค. ์žก์Œ์ด ๊ณ ์ฃผํŒŒ ์„ฑ๋ถ„์œผ๋กœ ๊ฐ„์ฃผ๋˜๊ณ , ๊ณ ์ฃผํŒŒ ์„ฑ๋ถ„์„ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ํ๋ฆฌ๊ฒŒ ๋งŒ๋“ค์–ด ์žก์Œ์„ ์ œ๊ฑฐํ•˜๋Š” ํšจ๊ณผ๋ฅผ ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์›๋ฆฌ

๊ฐ€์šฐ์‹œ์•ˆ ํ•„ํ„ฐ๋ฅผ ์ˆ˜์‹์œผ๋กœ ๋ณด๋ฉด

G(x,y)=12ฯ€ฯƒ2eโˆ’x2+y22ฯƒ2G(x,y) = \frac{1}{2\pi\sigma^2}e^{-\frac{x^2+y^2}{2\sigma^2}}

๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์œ„ ์ˆ˜์‹์„ ๊ทธ๋ž˜ํ”„๋กœ ์ดํ•ดํ•ด๋ณด๋ฉด

์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
ํŽธ์ฐจ(ฯƒ\sigma)๊ฐ€ ํฌ๋ฉด ํด์ˆ˜๋ก ๊ฐ€์ค‘์น˜๋“ค์ด ๋„๋ฆฌ ๋ถ„ํฌ ๋˜์–ด ํ˜•์„ฑ๋˜๊ณ 
ํŽธ์ฐจ๊ฐ€ ์ž‘์œผ๋ฉด ์ž‘์„์ˆ˜๋ก ๊ฐ€์ค‘์น˜๋“ค์ด ์ค‘์•™์— ๋ฐ€์ง‘ํ•˜๊ฒŒ ๋œ๋‹ค.


์ฝ”๋“œ ์ดํ•ด

  • ๋‹จ์ˆœํ•œ ๊ตฌํ˜„

    ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•ด๋ณด๋ฉด
gaussian_filter3x3 = np.array([[1, 2, 1],
                               [2, 4, 2],
                               [1, 2, 1]])
gaussian_filter3x3 = gaussian_filter3x3 / np.sum(gaussian_filter3x3)
192919294929192919\begin{matrix} \frac{1}{9} & \frac{2}{9} & \frac{1}{9} \\ \frac{2}{9} & \frac{4}{9} & \frac{2}{9} \\ \frac{1}{9} & \frac{2}{9} & \frac{1}{9} \end{matrix}
gaussian_filter5x5 = np.array([[1, 2, 4, 2, 1],
                               [2, 4, 8, 4, 2],
                               [4, 8,16, 8, 4],
                               [2, 4, 8, 4, 2],
                               [1, 2, 4, 2, 1]])
gaussian_filter5x5 = gaussian_filter5x5 / np.sum(gaussian_filter5x5)
1252254252251252254258254252254258251625825425225425825425225125225425225125\begin{matrix} \frac{1}{25} & \frac{2}{25} & \frac{4}{25} & \frac{2}{25} & \frac{1}{25} \\ \frac{2}{25} & \frac{4}{25} & \frac{8}{25} & \frac{4}{25} & \frac{2}{25} \\ \frac{4}{25} & \frac{8}{25} & \frac{16}{25} & \frac{8}{25} & \frac{4}{25} \\ \frac{2}{25} & \frac{4}{25} & \frac{8}{25} & \frac{4}{25} & \frac{2}{25} \\ \frac{1}{25} & \frac{2}{25} & \frac{4}{25} & \frac{2}{25} & \frac{1}{25} \\ \end{matrix}
# 3x3
result_box3x3 = Convolution2D(box_filter3x3, image_man)

cv2_imshow(result_box3x3)

# 5x5
result_box5x5 = Convolution2D(box_filter5x5, image_man)

cv2_imshow(result_box5x5)
์›๋ณธ์›๋ณธ

3X3๊ฐ€์šฐ์‹œ์•ˆํ•„ํ„ฐ3X3๊ฐ€์šฐ์‹œ์•ˆํ•„ํ„ฐ

5X5๊ฐ€์šฐ์‹œ์•ˆํ•„ํ„ฐ5X5๊ฐ€์šฐ์‹œ์•ˆํ•„ํ„ฐ

์œ„ ์‚ฌ์ง„๋“ค์„ ๋น„๊ตํ•ด๋ณด๋ฉด ํ•„ํ„ฐ ํฌ๊ธฐ๋ฅผ ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ smoothing ํšจ๊ณผ๊ฐ€ ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ถ”๊ฐ€๋กœ ํ‰๊ท ํ•„ํ„ฐ์™€ ๊ฐ€์šฐ์‹œ์•ˆํ•„ํ„ฐ๋ฅผ ๋น„๊ตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
https://velog.io/@mykirk98/AVERAGE-FILTER
๐Ÿ“—ํ‰๊ท ํ•„ํ„ฐ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์ด ํฌ์ŠคํŠธ์— ์„ค๋ช…๋˜์–ด์žˆ์œผ๋‹ˆ ๊ถ๊ธˆํ•˜์‹œ๋ฉด ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

5X5ํ‰๊ท ํ•„ํ„ฐ5X5ํ‰๊ท ํ•„ํ„ฐ

5X5๊ฐ€์šฐ์‹œ์•ˆํ•„ํ„ฐ5X5๊ฐ€์šฐ์‹œ์•ˆํ•„ํ„ฐ

ํ‰๊ท  ํ•„ํ„ฐ์™€ ๊ฐ€์šฐ์‹œ์•ˆ ํ•„ํ„ฐ๋ฅผ ๋น„๊ตํ•ด๋ณด๋ฉด ์ฐจ์ด๊ฐ€ ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ—๏ธ๊ทธ ์ด์œ ๋Š” ํ•„ํ„ฐ์˜ ๊ฐ€์ค‘์น˜ ์ฐจ์ด์ž…๋‹ˆ๋‹ค. ๊ฐ€์šฐ์‹œ์•ˆ ํ•„ํ„ฐ๋Š” ํ•„ํ„ฐ์˜ ์ค‘์•™์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ๊ฐ€์ค‘์น˜๋ฅผ ๋†’๊ฒŒ ๋‘์ง€๋งŒ, ํ‰๊ท  ํ•„ํ„ฐ๋Š” ๋ชจ๋“  ์ธ๋ฑ์Šค๊ฐ€ ๋‹ค ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€์ง€๋ฏ€๋กœ ๊ฐ€์ค‘์น˜๋ฅผ ๋‘๋Š” ๊ฐ€์šฐ์‹œ์•ˆ ํ•„ํ„ฐ๊ฐ€ ๋ฏธ์„ธํ•˜์ง€๋งŒ ๋น„๊ต์  ํ‰๊ท  ํ•„ํ„ฐ๋ณด๋‹ค ์„ ๋ช…ํ•จ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์™„๋ฒฝํ•œ ๊ตฌํ˜„

    ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์ˆ˜ํ•™์  ์ˆ˜์‹์„ ์™„๋ฒฝํ•˜๊ฒŒ ๋ฌ˜์‚ฌํ•˜์—ฌ ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
# size : ์ปค๋„ ํฌ๊ธฐ       sigma : ํ‘œ์ค€ํŽธ์ฐจ (sigma^2 : ๋ถ„์‚ฐ)
def Gaussian(size, sigma):
    #์ค‘์‹ฌ์—์„œ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ
    array = np.arange((size//2)*(-1), (size//2)+1)
    
    #x^2+y^2 ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
    xx_yy_array = np.zeros((size, size))
    
    for x in range(size):
        for y in range(size):
            #์ค‘์‹ฌ์—์„œ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ œ๊ณฑํ•ฉ์œผ๋กœ ๊ณ„์‚ฐ
            xx_yy_array[x,y] = array[x]**2+array[y]**2
    
    # ํ•„ํ„ฐ ์ดˆ๊ธฐํ™”
    filter = np.zeros((size, size))
    
    for x in range(size):
        for y in range(size):
             # ์ˆ˜ํ•™์  ์ˆ˜์‹ ๊ตฌํ˜„๋ถ€
             filter[x,y] = 1 / (2 * np.pi * sigma**2) * np.exp(-xx_yy_array[x,y]/(2 * sigma**2))

    # Scaling
    filter /= filter.sum()
    
    return filter

์ œ๊ฐ€ ๊ฐ€์šฐ์‹œ์•ˆ ํ•„ํ„ฐ ํ•จ์ˆ˜๋Š” ์ด๋ ‡์Šต๋‹ˆ๋‹ค.
์ด์ œ ์ด๋ฏธ์ง€์— ์ ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

# ํ‘œ์ค€ํŽธ์ฐจ๋Š” 5, 11X11 ํฌ๊ธฐ์˜ ๊ฐ€์šฐ์‹œ์•ˆ ํ•„ํ„ฐ ๊ฐ์ฒด ์ƒ์„ฑ
gaussian_11X11 = Gaussian(11, 5)
# ํ•„ํ„ฐ ์ ์šฉ
gaussian_MJ = cv2.filter2D(image_MJ, -1, gaussian_11X11)

# cv2.filter2D(src, ddepth, kernel)
#src: ํ•„ํ„ฐ๋ฅผ ์ ์šฉํ•  ๋Œ€์ƒ ์ด๋ฏธ์ง€
#ddepth: ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ฒฐ์ •ํ•˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ. -1์„ ์ฃผ๋ฉด src์™€ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ฐ€์ง€๊ฒŒ ๋จ
#kernel: ์ ์šฉํ•  ํ•„ํ„ฐ

์›๋ณธ ์ด๋ฏธ์ง€

๊ฐ€์šฐ์‹œ์•ˆ ํ•„ํ„ฐ ์ ์šฉ ์ด๋ฏธ์ง€

์ €๋Š” ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ํฌ๊ฒŒ ์ฃผ์–ด ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ„์‚ฐ์‹œํ‚จ ๊ฐ€์šฐ์‹œ์•ˆ ํ•„ํ„ฐ๋ฅผ ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
๊ฒฐ๊ณผ๋Š” ํ๋ฆฟํ•˜๊ฒŒ ๋‚˜์™”์ฃ .
๋ฐ˜๋Œ€๋กœ ํ‘œ์ค€ํŽธ์ฐจ๋ฅผ ์ž‘๊ฒŒ ์ฃผ๊ณ  ํ•„ํ„ฐ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ 3X3 or 5X5๋กœ ์ค€๋‹ค๋ฉด ๋”์šฑ ์„ ๋ช…ํ•ด์ง€๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋„์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

profile
๐’ฅ๐“Š๐“ƒ๐’พโ„ด๐“‡ ๐’Ÿ๐’ถ๐“‰๐’ถ ๐’ฎ๐’ธ๐’พโ„ฏ๐“ƒ๐“‰๐’พ๐“ˆ๐“‰

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 7์›” 17์ผ

์ €๋„ ๊ฐœ๋ฐœ์ž์ธ๋ฐ ๊ฐ™์ด ๊ต๋ฅ˜ ๋งŽ์ด ํ•ด๋ด์š” ใ…Žใ…Ž! ์„œ๋กœ ํ™”์ดํŒ…ํ•ฉ์‹œ๋‹ค!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ