Giter Club home page Giter Club logo

edge_detection's Introduction

๐Ÿ“Œ Edge_Detection

I. Edge Detection ์ด๋ž€?

Edge๋Š” ๊ฐ’(๋ฐ๊ธฐ, color, โ€ฆ)์ด ์ฐจ์ด๊ฐ€ ๋‚˜๋Š” ๊ฒฝ๊ณ„๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ธ๊ฐ„์€ ์–ด๋– ํ•œ ์ด๋ฏธ์ง€๋กœ๋ถ€ํ„ฐ๋“  ์ด edge๋ฅผ ์‰ฝ๊ฒŒ ์ธ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ปดํ“จํ„ฐ๋Š” ์ด ์ž‘์—…์„ ํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๊ณผ์ •์„ ๊ฑฐ์ณ์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋Ÿฌํ•œ edge detection์€ ์™œ ํ•„์š”ํ• ๊นŒ? ์ตœ๊ทผ ์—ฌ๋Ÿฌ ์ž๋™์ฐจ ํšŒ์‚ฌ์—์„œ ์„ ๋ณด์ด๊ณ  ์žˆ๋Š” ์ž์œจ ์ฃผํ–‰ ์„œ๋น„์Šค๋ฅผ ์˜ˆ๋กœ ๋“ค์ž๋ฉด, ์ฐจ์„ ์ธ์‹๋„ ์ด๋Ÿฐ edge detection์— ์ผ์ข…์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์˜๋ฃŒ์šฉ์ด๋‚˜ ๋‹ค์–‘ํ•œ ๊ณณ์— edge detection์ด ์‚ฌ์šฉ๋œ๋‹ค.

II. Sobel Filter (Gradient & Threshold)

์ด๋Ÿฌํ•œ edge๋ฅผ detectํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ”ฝ์…€์˜ ๊ฐ’์ด ๊ธ‰๊ฒฉํžˆ ๋ณ€ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ฐพ๋Š”๋‹ค. ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” 1์ฐจ ๋ฏธ๋ถ„์„ ํ†ตํ•ด ๊ทธ ๊ฐ’์ด ํฐ ๋ถ€๋ถ„์„ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, 1์ฐจ ๋ฏธ๋ถ„์˜ ๋ณ€ํ™”์œจ์ด ํฐ ๊ณณ์„ ์ฐพ๋Š”๋‹ค. ๊ทธ ํ›„ threshold๋ฅผ ์ •ํ•ด ๊ทธ ์•ˆ์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„๋งŒ edge๋ผ๊ณ  ๊ฒ€์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. image

Sobel Filter๋ฅผ ์‚ฌ์šฉํ•ด Edge Detection์„ ์ˆ˜ํ–‰ํ•˜์˜€๊ณ , Python์œผ๋กœ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. ์ฒซ๋ฒˆ์งธ๋กœ, ๊ตฌํ˜„์„ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ผ์ด ๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ ๋ชจ๋“ˆ์„ importํ•œ๋‹ค.

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

๊ทธ ํ›„, x์™€ y๋ฐฉํ–ฅ์œผ๋กœ ์ ์šฉํ•  sobel ํ•„ํ„ฐ array๋ฅผ ๋งŒ๋“ ๋‹ค.

sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
sobel_y = np.array([[ 1, 2, 1], [ 0, 0, 0], [-1,-2,-1]])

์ด๋ ‡๊ฒŒ ๋งŒ๋“  ํ•„ํ„ฐ๋ฅผ ์ด๋ฏธ์ง€์— ๊ฐ๊ฐ ์ ์šฉ์‹œํ‚ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

def edge_detection(img, filter_x, filter_y, threshold, show_img=True):

    img_shape = img.shape
    filter_size = filter_x.shape

    # Edge Detection์„ ์ €์žฅํ•  array
    result_arr = tuple(np.array(img_shape)-np.array(filter_size)+1)

    # ๊ฒฐ๊ณผ ํ–‰๋ ฌ ์ดˆ๊ธฐํ™”
    x_result = np.zeros(result_arr)
    y_result = np.zeros(result_arr)

    # edge detetion ์ˆ˜ํ–‰
    for h in range(0, result_arr[0]):
        for w in range(0, result_arr[1]):
            # ํ•„ํ„ฐ ์ ์šฉ
            tmp = img[h:h+filter_size[0],w:w+filter_size[1]]
            # x์ถ• ์ ์šฉ
            x_result[h,w] = np.abs(np.sum(tmp*filter_x))
            # y์ถ• ์ ์šฉ
            y_result[h,w] = np.abs(np.sum(tmp*filter_y))

image

๋‘ ๋ฐฉํ–ฅ์˜ ์ด๋ฏธ์ง€๋ฅผ ํ•ฉ์นœ ํ›„ threshold๋ฅผ ์ ์šฉํ•œ ๋ชจ์Šต์ด๋‹ค. Threshold๋ฅผ ๋†’๊ฒŒ ์„ค์ •ํ• ์ˆ˜๋ก ๋’ค์˜ ์—ด๊ธฐ๊ตฌ๋ฅผ ๊ฐ์ง€ํ•œ ๋ถ€๋ถ„์ด ์‚ฌ๋ผ์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ threshold๊ฐ€ 300์ผ๋•Œ์™€ 500์ผ ๋•Œ๋ฅผ ๋น„๊ตํ•ด๋ณด๋ฉด 500์ผ๋•Œ ์—ฌ์„ฑ์˜ ์–ด๊นจ์™€ ๋“ฑ์ชฝ์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„๋„ ์‚ฌ๋ผ์ง€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

    # ๋‘ ๊ฒฐ๊ณผ๋ฅผ ํ•ฉ์นœ๋‹ค.       
    result = x_result + y_result
    thr_result = np.zeros(result_arr)
    thr_result[result>threshold] = 1

image

ํ•ด๋‹น ์ด๋ฏธ์ง€์˜ ๋’ท๋ฐฐ๊ฒฝ์— ํ•ด๋‹นํ•˜๋Š” ์—ด๊ธฐ๊ตฌ ๊ทผ์ฒ˜๋ฅผ ๋ณด๋ฉด ์ ์ฒ˜๋Ÿผ ์ธ์‹๋˜๋Š” ๋ถ€๋ถ„๋“ค์ด ์žˆ๋‹ค. ์ด๋ฏธ์ง€๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋…ธ์ด์ฆˆ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ๋ฏธ๋ถ„์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๋” ์‹ฌํ•ด์ง„๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Sobel Filter๋ฅผ ์ ์šฉํ•  ๋•Œ๋Š” ๋ฏธ๋ถ„์„ ํ•˜์˜€์Œ์—๋„ ๋…ธ์ด์ฆˆ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ณผ์ •์ด ์—†์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด์— ๋Œ€ํ•œ ๋…ธ์ด์ฆˆ๊ฐ€ ์ปค์ ธ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์•˜์„ ๊ฒƒ์ด๋ผ ์˜ˆ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋” ๋งค๋„๋Ÿฝ๊ฒŒ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด noise๋ฅผ ๊ฐ์†Œ์‹œํ‚ค๋Š” smoothing filter๋ฅผ ์ ์šฉํ•ด noise๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ ์ž ํ•œ๋‹ค.

III. Gaussian Smoothing Filter

Gaussian smoothing์€ gaussian distribution์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋ฉฐ, ์ค‘์‹ฌ์— ์žˆ๋Š” ํ”ฝ์…€์—์„œ๋ถ€ํ„ฐ ๋ฉ€์–ด์งˆ ์ˆ˜๋ก ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘์•„์ง€๋„๋ก ์„ค์ •์ด ๋œ๋‹ค. ์ฆ‰, ๊ตฌํ•˜๊ณ ์žํ•˜๋Š” target pixel์ด ๊ฐ€์žฅ ํฐ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๊ณ  ์ค‘์‹ฌ์—์„œ ๋ฉ€์–ด์งˆ์ˆ˜๋ก ํ”ฝ์…€์ด ์ ์€ weight๋ฅผ ๊ฐ–๋Š”๋‹ค.

image ์ด๋Ÿฐ 2์ฐจ์› ๊ฐ€์šฐ์‹œ์•ˆ ์ปค๋„์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜์˜€๋‹ค

def gaussianKernel(size, sigma):
    # ์ค‘์‹ฌ์œผ๋กœ๋ถ€ํ„ฐ ๊ฑฐ๋ฆฌ ๋ฐฐ์—ด ์ƒ์„ฑ
    arr_dis = np.arange((size//2)*(-1), (size//2)+1)

    # array ์ดˆ๊ธฐํ™”
    arr = np.zeros((size, size))

    # ์ค‘์‹ฌ์—์„œ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ œ๊ณฑํ•ฉ์œผ๋กœ ๊ณ„์‚ฐ
    for x in range(size):
        for y in range(size):
            arr[x,y] = arr_dis[x]**2+arr_dis[y]**2

    # ์ปค๋„์˜ ๊ฐ’์„ ์ €์žฅํ•  ๋งคํŠธ๋ฆญ์Šค ์ƒ์„ฑ        
    kernel = np.zeros((size, size))
    
    # ๊ฐ€์šฐ์‹œ์•ˆ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์šฐ์‹œ์•ˆ ์ปค๋„ ์ƒ์„ฑ
    for x in range(size):
        for y in range(size):
             kernel[x,y] = np.exp(-arr[x,y]/(2*sigma**2))

    # ์ปค๋„์˜ ํ•ฉ์ด 1์ด ๋˜๋„๋ก ์ •๊ทœํ™”
    kernel /= kernel.sum()
    
    return kernel

ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์›๋ณธ ์ด๋ฏธ์ง€์— ์Šค๋ฌด๋”ฉ์„ ์ ์šฉํ•œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™๋‹ค.

image

IV. Result

์ด์ „ ๋‹จ๊ณ„์—์„œ ๊ตฌํ˜„ํ•œ โ€˜gaussianKernel()โ€™์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์‹œ Sobel filter๋ฅผ ์ ์šฉํ•˜์—ฌ Edge Detection์„ ์ˆ˜ํ–‰ํ•ด๋ณด์•˜๋‹ค. ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

image

๊ธฐ์กด์— Sobel Filter๋งŒ ์ ์šฉํ–ˆ์„ ๋•Œ์™€ ๋น„๊ตํ•˜๋ฉด ์—ด๊ธฐ๊ตฌ๊ฐ€ ์žˆ๋Š” ์ชฝ์— ์ ์ฒ˜๋Ÿผ ์ธ์‹๋œ ๋ถ€๋ถ„์ด ์ œ๊ฑฐ๋˜์–ด ๋” ๊น”๋”ํ•ด์ง„ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

image

๋‹ค๋ฅธ ์ด๋ฏธ์ง€๋“ค์„ ์ ์šฉํ•œ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

image

image

image

image

edge_detection's People

Contributors

syiee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.