Ask Your Question

Transposing and Flipping Frames on Android Camera

asked 2013-05-24 05:13:44 -0600

sp_mic88 gravatar image

updated 2013-06-06 08:02:53 -0600

Good morning. I am using Opencv4Android, ver 2.4.5, with Eclipse Juno. I have imported the opencv tutorial into Eclipse, and I have realized that face-detection sample, camera-control sample, ect, work only in landscape mode. I want to use these projects as base for my application, so I want to make possible to use that applications in all rotations of a device. So, I have found that it's possible to transpose and flip on the fly captured frames, in this way:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();  

    Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int screenOrientation = display.getRotation();
    switch (screenOrientation){
        case ORIENTATION_0: // Portrait
            Core.flip(mRgba.t(), mRgba, 1);
        case ORIENTATION_90: // Landscape right
            // do smth.
        case ORIENTATION_270: // Landscape left
            // do smth.
    return mRgba;

But the only result is that, in portrait mode, now the camera is only a black screen. The error in the logcat is:

05-24 12:05:55.168: ERROR/cv::error()(15219): OpenCV Error: Assertion failed (src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols) in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean), file /home/reports/ci/slave/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp, line 97
05-24 12:05:55.168: ERROR/ nMatToBitmap catched cv::Exception: /home/reports/ci/slave/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)
05-24 12:05:55.168: ERROR/CameraBridge(15219): Mat type: Mat [ 720*480*CV_8UC4, isCont=true, isSubmat=false, nativeObj=0x40e1f018, dataAddr=0x774c6010 ]
05-24 12:05:55.168: ERROR/CameraBridge(15219): Bitmap type: 720*480
05-24 12:05:55.168: ERROR/CameraBridge(15219): Utils.matToBitmap() throws an exception: /home/reports/ci/slave/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)

Please help me, if possible.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-06-03 16:49:13 -0600

wez470 gravatar image

updated 2013-06-04 08:45:33 -0600

I'm not so sure I can solve the problem entirely but I think I have some information that might be of use.

The error you are getting is from calling t() on an Rgba Mat. You can call t() on mGray and it will work without giving you an error.

Another thing is that I believe your flip function call is wrong. Giving a value of 1 will flip the image on the Y-axis and you want to flip it on the X-axis after rotating it 90 degrees so you should give it a value of 0.

I hope this bit of information is at least somewhat useful in getting part of your problem solved :) For some reason in my program when I display the edited gray frame, it is just black but I can process the gray frame and draw things on the rgba frame according to my findings and display that

P.S. I hate the formatting of this thing -- Solved it for you

edit flag offensive delete link more


ok, thank you!

sp_mic88 gravatar imagesp_mic88 ( 2013-06-06 06:46:42 -0600 )edit

Question Tools


Asked: 2013-05-24 05:13:44 -0600

Seen: 7,216 times

Last updated: Jun 06 '13