自2011年接触OpenCV已经有几年了,一直停留在写一些小程序,利用手冊完毕一些任务,一直没有深入研究当中代码,现在毕业,但各种原因未能进入图像处理行业,故现重学OpenCV,包含分析代码,学习算法,blog不定时更写,欢迎交流。
搭建好开发环境:VS2010+CV1.0
最新CV是3.0 alpha,但1.0结构简单,基础功能还是有的,并且本人比較熟悉C语言,故选择1.0。
官网下载1.0的安装文件,系统自己主动安装到C:Program Files (x86),进入OpenCV文件夹,会发现有一个C:Program Files (x86)OpenCV\_make文件夹,里面有一个OpenCV.sln文件,这个非常好辨认,用vs2010打开文件,由于是2008的project文件,所以须要转换,还好没有报错,直接生成project,使用Debug版本号,由于原版是Release的,无法单步调试。
生成项目后,会在对应文件夹下看到文件名称为d结尾的lib和dll,依据Vs配置OpenCV的原理(网上有非常多,不再赘述),至此环境完毕,仅仅要新建项目,单步调试,就能一步步的观察函数的内部结构。
#include <cv.h> #include <highgui.h> int main() { IplImage * image = cvLoadImage("e:\1.jpg",1); cvNamedWindow("原图"); cvNamedWindow("变换"); cvShowImage("原图",image); cvSmooth(image,image,CV_GAUSSIAN,3,3,0,0); cvShowImage("变换",image); cvWaitKey(); cvDestroyWindow("原图"); cvDestroyWindow("变换"); cvReleaseImage(&image); return 0; }调试的话进入cvSmooth:
CV_IMPL void cvSmooth( const void* srcarr, void* dstarr, int smooth_type, int param1, int param2, double param3, double param4 ) { CvBoxFilter box_filter; CvSepFilter gaussian_filter; CvMat* temp = 0; CV_FUNCNAME( "cvSmooth" ); __BEGIN__; int coi1 = 0, coi2 = 0; CvMat srcstub, *src = (CvMat*)srcarr; CvMat dststub, *dst = (CvMat*)dstarr; CvSize size; int src_type, dst_type, depth, cn; double sigma1 = 0, sigma2 = 0; bool have_ipp = icvFilterMedian_8u_C1R_p != 0; CV_CALL( src = cvGetMat( src, &srcstub, &coi1 )); CV_CALL( dst = cvGetMat( dst, &dststub, &coi2 )); if( coi1 != 0 || coi2 != 0 ) CV_ERROR( CV_BadCOI, "" ); src_type = CV_MAT_TYPE( src->type ); dst_type = CV_MAT_TYPE( dst->type ); depth = CV_MAT_DEPTH(src_type); cn = CV_MAT_CN(src_type); size = cvGetMatSize(src); if( !CV_ARE_SIZES_EQ( src, dst )) CV_ERROR( CV_StsUnmatchedSizes, "" ); if( smooth_type != CV_BLUR_NO_SCALE && !CV_ARE_TYPES_EQ( src, dst )) CV_ERROR( CV_StsUnmatchedFormats, "The specified smoothing algorithm requires input and ouput arrays be of the same type" ); if( smooth_type == CV_BLUR || smooth_type == CV_BLUR_NO_SCALE || smooth_type == CV_GAUSSIAN || smooth_type == CV_MEDIAN ) { // automatic detection of kernel size from sigma if( smooth_type == CV_GAUSSIAN ) { sigma1 = param3; sigma2 = param4 ? param4 : param3; if( param1 == 0 && sigma1 > 0 ) param1 = cvRound(sigma1*(depth == CV_8U ? 3 : 4)*2 + 1)|1; if( param2 == 0 && sigma2 > 0 ) param2 = cvRound(sigma2*(depth == CV_8U ? 3 : 4)*2 + 1)|1; } if( param2 == 0 ) param2 = size.height == 1 ? 1 : param1; if( param1 < 1 || (param1 & 1) == 0 || param2 < 1 || (param2 & 1) == 0 ) CV_ERROR( CV_StsOutOfRange, "Both mask width and height must be >=1 and odd" ); if( param1 == 1 && param2 == 1 ) { cvConvert( src, dst ); EXIT; } } if( have_ipp && (smooth_type == CV_BLUR || smooth_type == CV_MEDIAN) && size.width >= param1 && size.height >= param2 && param1 > 1 && param2 > 1 ) { CvSmoothFixedIPPFunc ipp_median_box_func = 0; if( smooth_type == CV_BLUR ) { ipp_median_box_func = src_type == CV_8UC1 ? icvFilterBox_8u_C1R_p : src_type == CV_8UC3 ? icvFilterBox_8u_C3R_p : src_type == CV_8UC4 ? icvFilterBox_8u_C4R_p : src_type == CV_32FC1 ? icvFilterBox_32f_C1R_p : src_type == CV_32FC3 ? icvFilterBox_32f_C3R_p : src_type == CV_32FC4 ? icvFilterBox_32f_C4R_p : 0; } else if( smooth_type == CV_MEDIAN ) { ipp_median_box_func = src_type == CV_8UC1 ? icvFilterMedian_8u_C1R_p : src_type == CV_8UC3 ? icvFilterMedian_8u_C3R_p : src_type == CV_8UC4 ? icvFilterMedian_8u_C4R_p : 0; } if( ipp_median_box_func ) { CvSize el_size = { param1, param2 }; CvPoint el_anchor = { param1/2, param2/2 }; int stripe_size = 1 << 14; // the optimal value may depend on CPU cache, // overhead of the current IPP code etc. const uchar* shifted_ptr; int y, dy = 0; int temp_step, dst_step = dst->step; CV_CALL( temp = icvIPPFilterInit( src, stripe_size, el_size )); shifted_ptr = temp->data.ptr + el_anchor.y*temp->step + el_anchor.x*CV_ELEM_SIZE(src_type); temp_step = temp->step ? temp->step : CV_STUB_STEP; for( y = 0; y < src->rows; y += dy ) { dy = icvIPPFilterNextStripe( src, temp, y, el_size, el_anchor ); IPPI_CALL( ipp_median_box_func( shifted_ptr, temp_step, dst->data.ptr + y*dst_step, dst_step, cvSize(src->cols, dy), el_size, el_anchor )); } EXIT; } } if( smooth_type == CV_BLUR || smooth_type == CV_BLUR_NO_SCALE ) { CV_CALL( box_filter.init( src->cols, src_type, dst_type, smooth_type == CV_BLUR, cvSize(param1, param2) )); CV_CALL( box_filter.process( src, dst )); } else if( smooth_type == CV_MEDIAN ) { if( depth != CV_8U || cn != 1 && cn != 3 && cn != 4 ) CV_ERROR( CV_StsUnsupportedFormat, "Median filter only supports 8uC1, 8uC3 and 8uC4 images" ); IPPI_CALL( icvMedianBlur_8u_CnR( src->data.ptr, src->step, dst->data.ptr, dst->step, size, param1, cn )); } else if( smooth_type == CV_GAUSSIAN ) { CvSize ksize = { param1, param2 }; float* kx = (float*)cvStackAlloc( ksize.width*sizeof(kx[0]) ); float* ky = (float*)cvStackAlloc( ksize.height*sizeof(ky[0]) ); CvMat KX = cvMat( 1, ksize.width, CV_32F, kx ); CvMat KY = cvMat( 1, ksize.height, CV_32F, ky ); CvSepFilter::init_gaussian_kernel( &KX, sigma1 ); if( ksize.width != ksize.height || fabs(sigma1 - sigma2) > FLT_EPSILON ) CvSepFilter::init_gaussian_kernel( &KY, sigma2 ); else KY.data.fl = kx; if( have_ipp && size.width >= param1*3 && size.height >= param2 && param1 > 1 && param2 > 1 ) { int done; CV_CALL( done = icvIPPSepFilter( src, dst, &KX, &KY, cvPoint(ksize.width/2,ksize.height/2))); if( done ) EXIT; } CV_CALL( gaussian_filter.init( src->cols, src_type, dst_type, &KX, &KY )); CV_CALL( gaussian_filter.process( src, dst )); } else if( smooth_type == CV_BILATERAL ) { if( param1 < 0 || param2 < 0 ) CV_ERROR( CV_StsOutOfRange, "Thresholds in bilaral filtering should not bee negative" ); param1 += param1 == 0; param2 += param2 == 0; if( depth != CV_8U || cn != 1 && cn != 3 ) CV_ERROR( CV_StsUnsupportedFormat, "Bilateral filter only supports 8uC1 and 8uC3 images" ); IPPI_CALL( icvBilateralFiltering_8u_CnR( src->data.ptr, src->step, dst->data.ptr, dst->step, size, param1, param2, cn )); } __END__; cvReleaseMat( &temp ); }