Reputation: 520
I'd like to convert a RGB image to Gray like this function does :
Imgproc.cvtColor(mRgba, mGrey, Imgproc.COLOR_RGBA2GRAY);
But openCV uses a specific formula (0.299*R + 0.587*G + 0.114*B I think ?), and I'd like to use min(R,G,B) instead.
Is there a performance-efficient way to do that ? (This is OpenCV for android, so with Java)
EDIT :
Here is the profiling, which I definitely should have done earlier :
Base application capturing the camera runs at 12 FPS
That may look incredible but just that little function costs half the FPS. The device can yet run very complicated stuff like ORB at decent rates (3-4 FPS), and all O(n) C++ routines without FPS difference. Also just Miki's "trick" makes the function 10x faster.
Upvotes: 0
Views: 1348
Reputation: 706
There is no pre-built way to give OpenCV an arbitrary function for conversion to grayscale. The remaining option is using the RGB channels themselves.
for (int x = 0; x < mRgba.cols(); x++)
{
for (int y = 0; y < mRgba.rows(); y++)
{
double[] rgb = mRgba.get(x, y);
mGray.put(x, y, Math.min(rgb[0], Math.min(rgb[1], rgb[2])));
}
}
As you have mentioned, it may be too slow, especially with large images and the branching that comes from if-statements. However, there are three rules of optimization: (1) Don't do it, (2) Don't do it yet, and (3) Profile first. Simply put, try the easy-to-code version first, then once you know for a fact this section is the slow section [i.e, by running it and timing it], change the code to be more efficient.
Upvotes: 1