I was always curious about image processing and last month gave open cv a toss , after working with the demos I wanted to test out image recognition . I started with face extraction and alignment from photos which should give better result .This is documented here (you can download the apk from google play below )
The problem with open cv is that works by design in horizontal orientation , which basically means that the phone has to be kept in a landscape mode for open cv to function .
This is quite irritating as opposed to the more natural portrait mode for taking the face shots.
I searched around for a possible solution and after trying out a lot of them I found that this one worked . The solution involves pixel by pixel flip after a matrix transpose. I wanted to work with the front camera and thought that just changing the code for the front camera will be enough , however as it turned out , it was not the case.
The front camera preview was upside down. It was clear that some changes also needs to be done to the rotating part of the code to make it work with the front camera. So , let us look at what worked.
For working with front camera , I made the following changes
In the initializeCamera function , I added the following
for (int i = 0; i < numberOfCameras; i++) { android.hardware.Camera.getCameraInfo(i, cameraInfo); if (cameraInfo.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT) { try { mCamera = Camera.open(i); mCameraId = i; //Added connected = true; } catch (RuntimeException e) { Log.e(TAG, "Camera #" + i + "failed to open: " + e.getMessage()); } if (connected) break; } }
To make the rotation correct in the front camera , i tried this
public Mat rgba() { Imgproc.cvtColor(mYuvFrameData, mRgba, Imgproc.COLOR_YUV2BGR_NV12, 4); if (mRotated != null) mRotated.release(); mRotated = mRgba.submat(0, mWidth, 0, mHeight); mRotated = mRotated.t(); Core.flip(mRotated, mRotated, 0); return mRotated; }
I am taking a submatrix with height and width exchanged . After the transpose , I am flipping it vertically with flip code == 0. This is to counter the the upside down in the front camera preview.
The full code can be downloaded here. To build the code , the open cv library has to be added from here
Check out the video below