opencv sobel filter on real time video android

asked 2015-05-24 12:30:33 -0600

leoum gravatar image

updated 2015-05-25 04:00:32 -0600

I am new to opencv. I want to apply sobel filter on real time video using opencv in android but i am failed in doing so. when i use the built-in function of sobel in opencv it gives me null pointer exception. i have searched alot but didnt find my error..

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.WindowManager;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
public class MainActivity extends Activity implements CameraBridgeViewBase.CvCameraViewListener2{

private CameraBridgeViewBase mOpenCvCameraView;
private Mat sobell;
public static final int CV_32F=5;
private static final String TAG ="OpenCV" ;

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_7, this, mLoaderCallback);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.activity_main);
    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);
}

@Override
public void onPause()
{
    super.onPause();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * This method is invoked when camera preview has started. After this method is invoked
 * the frames will start to be delivered to client via the onCameraFrame() callback.
 *
 * @Param width  -  the width of the frames that will be delivered
 * @Param height - the height of the frames that will be delivered
 */
@Override
public void onCameraViewStarted(int width, int height) {

}

/**
 * This method is invoked when camera preview has been stopped for some reason.
 * No frames will be delivered via onCameraFrame() callback after this method is called.
 */
@Override
public void onCameraViewStopped() {

}

/**
 * This method is invoked when delivery of the frame needs to be done.
 * The returned values - is a modified frame which needs to be displayed on the screen.
 *
 * @Param inputFrame
 */
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

    Imgproc.Sobel(inputFrame.gray(), sobell, CV_32F, 1, 1, 3);

    return sobell;
}
}
edit retag flag offensive close merge delete

Comments

you can pass empty Mat's as an output, but not null-pointer ones, so, try:

public void onCameraViewStarted(int width, int height) {
    sobell = new Mat();
}
berak gravatar imageberak ( 2015-05-24 23:13:34 -0600 )edit

thank you sir this error is removed. I have changed this line Imgproc.Sobel(inputFrame.gray(), sobell, CV_32F, 1, 1, 3); to this Imgproc.Sobel(inputFrame.gray(),sobell,CV_8UC1,1,0);
but i am getting a white blank screen. can you please tell me that my value are correct or not?

leoum gravatar imageleoum ( 2015-05-27 04:06:18 -0600 )edit
1

 

  I'm getting the following error at the line:

    public class HelloOpenCvActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {


    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference


What could be wrong?

 

n8bar gravatar imagen8bar ( 2015-11-30 12:57:46 -0600 )edit

How did you solved that problem with the java.lang.NullPointerException? I'm having exactly the same error.

RicardoGomes123 gravatar imageRicardoGomes123 ( 2016-08-10 08:59:30 -0600 )edit