10
\$\begingroup\$

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.

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?

\$\endgroup\$
7
\$\begingroup\$

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 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.

\$\endgroup\$
1
\$\begingroup\$

You can try to separate the two scalars objects in an pre-created shared instance in order to use a unique space on memory and only one execution of the Scalar's constructor. Like this:

private static firstScalar = new Scalar(20, 100, 100);
private static secondScalar =  new Scalar(30, 255, 255);

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, firstScalar, secondScalar, mMattemp);
    return mMattemp;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.