Opencv development note 6: pixel reading and writing (2) comparison of reading and writing efficiency

Keywords: OpenCV

1, How to calculate efficiency

To calculate the efficiency of a program, you need to get the time it takes for the program to run to its current location

Two simple timing functions getTickCount() and getTickFrequency() are provided in opencv

    double t = (double)getTickCount();
    // do something ...
    t = ((double)getTickCount() - t)/getTickFrequency();

Use as above

2, Code test

Code implementation

Create a 500 * 500 3-channel image, write random number test program to 3-channel continuously, and then test efficiency under the same conditions

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;
RNG rng(1234);

void p_RW_Pixel(Mat &src){
	for (int row = 0; row < src.rows; row++){
		Vec3b * cur = src.ptr<Vec3b>(row);  // Get a pointer to the current row
		for (int col = 0; col < src.cols; col++){
			if (src.channels() == 3){
				cur[col][0] = rng.uniform(0, 255) % 255; // b assigned random color
				cur[col][1] = rng.uniform(0, 255) % 255;
				cur[col][2] = rng.uniform(0, 255) % 255;
			}
		}
	}
}

void iter_RW_Pixel(Mat &src){
	MatIterator_<Vec3b> iter;
	for (iter = src.begin<Vec3b>(); iter != src.end<Vec3b>(); iter++){
		(*iter)[0] = rng.uniform(0, 255) % 255;
		(*iter)[1] = rng.uniform(0, 255) % 255;
		(*iter)[2] = rng.uniform(0, 255) % 255;

	}
}

void at_RW_Pixel(Mat &src){
	for (int row = 0; row < src.rows; row++){
		for (int col = 0; col < src.cols; col++){
			if (src.channels() == 3){
				src.at<Vec3b>(row, col)[0] = rng.uniform(0, 255) % 255;
				src.at<Vec3b>(row, col)[1] = rng.uniform(0, 255) % 255;
				src.at<Vec3b>(row, col)[2] = rng.uniform(0, 255) % 255;
			
			}
		}
	}
}

int main01(int argc, char **argv){

	Mat src = Mat::zeros(500, 500, CV_8UC3);

	double t_start = static_cast<double>(getTickCount());  // Program start time
	// The middle time is the time to test the program
	p_RW_Pixel(src);
	//iter_RW_Pixel(src);
	//at_RW_Pixel(src);
	double t_end = static_cast<double>(getTickCount());// End time of program

	double timeConsume = (t_end - t_start) / getTickFrequency();
	printf("time consume: %.5f ms\n", timeConsume*1000);


	imshow("src", src);																						
	waitKey(0);
	return 0;
}

int main(int argc, char **argv){

	Mat src = Mat::zeros(500, 500, CV_8UC3); // Create a 500 * 500 3-channel image 
	double t_start, timeConsume;

	// The efficiency of calculating at function
	t_start = static_cast<double>(getTickCount());  // Program start time
	// The middle time is the time to test the program
	at_RW_Pixel(src);
	timeConsume = ((double)getTickCount() - t_start) / getTickFrequency();
	printf("at time consume: %.5f ms\n", timeConsume * 1000);

	// Calculate the efficiency of the pointer
	t_start = static_cast<double>(getTickCount());  // Program start time
	// The middle time is the time to test the program
	p_RW_Pixel(src);
	timeConsume = ((double)getTickCount() - t_start) / getTickFrequency();
	printf("p time consume: %.5f ms\n", timeConsume * 1000);

	// Calculate the efficiency of iterator
	t_start = static_cast<double>(getTickCount());  // Program start time
	// The middle time is the time to test the program
	iter_RW_Pixel(src);
	timeConsume = ((double)getTickCount() - t_start) / getTickFrequency();
	printf("iterator time consume: %.5f ms\n", timeConsume * 1000);

	imshow("src", src);
	waitKey(0);
	return 0;
}

 

Posted by lmaster on Sat, 21 Dec 2019 12:36:25 -0800