Ask Your Question
0

Color recognition with OpenCV for Android

asked 2015-08-20 17:07:32 -0600

nbun gravatar image

I am trying the make an app that recognizes it white is on screen. I have written code that take an RGBA mat and converts it to a one pixel gray mat. I am trying to see the value of the grayPixel so I know what to use when I make my loop. Everytime I put

Log.i("Mat Value", grayPixel.dump());

I get 90 no matter what I point the camera at. When I do

Log.i("Mat Value", grayPixel..toString());

I get the mat size of 1*1, which is correct, and the address of the camera.

import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.hardware.camera2.CameraCaptureSession;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.MatOfPoint;
import org.opencv.imgproc.Imgproc;
import org.opencv.highgui.Highgui;


public class testActivity extends AppCompatActivity implements CvCameraViewListener2 {

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
public void onManagerConnected(int status){
        switch (status){
            case LoaderCallbackInterface.SUCCESS:
            {
                mOpenCvCameraView.enableView();
                break;
            }
            default:
            {
                super.onManagerConnected(status);
            }
        }
    }

};
private JavaCameraView mOpenCvCameraView;
Mat bgrPixel;
Mat grayPixel;
Mat rgbMat;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    mOpenCvCameraView = (JavaCameraView) findViewById(R.id.testVideoView);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);
}

@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();
    getSupportActionBar().hide();

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

    return super.onOptionsItemSelected(item);
}



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

public void onResume() {
    super.onResume();
    if (!OpenCVLoader.initDebug()) {
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallback);
    } else {
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }
}



@Override
public void onPause() {
    super.onPause();

    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    if(rgbMat == null){
        rgbMat = inputFrame.rgba();
        bgrPixel= rgbMat.submat(1,2,1,2).clone();
        grayPixel = new Mat();
        Imgproc.cvtColor(bgrPixel, grayPixel, Imgproc.COLOR_RGBA2GRAY, 1);
    }

    rgbMat= inputFrame.rgba();

    Log.i("Mat Value", grayPixel.toString());
    return rgbMat;
}


@Override
public void onCameraViewStarted(int width, int height) {

}
@Override
public void onCameraViewStopped() {
    bgrPixel.release();
    grayPixel.release();
    rgbMat.release();
}


}
edit retag flag offensive close merge delete

Comments

you only sample your pixel once in the 1st frame. is that intended ? i guess not.

in your onCameraFrame() you got if(rgbMat == null){ . this will be true only in the very 1st frame.

berak gravatar imageberak ( 2015-08-20 23:06:36 -0600 )edit

how would you suggest I fix this?

nbun gravatar imagenbun ( 2015-08-21 00:57:55 -0600 )edit

do you need the if(rgbMat == null){ ?

berak gravatar imageberak ( 2015-08-21 03:21:22 -0600 )edit

I got rid of if(rgbMat == null){ but am not getting the value of grayPixel.

nbun gravatar imagenbun ( 2015-08-21 03:43:31 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-08-21 03:47:44 -0600

nbun gravatar image

I got it to work thank you so much!!

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-08-20 17:07:32 -0600

Seen: 424 times

Last updated: Aug 20 '15