Chinaunix首页 | 论坛 | 博客
  • 博客访问: 263420
  • 博文数量: 28
  • 博客积分: 688
  • 博客等级: 上士
  • 技术积分: 365
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-16 10:25
文章分类

全部博文(28)

文章存档

2012年(28)

我的朋友

分类:

2012-09-17 10:59:34

3. 你能从图6-5显示的滤波器里建立一个可分核吗?如果可以,显示一下?
   图6-5如下所示。
 
 
核的部分点有负数,想求出与该核效果等同的分核,则需要知道这些数与图像中的像素数据计算时相互之间是什么具体关系,计算是如何进行的。因此,解答此题目我认为要查看OpenCV源代码中cvSobel()函数的具体实现,至少要搞清楚当aperture_size等于CV_SCHARR时的计算方式。
于是找到cvSobel的具体实现位置:cvderiv.cpp
  1. CV_IMPL void
  2. cvSobel( const void* srcarr, void* dstarr, int dx, int dy, int aperture_size )
  3. {
  4.     CvSepFilter filter;
  5.     void* buffer = 0;
  6.     int local_alloc = 0;

  7.     CV_FUNCNAME( "cvSobel" );

  8.     __BEGIN__;

  9.     int origin = 0;
  10.     int src_type, dst_type;
  11.     CvMat srcstub, *src = (CvMat*)srcarr;
  12.     CvMat dststub, *dst = (CvMat*)dstarr;

  13.     if( !CV_IS_MAT(src) )
  14.         CV_CALL( src = cvGetMat( src, &srcstub ));
  15.     if( !CV_IS_MAT(dst) )
  16.         CV_CALL( dst = cvGetMat( dst, &dststub ));

  17.     if( CV_IS_IMAGE_HDR( srcarr ))
  18.         origin = ((IplImage*)srcarr)->origin;

  19.     src_type = CV_MAT_TYPE( src->type );
  20.     dst_type = CV_MAT_TYPE( dst->type );

  21.     if( !CV_ARE_SIZES_EQ( src, dst ))
  22.         CV_ERROR( CV_StsBadArg, "src and dst have different sizes" );

  23.     if( ((aperture_size == CV_SCHARR || aperture_size == 3 || aperture_size == 5) &&
  24.         dx <= 2 && dy <= 2 && dx + dy <= 2 && icvFilterSobelNegVertBorder_8u16s_C1R_p) &&
  25.         (src_type == CV_8UC1 && dst_type == CV_16SC1 ||
  26.         src_type == CV_32FC1 && dst_type == CV_32FC1) )
  27.     {
  28.         CvDerivGetBufSizeIPPFunc ipp_sobel_getbufsize_func = 0;
  29.         CvDerivIPPFunc_8u ipp_sobel_func_8u = 0;
  30.         CvDerivIPPFunc_32f ipp_sobel_func_32f = 0;

  31.         CvDeriv3x3GetBufSizeIPPFunc ipp_scharr_getbufsize_func = 0;
  32.         CvDeriv3x3IPPFunc_8u ipp_scharr_func_8u = 0;
  33.         CvDeriv3x3IPPFunc_32f ipp_scharr_func_32f = 0;

  34.         if( aperture_size == CV_SCHARR )
  35.         {
  36.             if( dx == 1 && dy == 0 )
  37.             {
  38.                 if( src_type == CV_8U )
  39.                     ipp_scharr_func_8u = icvFilterScharrVertBorder_8u16s_C1R_p,
  40.                     ipp_scharr_getbufsize_func = icvFilterScharrVertGetBufSize_8u16s_C1R_p;
  41.                 else
  42.                     ipp_scharr_func_32f = icvFilterScharrVertBorder_32f_C1R_p,
  43.                     ipp_scharr_getbufsize_func = icvFilterScharrVertGetBufSize_32f_C1R_p;
  44.             }
  45.             else if( dx == 0 && dy == 1 )
  46.             {
  47.                 if( src_type == CV_8U )
  48.                     ipp_scharr_func_8u = icvFilterScharrHorizBorder_8u16s_C1R_p,
  49.                     ipp_scharr_getbufsize_func = icvFilterScharrHorizGetBufSize_8u16s_C1R_p;
  50.                 else
  51.                     ipp_scharr_func_32f = icvFilterScharrHorizBorder_32f_C1R_p,
  52.                     ipp_scharr_getbufsize_func = icvFilterScharrHorizGetBufSize_32f_C1R_p;
  53.             }
  54.             else
  55.                 CV_ERROR( CV_StsBadArg, "Scharr filter can only be used to compute 1st image derivatives" );
  56.         }
  57.         else
  58.         {
  59.             if( dx == 1 && dy == 0 )
  60.             {
  61.                 if( src_type == CV_8U )
  62.                     ipp_sobel_func_8u = icvFilterSobelNegVertBorder_8u16s_C1R_p,
  63.                     ipp_sobel_getbufsize_func = icvFilterSobelNegVertGetBufSize_8u16s_C1R_p;
  64.                 else
  65.                     ipp_sobel_func_32f = icvFilterSobelNegVertBorder_32f_C1R_p,
  66.                     ipp_sobel_getbufsize_func = icvFilterSobelNegVertGetBufSize_32f_C1R_p;
  67.             }
  68.             else if( dx == 0 && dy == 1 )
  69.             {
  70.                 if( src_type == CV_8U )
  71.                     ipp_sobel_func_8u = icvFilterSobelHorizBorder_8u16s_C1R_p,
  72.                     ipp_sobel_getbufsize_func = icvFilterSobelHorizGetBufSize_8u16s_C1R_p;
  73.                 else
  74.                     ipp_sobel_func_32f = icvFilterSobelHorizBorder_32f_C1R_p,
  75.                     ipp_sobel_getbufsize_func = icvFilterSobelHorizGetBufSize_32f_C1R_p;
  76.             }
  77.             else if( dx == 2 && dy == 0 )
  78.             {
  79.                 if( src_type == CV_8U )
  80.                     ipp_sobel_func_8u = icvFilterSobelVertSecondBorder_8u16s_C1R_p,
  81.                     ipp_sobel_getbufsize_func = icvFilterSobelVertSecondGetBufSize_8u16s_C1R_p;
  82.                 else
  83.                     ipp_sobel_func_32f = icvFilterSobelVertSecondBorder_32f_C1R_p,
  84.                     ipp_sobel_getbufsize_func = icvFilterSobelVertSecondGetBufSize_32f_C1R_p;
  85.             }
  86.             else if( dx == 0 && dy == 2 )
  87.             {
  88.                 if( src_type == CV_8U )
  89.                     ipp_sobel_func_8u = icvFilterSobelHorizSecondBorder_8u16s_C1R_p,
  90.                     ipp_sobel_getbufsize_func = icvFilterSobelHorizSecondGetBufSize_8u16s_C1R_p;
  91.                 else
  92.                     ipp_sobel_func_32f = icvFilterSobelHorizSecondBorder_32f_C1R_p,
  93.                     ipp_sobel_getbufsize_func = icvFilterSobelHorizSecondGetBufSize_32f_C1R_p;
  94.             }
  95.             else if( dx == 1 && dy == 1 )
  96.             {
  97.                 if( src_type == CV_8U )
  98.                     ipp_sobel_func_8u = icvFilterSobelCrossBorder_8u16s_C1R_p,
  99.                     ipp_sobel_getbufsize_func = icvFilterSobelCrossGetBufSize_8u16s_C1R_p;
  100.                 else
  101.                     ipp_sobel_func_32f = icvFilterSobelCrossBorder_32f_C1R_p,
  102.                     ipp_sobel_getbufsize_func = icvFilterSobelCrossGetBufSize_32f_C1R_p;
  103.             }
  104.         }

  105.         if( (ipp_sobel_func_8u || ipp_sobel_func_32f) && ipp_sobel_getbufsize_func ||
  106.             (ipp_scharr_func_8u || ipp_scharr_func_32f) && ipp_scharr_getbufsize_func )
  107.         {
  108.             int bufsize = 0, masksize = aperture_size == 3 ? 33 : 55;
  109.             CvSize size = cvGetMatSize( src );
  110.             uchar* src_ptr = src->data.ptr;
  111.             uchar* dst_ptr = dst->data.ptr;
  112.             int src_step = src->step ? src->step : CV_STUB_STEP;
  113.             int dst_step = dst->step ? dst->step : CV_STUB_STEP;
  114.             const int bordertype = 1; // replication border
  115.             CvStatus status;

  116.             status = ipp_sobel_getbufsize_func ?
  117.                 ipp_sobel_getbufsize_func( size, masksize, &bufsize ) :
  118.                 ipp_scharr_getbufsize_func( size, &bufsize );

  119.             if( status >= 0 )
  120.             {
  121.                 if( bufsize <= CV_MAX_LOCAL_SIZE )
  122.                 {
  123.                     buffer = cvStackAlloc( bufsize );
  124.                     local_alloc = 1;
  125.                 }
  126.                 else
  127.                     CV_CALL( buffer = cvAlloc( bufsize ));
  128.             
  129.                 status =
  130.                     ipp_sobel_func_8u ? ipp_sobel_func_8u( src_ptr, src_step, dst_ptr, dst_step,
  131.                                                            size, masksize, bordertype, 0, buffer ) :
  132.                     ipp_sobel_func_32f ? ipp_sobel_func_32f( src_ptr, src_step, dst_ptr, dst_step,
  133.                                                              size, masksize, bordertype, 0, buffer ) :
  134.                     ipp_scharr_func_8u ? ipp_scharr_func_8u( src_ptr, src_step, dst_ptr, dst_step,
  135.                                                              size, bordertype, 0, buffer ) :
  136.                     ipp_scharr_func_32f ? ipp_scharr_func_32f( src_ptr, src_step, dst_ptr, dst_step,
  137.                                                                size, bordertype, 0, buffer ) :
  138.                         CV_NOTDEFINED_ERR;
  139.             }

  140.             if( status >= 0 &&
  141.                 (dx == 0 && dy == 1 && origin || dx == 1 && dy == 1 && !origin)) // negate the output
  142.                 cvSubRS( dst, cvScalarAll(0), dst );

  143.             if( status >= 0 )
  144.                 EXIT;
  145.         }
  146.     }

  147.     CV_CALL( filter.init_deriv( src->cols, src_type, dst_type, dx, dy,
  148.                 aperture_size, origin ? CvSepFilter::FLIP_KERNEL : 0));
  149.     CV_CALL( filter.process( src, dst ));

  150.     __END__;

  151.     if( buffer && !local_alloc )
  152.         cvFree( &buffer );
  153. }
其中分析关键部分
  1. if( aperture_size == CV_SCHARR )
  2.         {
  3.             if( dx == 1 && dy == 0 )
  4.             {
  5.                 if( src_type == CV_8U )
  6.                     ipp_scharr_func_8u = icvFilterScharrVertBorder_8u16s_C1R_p,
  7.                     ipp_scharr_getbufsize_func = icvFilterScharrVertGetBufSize_8u16s_C1R_p;
  8.                 else
  9.                     ipp_scharr_func_32f = icvFilterScharrVertBorder_32f_C1R_p,
  10.                     ipp_scharr_getbufsize_func = icvFilterScharrVertGetBufSize_32f_C1R_p;
  11.             }
  12.             else if( dx == 0 && dy == 1 )
  13.             {
  14.                 if( src_type == CV_8U )
  15.                     ipp_scharr_func_8u = icvFilterScharrHorizBorder_8u16s_C1R_p,
  16.                     ipp_scharr_getbufsize_func = icvFilterScharrHorizGetBufSize_8u16s_C1R_p;
  17.                 else
  18.                     ipp_scharr_func_32f = icvFilterScharrHorizBorder_32f_C1R_p,
  19.                     ipp_scharr_getbufsize_func = icvFilterScharrHorizGetBufSize_32f_C1R_p;
  20.             }
  21.             else
  22.                 CV_ERROR( CV_StsBadArg, "Scharr filter can only be used to compute 1st image derivatives" );
  23.         }
其中dx和dy是在x和y这两个方向上求导的阶数。通常为0和1,最多2。0表示这个方向上没有求导。如果调用cvSobel()并且传递CV_SCHARR后,就会进入该if条件。从此处可以看出,该版本opencv只能支持x和y方向上的0或1阶导数。进入该if条件后只对ipp_scharr_func_8u或ipp_scharr_func_32f和ipp_scharr_getbufsize_func这两个变量赋了一个值。真正的计算在后边。
 
  1. if( (ipp_sobel_func_8u || ipp_sobel_func_32f) && ipp_sobel_getbufsize_func ||
  2.             (ipp_scharr_func_8u || ipp_scharr_func_32f) && ipp_scharr_getbufsize_func )
  3.         {
  4.             int bufsize = 0, masksize = aperture_size == 3 ? 33 : 55;
  5.             CvSize size = cvGetMatSize( src );
  6.             uchar* src_ptr = src->data.ptr;
  7.             uchar* dst_ptr = dst->data.ptr;
  8.             int src_step = src->step ? src->step : CV_STUB_STEP;
  9.             int dst_step = dst->step ? dst->step : CV_STUB_STEP;
  10.             const int bordertype = 1; // replication border
  11.             CvStatus status;

  12.             status = ipp_sobel_getbufsize_func ?
  13.                 ipp_sobel_getbufsize_func( size, masksize, &bufsize ) :
  14.                 ipp_scharr_getbufsize_func( size, &bufsize );

  15.             if( status >= 0 )
  16.             {
  17.                 if( bufsize <= CV_MAX_LOCAL_SIZE )
  18.                 {
  19.                     buffer = cvStackAlloc( bufsize );
  20.                     local_alloc = 1;
  21.                 }
  22.                 else
  23.                     CV_CALL( buffer = cvAlloc( bufsize ));
  24.             
  25.                 status =
  26.                     ipp_sobel_func_8u ? ipp_sobel_func_8u( src_ptr, src_step, dst_ptr, dst_step,
  27.                                                            size, masksize, bordertype, 0, buffer ) :
  28.                     ipp_sobel_func_32f ? ipp_sobel_func_32f( src_ptr, src_step, dst_ptr, dst_step,
  29.                                                              size, masksize, bordertype, 0, buffer ) :
  30.                     ipp_scharr_func_8u ? ipp_scharr_func_8u( src_ptr, src_step, dst_ptr, dst_step,
  31.                                                              size, bordertype, 0, buffer ) :
  32.                     ipp_scharr_func_32f ? ipp_scharr_func_32f( src_ptr, src_step, dst_ptr, dst_step,
  33.                                                                size, bordertype, 0, buffer ) :
  34.                         CV_NOTDEFINED_ERR;
  35.             }

  36.             if( status >= 0 &&
  37.                 (dx == 0 && dy == 1 && origin || dx == 1 && dy == 1 && !origin)) // negate the output
  38.                 cvSubRS( dst, cvScalarAll(0), dst );

  39.             if( status >= 0 )
  40.                 EXIT;
  41.         }
由返回status值的ipp_scharr_func_8u或ipp_scharr_func_32f计算。这两个是函数指针变量,拿ipp_scharr_func_8u来讲,他被赋值为icvFilterScharrHorizBorder_8u16s_C1R_p,而icvFilterScharrHorizBorder_8u16s_C1R_p由icvFilterScharrHorizBorder_32f_C1R_t定义。最终通过[cxmisc.h]IPCVAPI_EX()转到[cxmisc.h]IPCVAPI(type,declspec,name,args)调用
extern name##_t name##_p。
------------
未完待续
------------
 
阅读(1856) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~