Speed comparison of opencv image traversal methods and comparison of opencv Images

Source: Internet
Author: User

Speed comparison of opencv image traversal methods and comparison of opencv Images

<Pre name = "code" class = "cpp"> <span style = "background-color: rgb (255,255,255); font-family: Arial, Helvetica, sans-serif; font-size: 18px; "> in the field of image processing, we often need to traverse an image. opencv provides multiple methods to traverse the image, but their efficiency is different. In the program, we usually use the pointer or iterator method to traverse the image. The following program will compare the efficiency of the two methods. </Span>

Condition: for a single line, the main frequency is 3.4ghz. the image image.jpg is 768x576.


 
// ReadImage. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <opencv2/core. hpp> # include <opencv2/highgui. hpp> # include <iostream> using namespace cv; using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {Mat image = imread ("image.jpg ", 0); Mat gray1 = Mat: zeros (image. rows, image. cols, image. type (); Mat gray2 = Mat: zeros (image. rows, image. cols, image. type (); long int count = 0; uchar num = 0; double t = (double) getTickCount (); for (int I = 0; I <image. rows; I ++) {uchar * ptr = image. ptr <uchar> (I); uchar * g_ptr = gray1.ptr <uchar> (I); for (int j = 0; j <image. cols; j ++) {g_ptr [j] = ptr [j] ;}} t = (double) getTickCount ()-t)/getTickFrequency (); double t1 = (double) getTickCount (); for (int I = 0; I <image. rows; I ++) {for (int j = 0; j <image. cols; j ++) {gray1.at <uchar> (I, j) = image. at <uchar> (I, j) ;}} t1 = (double) getTickCount ()-t1)/getTickFrequency (); double t2 = (double) getTickCount (); for (int I = 0; I <image. rows; I ++) {uchar * ptr = image. ptr <uchar> (I); for (int j = 0; j <image. cols; j ++) {num = ptr [j] ;}} t2 = (double) getTickCount ()-t2)/getTickFrequency (); double t3 = (double) getTickCount (); for (int I = 0; I <image. rows; I ++) {for (int j = 0; j <image. cols; j ++) {num = image. at <uchar> (I, j) ;}} t3 = (double) getTickCount ()-t3)/getTickFrequency (); cout <endl; cout <t1 <endl; cout <t2 <endl; cout <t3 <endl; getchar (); return 0 ;}




We can see that for image traversal of this size, the pointer method is 30 times faster than the method using the iterator, and it takes only 1 millisecond to use the iterator, instead, it takes about 30 ms to use the iterator.

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.