The program executes but the contours are not drawn

asked 2016-01-27 08:16:44 -0500

Poovamraj gravatar image

package org.opencv.samples.tutorial1;

import org.opencv.android.BaseLoaderCallback; import org.opencv.android.CameraBridgeViewBase; import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.*; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc;

import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceView; import android.view.View; import android.view.View.OnTouchListener; import android.view.WindowManager;

import com.example.imgloader.R;

import java.util.ArrayList; import java.util.List;

public class Tutorial1Activity extends Activity implements CvCameraViewListener2 {

private static final String TAG = "OCVSample::Activity";
private Mat mRgba;
private Mat mGray;
private Mat fin;
private CameraBridgeViewBase mOpenCvCameraView;
Scalar min=new Scalar(0,30,60,0);
Scalar max=new Scalar(20,150,255,0);
Mat pointMatHsv;
Mat heirarchy;

Scalar CONTOUR_COLOR = new Scalar(255,0,0);

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;
        }
    }
};

public Tutorial1Activity() {
    Log.i(TAG, "Instantiated new " + this.getClass());
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.tutorial1_surface_view);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial1_activity_java_surface_view);
    mOpenCvCameraView.setMaxFrameSize(720, 480);

    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);

    mOpenCvCameraView.setCvCameraViewListener(this);
}

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

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}

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


public void onCameraViewStarted(int width, int height) {
    mGray = new Mat();
    mRgba = new Mat();
    pointMatHsv = new Mat(1, 1, CvType.CV_8UC3);
    heirarchy=new Mat();




}

public void onCameraViewStopped() {
}

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

    List<MatOfPoint> contours =new ArrayList<MatOfPoint>();
    List<MatOfInt> hull = new ArrayList<MatOfInt>();
    Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGB2HSV);
    Core.inRange(mGray, min, max, mRgba);
    Imgproc.dilate(mRgba, mRgba, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
    //Imgproc.dilate(mRgba, mRgba, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));
    Imgproc.erode(mRgba, mRgba, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2, 2)));




    mRgba.copyTo(pointMatHsv);
    contours=retContours(pointMatHsv);



    Imgproc.drawContours(pointMatHsv,contours,-1,CONTOUR_COLOR);
    return mRgba;
}
public List<MatOfPoint> retContours(Mat pointMatHsv)
{
    List<MatOfPoint> contours =new ArrayList<MatOfPoint>();
    Imgproc.findContours(pointMatHsv, contours, heirarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_NONE);
    return contours;
}

}

the program executes perfectly but the contours are not drawn in the image. please help

edit retag flag offensive close merge delete