5. Common function Learning in OPENCV

Source: Internet
Author: User

First, preface

After two weeks of effort, while learning, while writing code, the initial completion of the system interface and some basic functions, including: 1 data read and write and display, and related basic operations (amplification, reduction and movement), 2 sample Data selection, 3 data normalization processing, 4 plotting spectral curve; 5 getting band information. The next work is to complete the remote sensing image classification of the relevant algorithm. This part is mainly mathematical calculation, especially the correlation operation and operation of Matrix. To this end, the system of learning and understand the OPENCV library commonly used matrix operation function, recorded, convenient for later review.

Second, OpenCV function

1 reshape

1 C + +: Mat mat::reshape (intint rows=0Const

Official explanation: The ability to change the channel and form of a two-dimensional array and not copy the data (just create the information header of the matrix).

Parameters: Int cn: Number of channels after transformation (0 means the number of original channels is maintained);

int rows: The number of rows is changed (0 means the number of lines is unchanged), and the number of columns is automatically determined by more than two parameters.

For remote sensing imagery, we often use the channel data (band data) as a feature for subsequent data processing, so we would like to change the number of channels to rows or columns, so we can do this in the following ways:

1 intMainintargcChar*argv[])2 {3 qcoreapplication A (argc, argv);4 5Cv::mat img = Cv::imread ("f:\\papersystem\\opencv\\2.tif");6 Cv::mat TMPB;7TMPB = Img.reshape (1, Img.rows*img.cols);//Change the number of channels to the number of columns and each row can represent each sample data8Qdebug () <//Number of channels9Qdebug () < (0,0)[0]<<tmpb.at<uchar> (0,0);TenQdebug () < (0,0)[1]<<tmpb.at<uchar> (0,1); OneQdebug () < (0,0)[2]<<tmpb.at<uchar> (0,2); A     returna.exec (); -}

Shown below:

2 norm

1C++:DoubleNorm (Inputarray Src1,intNORMTYPE=NORM_L2, Inputarray mask=Noarray ())//Calculate the norm of Matrix src1, mainly including 1,2,inf norm norm_l1/Norm_l2/norm_inf
2C + +:DoubleNorm (Inputarray src1, Inputarray src2,intnormtype=Norm_l2, Inputarraymask=Noarray ())//The 1, 2, INF norm of the Compute matrix (SRC1-SRC2)Norm_l1/norm_l2/norm_inf (For normtype = Norm_relative_inf/norm_relative_l1/norm_relative_l2, The quotient of the norm of the computed (SRC1-SRC2) and the corresponding norm of the SRC1
3C++:DoubleNormConstsparsemat& SRC,intNormtype)

Parameters: We all know

1 intMainintargcChar*argv[])2 {3 qcoreapplication A (argc, argv);4 5Cv::mat img = Cv::imread ("f:\\papersystem\\opencv\\2.tif");6Std::vector<cv::mat>splitimg (Img.channels ());7 Cv::split (img,splitimg);8 9Cv::mat tmp = Img.reshape (1, img.rows*img.cols);Ten  One     Doublenorm1_splitimg = Cv::norm (splitimg[0],cv::norm_l1); A     Doublenorm2_splitimg = Cv::norm (splitimg[0],cv::norm_l2); -     Doublenorminf_splitimg = Cv::norm (splitimg[0],cv::norm_inf); -  the     DoubleNorm1_img =Cv::norm (IMG,CV::NORM_L1); -     DoubleNorm2_img =Cv::norm (IMG,CV::NORM_L2); -     DoubleNorminf_img =Cv::norm (img,cv::norm_inf); -  +     DoubleNorm1_tmp =Cv::norm (TMP,CV::NORM_L1); -     DoubleNorm2_tmp =Cv::norm (TMP,CV::NORM_L2); +     DoubleNorminf_tmp =Cv::norm (tmp,cv::norm_inf); A  atQdebug () <<norm1_splitImg<<"  "<<norm1_img<<"  "<<norm1_tmp; -Qdebug () <<norm2_splitImg<<"  "<<norm2_img<<"  "<<norm2_tmp; -Qdebug () <<normInf_splitImg<<"  "<<normInf_img<<"  "<<norminf_tmp; -     returna.exec (); -}

Shown below: For multi-channel data is converted into a single-channel post-processing, all channel data are added to the calculation.

3 normalize

1 void Double alpha=1double beta=0int dtype=-1, Inputarray mask=Noarray ())2void normalize (constdoubleint Normtype)

Parameter: Inputarray src: input matrix;

Outputarray DST: Output matrix;

Double alpha: Rules The Matrix to Aipha, or the minimum value of the matrix after the rule;

Double beta: The upper bound of the matrix element's rule, and when the preceding alpha is to a value, it is not used

。。。。。。。

Cv::normalize (splitimg[0],tmp,0,255, Cv::norm_minmax);  //About this function I do not quite understand, the matrix element normalized to a range can, but why the matrix of a certain norm normalized to a value but never

Please help me if you know.

4 reduce

1 void int int int dtype=-1 )

This function is very powerful, can achieve the matrix of the column summation, mean, maximum, minimum.

Parameters: int dim:0---Line

int rtype:cv_reduce_sum Cv_reduce_avg Cv_reduce_max cv_reduce_min

int DTYPR: Defaults to the type of the original data, Note: For summation and mean to change the data type, especially the sum, otherwise the data size exceeds the original data type will be error

5 Repeat

1 void int int NX, Outputarray DST) 2 C + +: Mat repeat (constintint NX)

This code is the same as the repmat in MATLAB

Summary: OpenCV almost has a similar matrix operation function in MATLAB, it seems that the next MATLAB code into the OPENCCV format is not so difficult haha ...

5. Common function Learning in 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.