I am looking for a fast way to rotate a Bitmap 180 degrees using OpenCV on Android. Also, the Bitmap may be rotated in place, i.e. without allocation of additional memory.
Here riwnodennyk described different methods of rotating Bitmaps and compared their performances: https://stackoverflow.com/a/29734593/1707617. Here is GitHub repository for this study: https://github.com/riwnodennyk/ImageRotation.
This study doesn't include OpenCV implementation. So, I tried my own implementation of OpenCV rotator (only 180 degrees).
The best rotation methods for my test picture are as follows:
- OpenCV: 13 ms
- Ndk: 15 ms
- Usual: 29 ms
Because OpenCV performance looks very promising and my implementation seems to be unoptimized, I decided to ask you how to implement it better.
My implementation with comments:
@Override
public Bitmap rotate(Bitmap srcBitmap, int angleCcw) {
Mat srcMat = new Mat();
Utils.bitmapToMat(srcBitmap, srcMat); // Possible memory allocation and copying
Mat rotatedMat = new Mat();
Core.flip(srcMat, rotatedMat, -1); // Possible memory allocation
Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.ARGB_8888); // Unneeded memory allocation
Utils.matToBitmap(rotatedMat, dstBitmap); // Unneeded copying
return dstBitmap;
}
There are 3 places where, I suppose, unnecessary allocations and copying may take place.
Is it possible to get rid of theese unnecessary operations?