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.