0

我正在做一个简单的测试来显示 opencv4android 中 Mat 对象的内容。我发现 dump() 方法有效,它为我提供了矩阵的所有条目,而 get() 方法失败了,我打算从矩阵中获取单个条目。这两种方式在mainactivity中的onTouchEvent中实现,一种执行良好,另一种使程序崩溃。我不知道为什么。任何想法 ?

public class puzzle15Activity extends Activity
{
      Mat showMat;
private static final String TAG = "Sample::Activity";
public double[] a;
      double b;
      private SampleCvViewBase mView=null;
      private BaseLoaderCallback  mOpenCVCallBack = new BaseLoaderCallback(this) 
{
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                //mView = new puzzle15View(mAppContext);
                mView=new SampleCvViewBase(mAppContext);
                setContentView(mView);
                mView.openCamera();

            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }

    }
};


@Override
public void onCreate(Bundle savedInstanceState)
{
    Log.i(TAG, "onCreate");
    super.onCreate(savedInstanceState); 
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2,this,mOpenCVCallBack);

}


@Override
public boolean onTouchEvent(MotionEvent m)
{
    Toast.makeText(this,mView.mrgba.dump(),Toast.LENGTH_SHORT).show();
    a= mView.mrgba.get(1,1);
              //b=a[1];//here b can't be assigned this way.......
    //Toast.makeText(this,Double.toString(b),Toast.LENGTH_SHORT).show();
    return false;
}
}



class SampleCvViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private static final String TAG = "Sample::SurfaceView";

private SurfaceHolder       mHolder;
private VideoCapture        mCamera;
Mat mrgba;
public int[] list={1,2,3};

public SampleCvViewBase(Context context) {
    super(context);
    mrgba=new Mat(4,4,CvType.CV_16S);
    mHolder = getHolder();
    mHolder.addCallback(this);
}

public boolean openCamera() {
    synchronized (this) {
        releaseCamera();
        mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);
        if (!mCamera.isOpened()) {
            mCamera.release();
            mCamera = null;
            return false;
        }
    }
    return true;
}

public void releaseCamera() {
    Log.i(TAG, "releaseCamera");
    synchronized (this) {
        if (mCamera != null) {
                mCamera.release();
                mCamera = null;
        }
    }
}

public void setupCamera(int width, int height) {
    Log.i(TAG, "setupCamera("+width+", "+height+")");
    synchronized (this) {
        if (mCamera != null && mCamera.isOpened()) {
            List<Size> sizes = mCamera.getSupportedPreviewSizes();
            int mFrameWidth = width;
            int mFrameHeight = height;
            {
                double minDiff = Double.MAX_VALUE;
                for (Size size : sizes) {
                    if (Math.abs(size.height - height) < minDiff) {
                        mFrameWidth = (int) size.width;
                        mFrameHeight = (int) size.height;
                        minDiff = Math.abs(size.height - height);
                    }
                }
            }

            mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth);
            mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight);
        }
    }

}

public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
    Log.i(TAG, "surfaceChanged");
    setupCamera(width, height);
}

public void surfaceCreated(SurfaceHolder holder) {
    Log.i(TAG, "surfaceCreated");
    (new Thread(this)).start();
}

public void surfaceDestroyed(SurfaceHolder holder) {
    Log.i(TAG, "surfaceDestroyed");
    releaseCamera();
}

public void run() {
    Log.i(TAG, "Starting processing thread");
    while (true) {
        Bitmap bmp = null;

        synchronized (this) {
            if (mCamera == null)
                break;

            if (!mCamera.grab()) {
                Log.e(TAG, "mCamera.grab() failed");
                break;
            }

        }

            }

    Log.i(TAG, "Finishing processing thread");
}
}
4

1 回答 1

0

糟糕,我错误地调用了从不存在的矩阵组件。最初,矩阵只有一个通道。这就解释了麻烦。谢谢。

于 2012-11-20T01:16:50.323 回答