I am building Face detection app using OpenCv. I am processing preview frame data received on callback onPreviewFrame. I am using camera in portrait mode, whereas onPreviewFrame returns me data in landscape mode. I am rotating frame data using this code.
public static byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight) {
byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
// Rotate the Y luma
int i = 0;
for (int x = 0; x < imageWidth; x++) {
for (int y = imageHeight - 1; y >= 0; y--) {
yuv[i] = data[y * imageWidth + x];
i++;
}
}
// Rotate the U and V color components
i = imageWidth * imageHeight * 3 / 2 - 1;
for (int x = imageWidth - 1; x > 0; x = x - 2) {
for (int y = 0; y < imageHeight / 2; y++) {
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
i--;
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)];
i--;
}
}
return yuv;
}
After rotating data, I am converting to byte array to OpenCv Mat. After the conversion I pass into openCv native code.
In landscape mode (without rotating preview data), I am able to get almost 20 FPS after processing camera preview. But in portrait mode, with above method, the FPS is reduced to 3 FPS. On measuring time taken by rotateYUV420Degree90
, this method is the main culprit.
I am new to OpenCv. Is there any other approach that i can take to rotate preview data using java code or native code, fastly. Because of complexity of my app, i cannot use JavaCameraView
provided by OpenCV.