Learning opencv-mat data operations for normal youth, literary youth, and violent youth. & mat precautions

Source: Internet
Author: User

From: http://blog.csdn.net/yang_xian521/article/details/7161335

The procedure for ordinary youth is usually M. at <float> (I,
J)

Young people generally walk through the M. PTR <float> (
I) [J]

Violent Young People usually use the M. Data Pointer I mentioned in 40th directly.

The experiment code is as follows:

t = (double)getTickCount();Mat img1(1000, 1000, CV_32F);for (int i=0; i<1000; i++){for (int j=0; j<1000; j++){img1.at<float>(i,j) = 3.2f;}}t = (double)getTickCount() - t;printf("in %gms\n", t*1000/getTickFrequency());//***************************************************************t = (double)getTickCount();Mat img2(1000, 1000, CV_32F);for (int i=0; i<1000; i++){for (int j=0; j<1000; j++){img2.ptr<float>(i)[j] = 3.2f;}}t = (double)getTickCount() - t;printf("in %gms\n", t*1000/getTickFrequency());//***************************************************************t = (double)getTickCount();Mat img3(1000, 1000, CV_32F);float* pData = (float*)img3.data;for (int i=0; i<1000; i++){for (int j=0; j<1000; j++){*(pData) = 3.2f;pData++;}}t = (double)getTickCount() - t;printf("in %gms\n", t*1000/getTickFrequency());//***************************************************************t = (double)getTickCount();Mat img4(1000, 1000, CV_32F);for (int i=0; i<1000; i++){for (int j=0; j<1000; j++){((float*)img3.data)[i*1000+j] = 3.2f;}}t = (double)getTickCount() - t;printf("in %gms\n", t*1000/getTickFrequency());

The last two tricks can be regarded as a method for violent youth, because they are all pointer operations, which limit the violent youth to be violent.

The test results in debug and release modes are as follows:

Test Results
  Debug Release
Ordinary youth 139.06 Ms 2.51 Ms
Literary Youth 66.28 Ms 2.50 Ms
Violent youth 1 4.95 Ms 2.28 Ms
Violent Youth 2 5.11 Ms 1.37 Ms

According to the test results, I think Ji Ming is very credible. The operations of ordinary youth are really slow in the debug mode, and the routes he recommends for young people in literature and art are indeed improved. It is worth noting that the last two methods are indeed a relatively young practice, because at operators or PTR operators are actually checked in memory to prevent operations from going out of bounds, directly using the Data Pointer is indeed dangerous. However, the speed is truly eye-catching, So I dare not call such a young man 2B, or a violent young man.

However, in the release version, the speed difference between several methods is not obvious, and they are very common young people. So if you don't care about these methods when releasing the program at the end, we recommend the first two methods. They are all well-written. Let's leave the pointer operations to the experts. Here we are ~~

Supplement: min Ming also recommended two solutions for young artists. I also tested them casually. I first posted the code and then posted the test results:

/********* Enhanced version *******/t = (double) gettickcount (); MAT img5 (1000,100 0, cv_32f ); float * pdata1; For (INT I = 0; I <1000; I ++) {pdata1 = img5.ptr <float> (I); For (Int J = 0; j <1000; j ++) {pdata1 [J] = 3.2f ;}} t = (double) gettickcount ()-T; printf ("in % GMS \ n ", T * 1000/gettickfrequency ();/********* upgrade version *****/t = (double) gettickcount (); MAT img6 (1000,100 0, cv_32f); float * pdata2; size = img6.size (); If (img2.iscontinuous () {size. width = size. width * size. height; size. height = 1;} size. width * = img2.channels (); For (INT I = 0; I <size. height; I ++) {pdata2 = img6.ptr <float> (I); For (Int J = 0; j <size. width; j ++) {pdata2 [J] = saturate_cast <float> (3.2f) ;}} t = (double) gettickcount ()-T; printf ("in % GMS \ n", T * 1000/gettickfrequency ());

Test results:

  Debug Release
Enhanced literary youth 5.74 Ms 2.43 Ms
Literary Youth in simplified versions 40.12 Ms 2.34 Ms

My test results show that these two solutions are just icing on the cake and give you more options for operations. However, I feel that the speed has not increased by an order of magnitude, thanks again for your support for my blog. Later, James said that saturate_cast reduced the speed. I agree that I will not post the test results. However, I checked the information to understand the role of saturate_cast. It can be regarded as a type forced conversion. For example, for saturate_cast <uchar>, it is to convert the data into 8bit 0 ~ The value ranges from 255 to 0. If the value is greater than 255, the value is 255. If it is a floating point data, it turns into the nearest integer of the round, it is still a very useful function. We recommend that you try it as needed.

 

 

Notes:

1. The Mat copy only copies the mat information header, and the Data Pointer also points to the copied data address, without creating a new memory to store the new matrix content. One problem is that data operations on one of the mat will have a catastrophic impact on the other mat pointing to the same piece of data.

2. The format for creating multi-dimensional arrays is as follows:

int sz[3] = {2, 2, 2};Mat L(3, sz, CV_8UC(1), Scalar::all(0));

3. The traditional lplimage format can also be directly converted to the mat format.

IplImage* img = cvLoadImage("greatwave.png", 1);Mat mtx(img); // convert IplImage* -> Mat

 

To convert the mat format of the new version to the old version, you need to call the following:

Mat I;IplImage* pI = &I.operator IplImage();CvMat* mI = &I.operator CvMat();

However, the safer call format is:

 

Ptr<IplImage> piI = &I.operator IplImage();

4. The mat structure is more friendly, and many operations are closer to the MATLAB style.

5. Data structures such as point2f, point3f, and vector can also be used.

6. RNG class can generate random numbers

7. Implement separation of color channels using the split Function

 

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.