1st days of learning opencv

Source: Internet
Author: User
# If (defined Win32 | defined win64) & defined cvapi_exports # define cv_exports _ declspec (dllexport) # else # define cv_exports # endif # ifndef cvapi # define cvapi (rettype) cv_extern_c cv_exports rettype cv_cdecl # endif/* cv_funcname macro defines icvfuncname constant which is used by cv_error macro */# ifdef limit # define cv_funcname (name) # define cvfuncname "" # else # define cv_funcname (Nam E) Static char cvfuncname [] = Name # endif/* cv_call macro callcv (or IPL) function, checks error status and signals a error if the function failed. useful in "parent node" error procesing mode */# define cv_call (func) {func; cv_check () ;}/ * simplified form of cv_error */# define cv_error_from_code (CODE) cv_error (Code, "")/* cv_check macro checks error status after CV (or IPL) function call. If error detected, control will be transferred to the exit label. */# define cv_check () {If (cvgeterrstatus () <0) cv_error (cv_stsbacktrace, "inner function failed. ") ;}# DEFINE _ begin _ {# DEFINE _ end _ goto exit; exit :;#define _ cleanup __# define exit goto exit /*. cv_blur_no_scale (simple fuzzy without scale transformation)-sums the param1 × param2 fields of each pixel. If the neighborhood size changes, you can use the cvintegral function to calculate the integral image .. Cv_blur (simple blur)-sums the param1 × param2 neighbor of each pixel and performs scale transformation 1/(param1.param2 ).. cv_gaussian (Gaussian Blur)-perform Gaussian convolution with a kernel size of param1 × param2. cv_median (median blur)-perform a median filter (I. e. the neighborhood is square ).. cv_bilateral (bidirectional filtering)-apply bidirectional 3x3 filtering, color Sigma = param1, spatial Sigma = param2. the first parameter of the smooth operation. */cv_impl voidcvsmooth (const void * srcarr, void * dstarr, int smooth_type, int param1, int param2, double param3, doubl E param4) {cvboxfilter box_filter; // defines the cvsepfilter gaussian_filter object of a cvboxfilter; // defines the cvmat * temp = 0 object of a cvsepfilter; // The temp pointer is empty cv_funcname ("cvsmooth");/* cv_funcname macro defines icvfuncname constant which is used by cv_error macro */_ begin __; // This is useless, it is equivalent to telling you that I want to start working. Int coi1 = 0, coi2 = 0; // COI channel of interest, similar to ROI cvmat srcstub, * src = (cvmat *) srcarr; // void pointer for type conversion cvmat dststub, * DST = (cvmat *) DS Tarr; // void pointer type conversion 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 )); /* cv_call macro callcv (or IPL) function, checks error status and signals a error if the function is failed. useful in "parent node" error procesing mode */If (coi1! = 0 | coi2! = 0) // COI must all be 0, which is equivalent to coi1 = 0 & coi2 = 0 cv_error (cv_badcoi, ""); src_type = cv_mat_type (SRC-> type ); // # define cv_mat_type (flags) & cv_mat_type_mask) // # define cv_mat_type_mask (cv_depth_max * cv_cn_max-1) the result is: 01 1111 1111 // # define cv_cn_max 64 // # define cv_cn_shift 3 // # define cv_depth_max (1 <cv_cn_shift) // dst_type = cv_mat_type (DST-> type ); depth = cv_mat_depth (src_type); // # define cv_mat _ Depth (flags) & cv_mat_depth_mask) // # define cv_mat_depth_mask (cv_depth_max-1) the result is: 00 0000 0111 // # define cv_cn_shift 3 // # define cv_depth_max (1 <cv_cn_shift) Cn = cv_mat_cn (src_type); // # define cv_mat_cn (flags) (flags) & cv_mat_cn_mask)> cv_cn_shift) + 1) // # define cv_mat_cn_mask (cv_cn_max-1) <cv_cn_shift) the result is: 11 1111 1000 // # define cv_cn_max 64 // # define cv_cn_shift 3 size = Cvgetmatsize (SRC); // cvgetsize (): If (! Cv_are_sizes_eq (SRC, DST) // judge whether the input and output sizes are equal cv_error (cv_stsunmatchedsizes, ""); If (smooth_type! = Cv_blur_no_scale &&! Cv_are_types_eq (SRC, DST) // except for cv_blur_no_scale, the input/output matrix type should be the same as cv_error (cv_stsunmatchedformats, "The specified smoothing algorithm requires input and ouput arrays be of the same type");/* second parameter of the param2 smoothing operation. if the value of param2 is zero, it indicates that it is set to param1. Param3 corresponds to the Gaussian sigma (standard deviation) of Gaussian parameters ). if it is zero, the standard deviation is calculated by the following kernel size: Sigma = (n/2-1) * 0.3 + 0.8, where n = param1 corresponds to the horizontal kernel, N = param2 corresponds to the vertical core. */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; // sigma1, sigma2 is param3 and param4. If param4 is 0, sigma2 is the value of 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; // when P2 is 0, use sigma1 * (depth = cv_8u? 3: 4) * 2 + 1 calculate the window size (ODD)} If (param2 = 0) param2 = size. Height = 1? 1: param1; If (param1 <1 | (param1 & 1) = 0 | param2 <1 | (param2 & 1) = 0) // param1, when param2 is an even number or a negative number, cv_error (cv_stsoutofrange, "both mask width and height must be> = 1 and odd") is returned; If (param1 = 1 & param2 = 1) // param1, param2 is 1, and the image remains unchanged {cvconvert (SRC, DST); exit ;}} // ================================================ ========================================================== ================================================================/// use IP P acceleration: If (have_ipp & (smooth_type = cv_blur | smooth_type = cv_median) & size. width> = param1 & size. height >=param2 & param1> 1 & param2> 1) {cvsmoothfixedippfunc Limit = 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; // This method is awesome...} 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? Condition: 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); Shi Fted_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 = Hangzhou) {cv_call (values (SRC-> cols, src_type, dst_type, smooth_type = cv_blur, cvsize (param1, param2 ))); // initialize box_filter cv_call (box_filter.process (SRC, DST);} else if (smooth_type = cv_median) {If (depth! = Cv_8u | CN! = 1 & CN! = 3 & CN! = 4) // The number of median filtering channels must be 1, 3, 4, and the depth must be cv_8u cv_error (cv_stsunsupportedformat, "median filter only supports 8uc1, 8uc3 and 8uc4 images "); // The Section 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 }; // initial core size float * kx = (float *) cvstackalloc (ksize. width * sizeof (kx [0]); // stack allocation. The float type is float * Ky = (float *) cvstackalloc (ksize. height * sizeof (KY [0]); // stack allocation. The float type is Y axis core width cvmat kx = cvmat (1, ksize. width, cv_32f, kx); // The float type is X-axis kernel width, and the vector cvmat Ky = cvmat (1, ksize. height, cv_32f, KY); // Float Type: X axis kernel width, vector cvsepfilter: init_gaussian_kernel (& kx, sigma1); If (ksize. width! = Ksize. height | FABS (sigma1-sigma2)> flt_epsilon) cvsepfilter: Upper (& Ky, sigma2); // # define flt_epsilon upper-07f/* smallest such that 1.0 + flt_epsilon! = 1.0 * // ========================================== ========================================================== ========================================/// flt_epsilon is used for the float type. // It satisfies the minimum positive number of X + 1.0 not equal to 1.0 // That is to say, all positive numbers x, x + 1.0 = 1.0 smaller than flt_epsilon are true. // ================================================ ========================================================== ========================== Else Ky. data. FL = kx; // ================================================ ========================================================== ==============================================================/// use IPP for acceleration hour: if (have_ipp & size. width> = param1 * 3 & size. height> = param2 & param1> 1 & param2> 1) {int done; cv_call (done = icvip?pfilter (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) // param1, param2 detection legality cv_e Rror (cv_stsoutofrange, "thresholds in bilaral filtering shocould not bee negative"); // If param1 or param2 is 0, 1 param1 + = param1 = 0; param2 + = param2 = 0; // check if (depth! = Cv_8u | CN! = 1 & CN! = 3) cv_error (cv_stsunsupportedformat, "bilateral filter only supports 8uc1 and 8uc3 images"); // The ippi_call (callback (SRC-> data. PTR, Src-> step, DST-> data. PTR, DST-> step, size, param1, param2, CN) ;}_ _ end __; // release temp cvreleasemat (& temp );}

1st days of learning opencv

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.