issues in working with android portrait mode with

2019-06-09 23:35发布

问题:

I know this is one of the most repeated questions, however, no working solution found anywhere, after putting so much of efforts.

This is really a killing issue though it might be a simple one for the experts.

I am working on opencv Haar Cascade classifiers.(Eg: Face Detection, Eye Pair Detection)

I have just taken the face-detection sample code from the "OpenCV-2.4.9-android-sdk" - samples.

This sample code is set in Landscape mode and everything is working fine.

However, i want to make make the classifiers work in portrait mode. I know the haar classifiers were not made to work with portrait mode.

As opencv uses "CameraBridgeViewBase", i don't have all controls to play with the resolution of the camera and displaying the images back on the screen.(preview)

Now, the moment i set the screen orientation as "android:screenOrientation="portrait", the images is rotated 90deg clock wise.

What have i tried:

To preview the portrait image without rotation: i modified the "deliverAndDrawFrame" in "CameraBridgeViewBase" by adding

            Matrix matrix = new Matrix();
            matrix.preTranslate((canvas.getWidth() - mCacheBitmap.getWidth()) / 2,(canvas.getHeight() - mCacheBitmap.getHeight()) / 2);

            if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
                matrix.postRotate(90f,(canvas.getWidth()) / 2,(canvas.getHeight()) / 2);
            canvas.drawBitmap(mCacheBitmap, matrix, null);

To make the classifier work with portrait mode: I played with all kinds of permutation and combination of transpose and flip to rotate the grey scale image that i pass to detectmultiscale in "onCameraFrame".

What is my issue?

  1. I am not able to get the images with native resolution which is higher than what i get at "onCameraFrame". How to get the original or actual or native quality of the images upto the camera sensors capacity?

  2. As i already put a lot efforts on making the classifier work with portrait mode without any luck, any kind of suggestions/sample codes/reference which are working,would help me a lot.

回答1:

I found a solution:

  Matrix matrix = new Matrix();
  matrix.preTranslate((canvas.getWidth() - mCacheBitmap.getWidth()) / 2,(canvas.getHeight() - mCacheBitmap.getHeight()) / 2);
  matrix.postRotate(90f,(canvas.getWidth()) / 2,(canvas.getHeight()) / 2);
  float scale = (float) canvas.getWidth() / (float) mCacheBitmap.getHeight();
  matrix.postScale(scale, scale, canvas.getWidth()/2 , canvas.getHeight()/2 );
  canvas.drawBitmap(mCacheBitmap, matrix, new Paint());

This delivers portrait mode with CORRECT scale :D try it please. I'm using OpenCV 3.4 .