patternjavaMinor
OpenCV Mat processing time
Viewed 0 times
processingmattimeopencv
Problem
I'd like to know whether having different variables for the src (source) and dst (destination) of an OpenCV function will have an effect on the processing time. I have two functions below
that does the same thing.
VERSUS
Which of the two is considered best-practice? What is the advantage of one over the other?
that does the same thing.
public static Mat getY(Mat m){
Mat mMattemp = new Mat();
Imgproc.cvtColor(m,mMattemp,Imgproc.COLOR_YUV420sp2RGB);
Imgproc.cvtColor(mMattemp,mMattemp, Imgproc.COLOR_RGB2HSV);
Core.inRange(mMattemp, new Scalar(20, 100, 100), new Scalar(30, 255, 255), mMattemp);
return mMattemp;
}VERSUS
public static Mat getY(Mat m){
Mat mMattemp_rgb = new Mat();
Mat mMattemp_hsv = new Mat();
Mat mMattemp_ir = new Mat();
Imgproc.cvtColor(m,mMattemp_rgb,Imgproc.COLOR_YUV420sp2RGB);
Imgproc.cvtColor(mMattemp_rgb,mMattemp_hsv, Imgproc.COLOR_RGB2HSV);
Core.inRange(mMattemp_hsv, new Scalar(20, 100, 100), new Scalar(30, 255, 255), mMattemp_ir);
return mMattemp_ir;
}Which of the two is considered best-practice? What is the advantage of one over the other?
Solution
Just looking over the two functions, I would probably say the first function is a better practice. It's shorter (meaning it's more maintainable), it uses fewer variables (which can cause less confusion), and does the same thing as the second function. Overall the advantage is that the first function is just shorter.
In terms of speed/efficiency (if I had to guess), it looks like the first function would also be a bit faster as well (though not by very much at all). To know for sure which one is faster, test the function calls with OpenCV's methods
Now you want to know how many seconds are these clock cycles. For that, you want to know how many seconds a single clock takes, i.e.
For that, OpenCV gives second function,
Now you have
In terms of speed/efficiency (if I had to guess), it looks like the first function would also be a bit faster as well (though not by very much at all). To know for sure which one is faster, test the function calls with OpenCV's methods
double getTickCount() and double getTickFrequency().getTickCount gives you the number of clock cycles after a certain event, e.g., after machine is switched on. A = getTickCount() // number of clock cycles from beginning (for example, 100)
getY(image) // do whatever process you want
B = getTickCount() // number of clock cycles from beginning (for example, 150)
C = B - A // number of clock cycles for processing (150-100 = 50)Now you want to know how many seconds are these clock cycles. For that, you want to know how many seconds a single clock takes, i.e.
clock_time_period. If you find that, simply multiply by 50 to get total time taken.For that, OpenCV gives second function,
getTickFrequency(). It gives you frequency, i.e. how many clock cycles per second. You take its reciprocal to get time period of clock.time_period = 1/frequency.Now you have
time_period of one clock cycle, multiply it with 50 to get total time taken in seconds.Code Snippets
A = getTickCount() // number of clock cycles from beginning (for example, 100)
getY(image) // do whatever process you want
B = getTickCount() // number of clock cycles from beginning (for example, 150)
C = B - A // number of clock cycles for processing (150-100 = 50)time_period = 1/frequency.Context
StackExchange Code Review Q#17846, answer score: 7
Revisions (0)
No revisions yet.