我编写此代码以使用opencv4android实时检测功能。但是这个代码不能正常工作,我无法在此代码中找到错误,但我想错误的代码就是这个{detector.detect(mRgba,关键点)对不起我的糟糕解释,我很抱歉。我对这个主题真的很陌生。请帮帮我。
public class MainActivity extends Activity implements CvCameraViewListener2{
private CameraBridgeViewBase mOpenCvCameraView;
private static final String TAG = "feature detection";
private Mat mRgba, mGray;
private FeatureDetector detector;
private DescriptorExtractor SurfExtractor;
private MatOfKeyPoint keypoints;
BaseLoaderCallback blc = new BaseLoaderCallback(this) {
public void onManagerConnected(int status) {
if(status == LoaderCallbackInterface.SUCCESS){
Log.i(TAG, "OpenCV loaded successfully");
mOpenCvCameraView.enableView();
mOpenCvCameraView.setCameraIndex(0);
}else{
super.onManagerConnected(status);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.cam);
mOpenCvCameraView.setCvCameraViewListener(this);
}
@Override
protected void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_8, this,blc);
}
@Override
protected void onPause() {
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onDestroy() {
super.onDestroy();
mOpenCvCameraView.disableView();
}
@Override
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(width, height, CvType.CV_8SC4);
detector = FeatureDetector.create(FeatureDetector.ORB);
SurfExtractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
}
@Override
public void onCameraViewStopped() {
mRgba.release();
}
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
detector.detect(mRgba, keypoints);
Features2d.drawKeypoints(mRgba, keypoints, mRgba);
return mRgba;
}
}
答案 0 :(得分:0)
使用opencv java包装器,您可以为结果/输出值传递空Mat,但不能为空值传递。
所以你必须至少调用m = new Mat(),或者在这种情况下,keypoints = new MatOfKeyPoint():
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
keypoints = new MatOfKeyPoint();
detector.detect(mRgba, keypoints);
Features2d.drawKeypoints(mRgba, keypoints, mRgba);
return mRgba;
}
(与下一步中的描述符相同,顺便说一句。)
答案 1 :(得分:0)
它必须改变inputFrame.rgba(); to inputFrame.gray();以及