본문 바로가기

Lab/Basic Algorithms

Edge Detection - Sobel



sobel은 많이 쓰이는 edge detection 알고리즘중의 하나이다.

기본적으로 edge는 x, y direction을 구하고 이를 이용하여 magnitude을 구하여 이를 표시하면 edge을 구할수 있다.

plus,  x, y direction 을 이용하여 edge direction을 구할 수 있다.


또 하나의 중요한 것은 mask단위이므로 convolution 연산을 한다는 것이다. 


sobel mask


\mathbf{G}_x = \begin{bmatrix} 
-1 & 0 & +1 \\
-2 & 0 & +2 \\
-1 & 0 & +1 
\end{bmatrix} * \mathbf{A}
\quad
\mbox{and}
\quad   
\mathbf{G}_y = \begin{bmatrix} 
-1 & -2 & -1  \\
\ \ 0 & \ \ 0 & \ \ 0 \\
+1 & +2 & +1 
\end{bmatrix} * \mathbf{A}


magnitude

\mathbf{G} = \sqrt{ {\mathbf{G}_x}^2 + {\mathbf{G}_y}^2 }


angle

\mathbf{\Theta} = \operatorname{arctan}\left({ \mathbf{G}_y \over \mathbf{G}_x }\right).


소스 보기 


        int w = image->width;

        int h = image->height;


        // soble x, y

        int sx[3][3] = { {-1,  0,  1}, {-2, 0, 2}, {-1, 0, 1}};

        int sy[3][3] = { {-1, -2, -1}, { 0, 0, 0}, { 1, 2, 1}};


        for( int i=1; i<h-1; i++){

                for( int j=1; j<w-1; j++){


                        float dx = 0, dy = 0;

                        for(int n=0; n<3; n++){

                                for( int m=0; m<3; m++){

                                        dx+=sx[n][m]*_image[i+n-1][j+m-1];

                                        dy+=sy[n][m]*_image[i+n-1][j+m-1];

                                }

                        }

                        //

                        float mag = sqrtf(dx*dx+dy*dy);

                        if(mag>255) mag = 255;

                        if(mag<0) mag = 0;

                        _edge[i][j] = (int)mag;

                }

        }


'Lab > Basic Algorithms' 카테고리의 다른 글

Gaussian Blur - 1  (0) 2013.01.25
bilinear interpolation  (0) 2012.10.15