Opencv for android ignoring inputFrame

asked 2018-06-11 09:31:28 -0600

federicoponzi gravatar image

updated 2018-06-11 09:33:34 -0600

I'm trying to create a simple helloworld in android, but not sure why it's not displayed what I return from onCameraFrame.

I'm using opencv 3.4.1 on android 8.0.0. This dosen't draw anything:

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
{
    mRgba = inputFrame.rgba();
    Imgproc.circle (
            mRgba,                 //Matrix obj of the image
            new Point(mWidth * 0.5, mHeight * 0.5),    //Center of the circle
            10,                    //Radius
            new Scalar(0, 0, 255),  //Scalar object for color
            10                     //Thickness of the circle
    );
     Log.i(TAG, "Called.");
    return mRgba;

But Called appears in the prompt. And neither this is working:

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
{
    return inputFrame.gray();
}

The image is still colored. I've searched a lot and tryied different solutions, and none of them worked :(

Here is the complete class code:

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2
{

    private static final String TAG = MainActivity.class.getCanonicalName();
    private CameraBridgeViewBase mOpenCvCameraView;
    private static final Scalar FACE_RECT_COLOR = new Scalar(0, 255, 0, 255);
    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this)
    {
        @Override
        public void onManagerConnected(int status)
        {
            switch (status)
            {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i(TAG, "OpenCV loaded successfully");
                    try
                    {
                        initializeOpenCVDependencies();
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                break;
                default:
                {
                    super.onManagerConnected(status);
                }
                break;
            }
        }
    };
    private Mat mRgba;
    private Mat mGray;
    private int mWidth;
    private int mHeight;

    private void initializeOpenCVDependencies() throws IOException
    {

        mOpenCvCameraView.enableView();

    }


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        if (ContextCompat.checkSelfPermission(this,
                                              Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_DENIED)
        {
            Log.i(TAG, "Requesting camera permissions because are: " +
                    ContextCompat.checkSelfPermission(this,
                                                      Manifest.permission.CAMERA));
            requestPermissions(new String[]{Manifest.permission.CAMERA}, 13);
            return;
        }

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

            }
        });

        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
    {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
        {

            Snackbar.make(findViewById(R.id.container), "Permission Granted",
                          Snackbar.LENGTH_LONG).show();

        }
        else
        {

            Snackbar.make(findViewById(R.id.container), "Permission denied",
                          Snackbar.LENGTH_LONG).show();

        }
    }

    @Override
    public void onResume()
    {

        super.onResume();
        if (!OpenCVLoader.initDebug())
        {
            Log.d(TAG, "Internal OpenCV library not found. Using OpenCV Manager for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this, mLoaderCallback);
        }
        else
        {
            Log.d(TAG, "OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }

    @Override
    public void onPause()
    {

        super.onPause();
        if (mOpenCvCameraView != null)
        {
            mOpenCvCameraView.disableView();
        }
    }

    @Override
    public void onCameraViewStarted(int width, int height)
    {
        mGray = new Mat(height, width, CvType.CV_8UC4);
        mRgba = new Mat();
        mWidth = width;
        mHeight = height;
    }

    @Override
    public void onCameraViewStopped()
    {
        mGray.release();
        mRgba.release();
    }

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
    {
        return inputFrame.gray();
    }
}
edit retag flag offensive close merge delete

Comments

I'm having the same problem. It only displays original rgb frames, even when I change inputFrame.rgba()

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame)
{
    matgray = inputFrame.gray();
    return matgray;

When I leave inputFrame.rgba() and try to use openCV function Imgproc.cvtColor(matrgba,matgray, Imgproc.COLOR_RGBA2GRAY) the problem continues...

We've followed some tutorials on the internet and don't understand what's missing. Any help would be welcome! I'm using openCV 3.4.3 and the latest android studio 3.4 (android-studio-ide-183.5452501-windows)

Thanks!

tgpsantos gravatar imagetgpsantos ( 2019-04-29 05:19:21 -0600 )edit

@tgpsantos , however, please do not post answers here, if you have a question or a comment, thank you.

berak gravatar imageberak ( 2019-04-29 05:31:12 -0600 )edit