Computer Vision2009. 4. 22. 19:55

() 글을 읽기 전에 
 글은 저와 같은 OpenCV초보자를 위해서 정리하는 것입니다.
전문가이시라면 굳이 읽을 필요가 없습니다.
공부하면서 정리하는 글이라서 서툰 부분이 많이많이 보입니다.


SIFT의 내용을 따라가다 보면 Smooth함수를 호출합니다.

그래서 이번에 OpenCV Smooth함수를 아래와 같이 정리하고 테스트를 해보았습니다.

중요한 포인트는 번역을 했지만 억지로 번역하여 넣지 않았습니다.  

예제는 인터넷에서 다운 받은것을 조금 수정하였습니다.

( 인터넷에서 다운 받았는데, 나중에 페이지를 찾을려고 하니, 시간이 지난 뒤여서  찾지 못하였습니다.

   그때 그때 북마크를 걸어두어야 하는데, 나중에 보니 못찾겠더군요.. 쩝.. )

파라미터를 바꾸어가면서 테스트 할 수 있도록 수정하였기 때문에

필터 효과를 눈으로 확인을 할 수 있습니다.

덕분에 TrackBar의 사용법도 알게 되었습니다.

궁굼하신 분은 코드를 보시면 쉽게 이해가실 것입니다.

아래 그림은 실행시켰을때 나오는 그림입니다.

사용자 삽입 이미지
트랙바가 2개인데 위 트랙바는 필터의 종류를

두번째 트랙바는 필터의 인자를 뜻합니다.

프린트로 덤프하므로 하나씩 움직여가면서 테스트 해보시면 쉽게 결과를 보실 수 있습니다.


Smooth

OpenCV에서는 몇가지 방법으로 Smooth를 할 수 있게 하여 줍니다.

함수의 원형은 다음과 같습니다.

void cvSmooth( const CvArr* src, CvArr* dst,

               int smoothtype=CV_GAUSSIAN,

               int param1=3, int param2=0, double param3=0 );


smoothing 타입에 따라서 파라미터 인자의 역활이 바뀝니다.

요 부분만 알고 있으면 나머진 쉽습니다.

src

원본 이미지


dst

결과 이미지


smoothtype

smoothing 타입

  • CV_BLUR_NO_SCALE (simple blur with no scaling)
    • summation over a pixel param1×param2 neighborhood.
    • If the neighborhood size may vary, one may precompute integral image with cvIntegral function.
  • CV_BLUR (simple blur)
    • summation over a pixel param1×param2 neighborhood with subsequent scaling by 1/(param1param2).
  • CV_GAUSSIAN (gaussian blur)
    • convolving image with param1×param2 Gaussian kernel.
  • CV_MEDIAN (median blur)
    • finding median of param1×param1 neighborhood (i.e. the neighborhood is square).
  • CV_BILATERAL (bilateral filter)


param1

첫번째 인자

param2

두번째 인자

In case of simple scaled/non-scaled and Gaussian blur if param2 is zero, it is set to param1.

param3

         가우시안 방식일 경우 시그마를 넣어야 하지만,

이 파라미터가 0이면 커널 사이즈에서 자동으로 만들어진다.  수식은 아래와 같습니다.

              sigma = (n/2 - 1)*0.3 + 0.8,

                  where n=param1 for horizontal kernel,

                                                

                        n=param2 for vertical kernel.


  • Using standard sigma for small kernels (3×3 to 7×7) gives better speed.
  •       If param3 is not zero, while param1 and param2 are zeros, the kernel size is calculated from the sigma (to provide accurate enough operation).

보통의 시그마를 쓸 경우 그냥 좀 빨라진다. (차이는 ???)

         - 사실 차이라는게 크게 없는듯 합니다. 많이 하면 차이가 있겠지만 제 머쉰에서는 금방 결과가 나왔습니다.

         커널 사이즈를 안주고 (0으로 주고) 호출하면 역으로 시그마에서 커널 사이즈를 만듭니다.


The function cvSmooth smooths image using one of several methods. Every of the methods has some features and restrictions listed below

Blur with no scaling works with single-channel images only and supports accumulation of 8-bit to 16-bit format (similar to cvSobel and cvLaplace) and 32-bit floating point to 32-bit floating-point format.

Simple blur and Gaussian blur support 1- or 3-channel, 8-bit and 32-bit floating point images. These two methods can process images in-place.

Median and bilateral filters work with 1- or 3-channel 8-bit images and can not process images in-place.


아래는 소스 코드입니다.


#include <stdio.h>

#include <unistd.h>

#include <string.h>

#include <errno.h>


#include <opencv/cv.h>

#include <opencv/highgui.h>




int g_switch_value = 0;

int sigma_value  = 0;

int filterInt = 0;

int lastfilterInt = -1;

int lastsigma = -1;


void switch_callback( int position ){

filterInt = position;

}


void switch_sigma_callback( int position ){

  if(position < 3 )

    position = 4;


  sigma_value = position;

}



int main(int argc, char * argv[])

{

const char* name = "Filters Window";

float f_sigma ;

IplImage* img = cvLoadImage( "4390m.jpg",1 );

IplImage* out = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 3 );


cvNamedWindow( name, 1 );

cvShowImage(name, out);

// Other Variables

CvPoint seed_point = cvPoint(305,195);

CvScalar color = CV_RGB(250,0,0);


// Create trackbar

cvCreateTrackbar( "FILTER", name, &g_switch_value, 5, switch_callback );

cvCreateTrackbar( "parameter", name, &sigma_value, 11, switch_sigma_callback );


while( 1 ) {


switch( filterInt ){

case 0:

 cvSmooth( img, out, CV_BLUR, 7, 7 , 0 ,0 );

break;

case 1:

 f_sigma = ( sigma_value / 2 - 1 ) * 0.3 + 0.8 ;

 cvSmooth( img, out, CV_GAUSSIAN, 7 , 7 , f_sigma , f_sigma);

 

break;

case 2:

 if( sigma_value < 3 )  sigma_value = 3 ;

 if( sigma_value % 2  == 0)  sigma_value +=1 ;

 cvSmooth( img, out, CV_MEDIAN,  sigma_value  ,  sigma_value , 0 , 0);

 break;

case 3:

cvErode( img, out, NULL, 1);

break;

case 4:

cvDilate( img, out, NULL, 1);

break;

case 5:

cvFloodFill( out, seed_point, color, cvScalarAll(5.0), cvScalarAll(5.0), NULL, 4, NULL );

break;

}


if(

  (filterInt != lastfilterInt)||

  (sigma_value != lastsigma)

 )

 {

cvShowImage(name, out);

lastfilterInt = filterInt;

lastsigma     = sigma_value;


switch( filterInt ){

   case 0:

     printf("Blur Smooth\n");

     break;

   case 1:

     printf("Gaussian Smooth\n");

       printf(" Sigma %d\n",  sigma_value );  

     break;

   case 2:

     printf("Median Smooth : Filter Size = %d x %d\n" , sigma_value , sigma_value);

     break;

   case 3:

     printf("Erode\n");

     break;

   case 4:

          printf("Dilate\n");

     break;

   case 5:

       printf("Flood Fill\n");

     break;

           }

 }


if( cvWaitKey( 15 ) == 27 )

break;

}


cvReleaseImage( &img );

cvReleaseImage( &out );

cvDestroyWindow( name );

return 0;

}





'Computer Vision' 카테고리의 다른 글

OpenCV - Alpha Blending cvAddWeighted , cvFillPoly  (2) 2009.04.25
JPEG2AVI 를 맥에서 빌드하기  (0) 2009.04.24
Rob Hess의 SIFT [6]  (0) 2009.04.18
OpenCV - Convert Image  (0) 2009.04.11
OpenCV - Image Mask  (0) 2009.03.16
Posted by GUNDAM_IM