在void cv :: cvtColor中断言失败(scn == 3 || scn == 4)(cv :: InputArray Android

时间:2015-08-18 18:55:46

标签: java android opencv

我正在尝试将Mat转换为灰度值,以便我可以获得像素的数值。在虚拟机cv :: cvtColor(cv :: InputArray错误)上我得到了一个“断言失败(scn == 3 || scn == 4)。我知道我错过了什么,但我不知道它是什么

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.setMaxFrameSize(240,320);
    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,1,2,2).clone();
        grayPixel = new Mat();
        Imgproc.cvtColor(bgrPixel, grayPixel, Imgproc.COLOR_BGR2GRAY,1);
    }
    rgbMat= inputFrame.rgba();
    return rgbMat;
}


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

}

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


}

log cat

08-18 14:44:37.649  17571-17884/bluemage.systems.displaylatencytester E/cv::error()﹕ OpenCV Error: Assertion failed (scn == 3 || scn == 4) in void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp, line 3737
08-18 14:44:37.650  17571-17884/bluemage.systems.displaylatencytester E/org.opencv.imgproc﹕ imgproc::cvtColor_10() caught cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
08-18 14:44:37.659  17571-17884/bluemage.systems.displaylatencytester E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-4081
Process: bluemage.systems.displaylatencytester, PID: 17571
CvException [org.opencv.core.CvException: cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
]
        at org.opencv.imgproc.Imgproc.cvtColor_0(Native Method)
        at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:4244)
        at bluemage.systems.displaylatencytester.testActivity.onCameraFrame(testActivity.java:117)
        at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:387)
        at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:328)
        at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:4)

if(rgbMat == null){
    rgbMat = inputFrame.rgba();
    bgrPixel= rgbMat.submat(1,1,2,2).clone();
    grayPixel = new Mat();
    Imgproc.cvtColor(bgrPixel, grayPixel, Imgproc.COLOR_BGR2GRAY,1);
}

我在这里看到两个错误。我猜你是想提取第一个像素。我不知道你的是否是最好的方法,但是submat()(见[here](http://docs.opencv.org/java/org/opencv/core/Mat.html#submat(int,int,int,int)))需要rowStart, rowEnd, colStart, colEnd。因此,您应该将其更改为submat(1,2,1,2)

另请注意,rgba()会返回RGBA图片,因此您应使用Imgproc.COLOR_RGBA2GRAY