Experience the Masters' code style _ opencv source code structure analysis 1

Source: Internet
Author: User
Tags scalar

Recently, when calling opencv, I always look at the original code of opencv. In those complicated macro definitions, I still feel very interesting.

Cvget2d (const cvarr * arr, int y, int X); // The first coordinate is Y, and the Second coordinate is X.

 

Cv_impl cvscalar
Cvget2d (const cvarr * arr, int y, int X) // cv_impl macro defines extern "C"
{
Cvscalar scalar = {0, 0, 0 }};

Cv_funcname ("cvget2d"); // The cv_funcname here is to pass a function name to the Global static variable cvfuncname of the system, and then this variable will be used in cverror to output the function error.

_ Begin __; // here is {

Int type = 0;
Uchar * PTR;

If (cv_is_mat (ARR) // common matrix Array
{
Cvmat * MAT = (cvmat *) Arr;

If (unsigned) y >=( unsigned) (mat-> rows) |
(Unsigned) x> = (unsigned) (mat-> Cols ))
Cv_error (cv_stsoutofrange, "index is out of range ");

Type = cv_mat_type (mat-> type );
PTR = mat-> data. PTR + (size_t) y * mat-> step + x * icvpixsize [type];
}
Else if (! Cv_is_sparse_mat (ARR) // Image
PTR = cvptr2d (ARR, Y, X, & type );
Else // sparse matrix Array
{
Int idx [] = {Y, x };
PTR = icvgetnodeptr (cvsparsemat *) Arr, idx, & type, 0, 0 );
}

Cvrawdatatoscalar (PTR, type, & scalar );

_ End __; // here is # DEFINE _ end _ goto exit; exit :;}. an empty statement is actually executed. I don't understand what this macro means in every function.

Return scalar;
}

///

There are many macro definitions in opencv. ippi_call generally makes a judgment on the function pointer when calling a function.

 

When looking at the original file cvtemplmatch. cpp, we found some initial functions defined by the function.

// Define the pointer type of a function first. the following statement calls Intel's internal functions. The original Code cannot be found in opencv. I did not find it, but I don't know if anyone can find it.

/**************************** IPP match template functions ****** ************************/

Icvcross1_valid_norm_8u32f_c1r_t icvcross1_valid_norm_8u32f_c1r_p = 0;
Icvcross1_valid_normlevel_8u32f_c1r_t icvcross1_valid_normlevel_8u32f_c1r_p = 0;
Icvsqrdistancevalid_norm_8u32f_c1r_t icvsqrdistancevalid_norm_8u32f_c1r_p = 0;
Icvcross1_valid_norm_32f_c1r_t icvcross1_valid_norm_32f_c1r_p = 0;
Icvcross1_valid_normlevel_32f_c1r_t icvcross1_valid_normlevel_32f_c1r_p = 0;
Icvsqrdistancevalid_norm_32f_c1r_t icvsqrdistancevalid_norm_32f_c1r_p = 0;

Typedef cvstatus (cv_stdcall * cvtemplmatchippfunc)
(Const void * IMG, int imgstep, cvsize imgsize,
Const void * Templ, int templstep, cvsize templsize,
Void * result, int rstep );

/*************************************** **************************************** **********/

 

Define some function pointers, but these functions cannot be found

 

In the cvmatchtemplate function, select different functions based on different template matching methods.

Cvtemplmatchippfunc ipp_func =
Depth = cv_8u?
(Method = cv_tm_sqdiff_normed? (Cvtemplmatchippfunc) icvsqrdistancevalid_norm_8u32f_c1r_p:
Method = cv_tm_c1__normed? (Cvtemplmatchippfunc) icvcross1_valid_norm_8u32f_c1r_p:
(Cvtemplmatchippfunc) icvcross1_valid_normlevel_8u32f_c1r_p ):
(Method = cv_tm_sqdiff_normed? (Cvtemplmatchippfunc) icvsqrdistancevalid_norm_32f_c1r_p:
Method = cv_tm_c1__normed? (Cvtemplmatchippfunc) icvcross1_valid_norm_32f_c1r_p:
(Cvtemplmatchippfunc) icvcross1_valid_normlevel_32f_c1r_p );

There is a function body in opencv, but the function name is dynamically changed. This policy can be applied to code encryption.

# Define icv_decl_cross1__direct (flavor, arrtype, corrtype, worktype )/

Static cvstatus cv_stdcall/
Icvcross‑direct _ ## flavor # _ CNR (const arrtype * img0, int imgstep ,/
Cvsize imgsize, const arrtype * templ0, int templstep, cvsize templsize ,/
Corrtype * corr, int sequence step, cvsize sequence size, int CN )/
{

// Function body

}

The following function declaration

Icv_decl_cross1__direct (8u32f, uchar, float, INT)
Icv_decl_cross1__direct (32f, float, float, double)
Icv_decl_cross1__direct (64f, Double, double, double)

Typedef cvstatus (cv_stdcall * cvcross‑directfunc )(
Const void * IMG, int imgstep, cvsize imgsize,
Const void * Templ, int templstep, cvsize templsize,
Void * corr, int skip step, cvsize skip size, int CN );

The following is the call method.

Cvcross‑directfunc ‑_func = 0;
If (depth = cv_8u & pai_type = cv_32f)
Required _func = (cvcross1_directfunc) icvcross1_direct_8u32f_cnr;
Else if (depth = cv_32f & corr_type = cv_32f)
Required _func = (cvcross1_directfunc) icvcross1_direct_32f_cnr;
Else if (depth = cv_64f & corr_type = cv_64f)
Required _func = (cvcross1_directfunc) icvcross1_direct_64f_cnr;
Else
Cv_error (cv_stsunsupportedformat,
"Unsupported combination of input and output formats ");

Ippi_call (effec_func (IMG-> data. PTR, IMG-> step, imgsize,
Templ-> data. PTR, Templ-> step, templsize,
Corr-> data. PTR, corr-> step, shard size, CN ));

It's really interesting.

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.