You are on page 1of 5

Mirror x for front facing camera: (starting on 12-17-12 and resolved by 12-18-12)

I am working o! the OpenCV sample code for facial tracking for the Android. My previous tutorials show more details about this part so please go back and refer to my page if you dont have the project setup already. Use the new version of the Eclipse environment that was just released for implementation of the project. The code that I am looking at was from a sample project on how to x the mirroring issue with the front facing camera. ! // adding in the definition of a matrix for the mirror Matrix rotateRight = new Matrix(); if(android.os.Build.VERSION.SDK_INT>13) {! ! // matrix for mirroring of the image float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1}; rotateRight = new Matrix(); Matrix matrixMirrorY = new Matrix(); matrixMirrorY.setValues(mirrorY); rotateRight.postConcat(matrixMirrorY); } The code that performs the operation on the image is shown below. public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean lter) Added in API level 1 Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix. The new bitmap may be the same object as source, or a copy may have been made. It is initialized with the same density as the original bitmap. If the source bitmap is immutable and the requested subset is the same as the source bitmap itself, then the source bitmap is returned and no new bitmap is created. Parameters source The bitmap we are subsetting x The x coordinate of the rst pixel in source y The y coordinate of the rst pixel in source width The number of pixels in each row height The number of rows m Optional matrix to be applied to the pixels

lter

true if the source should be ltered. Only applies if the matrix contains more than just translation.

Returns A bitmap that represents the specied subset of source Throws

??? - there may be more information from the compiler but this is enough to get started. (the missing part if the exception that is thrown but can leave this out for now) Questions about code: Does the matrix m need to be the same dimension as the bitmap? What does the lter do to the source? Matrix Manipulations: Here is a good document that I found on matrix manipulations for reference. http://www.ux.uis.no/~hirpa/KdB/FEM/MatrixHandout.pdf Questions about the math: In order to correct for the mirroring, the columns should be inverted. How can this be done in code? Analysis: 1) Basically the order of the elements should be reversed to x the display.

a11 a12 a13 a14 a15 a16

a16 a15 a14 a13 a12 a11


This can be done algorithmically but may be too slow for large bit maps. It is good to look back at createBitmap to determine if there is a way to use the the matrix operation to perform the transform of the data.

2) The denition of the matrix class for Android provides more details on the operations that can be performed through the specication of the 3x3 matrix. The Matrix class holds a 3x3 matrix for transforming coordinates. Matrix does not have a constructor, so it must be explicitly initialized using either reset() - to construct an identity matrix, or one of the set..() functions (e.g. setTranslate, setRotate, etc.). Since we want to ip the values around the X axis to remove the mirroring, the matrix shown below is used.

-1 0 0 0 1 0 0 0 1
3) Here is some additional information on Wikipedia that explains how this works. http://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors# Uniform scaling and reection: Multiplying every vector with a constant real number k is represented by the diagonal matrix whose entries on the diagonal are all equal to k. Mechanically, this corresponds to stretching a rubber sheet equally in all directions such as a small area of the surface of an inating balloon. All vectors originating at origin (i.e., the xed point on the balloon surface) are stretched equally with the same scaling factor k while preserving its original direction. Thus, every non-zero vector is an eigenvector with eigenvalue k. Whether the transformation is stretching (elongation, extension, ination), or shrinking (compression, deation) depends on the scaling factor: if k > 1, it is stretching; if 0 < k < 1, it is shrinking. Negative values of k correspond to a reversal of direction, followed by a stretch or a shrink, depending on the absolute value of k. Implementation: Open the project for OpenCV and select the face-detection sample les. The le to be modied is the SampleCVViewBase.java le. Scroll down to the section shown in the screen shot.

Change the line ! to ! bmp = processFrame(mCamera);


Bitmap temp_bmp = processFrame(mCamera);

The new code will be inserted after this line and before ! ! ! mFps.measure(); New code: // setting up the steps for fixing the mirror issue with the front facing camera Matrix rotateRight = new Matrix(); if(android.os.Build.VERSION.SDK_INT>13) {! // matrix for mirroring of the image float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1}; rotateRight = new Matrix(); Matrix matrixMirrorY = new Matrix(); matrixMirrorY.setValues(mirrorY); rotateRight.postConcat(matrixMirrorY); }

// use the createBitmap to implement the mirror operation bmp = Bitmap.createBitmap(temp_bmp, 0, 0, ! temp_bmp.getWidth(), temp_bmp.getHeight(), rotateRight, true);

You might also like