2014-06-08 45 views
0

我寫這段代碼來實時檢測使用opencv4android的功能。但不知何故,這段代碼不起作用,我找不到這個代碼中的錯誤,但我猜錯誤的代碼是{detector.detect(mRgba,keypoints)}。我很抱歉我的糟糕解釋。我對這個主題很陌生。請幫幫我。我使用openCV4Android的功能檢測代碼不起作用?

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

和錯誤是? – berak

+0

它給出空指針eception。 – user3719132

+0

以上實際檢測到的是什麼? –

回答

0

與OpenCV的Java包裝,你可以傳遞一個空的墊子結果/輸出值,而不是空的。

,所以你必須調用至少M =新墊(),或在這種情況下,關鍵點=新MatOfKeyPoint():

@Override 
    public Mat onCameraFrame(CvCameraViewFrame inputFrame) { 
     mRgba = inputFrame.rgba(); 
     keypoints = new MatOfKeyPoint(); 
      detector.detect(mRgba, keypoints); 
     Features2d.drawKeypoints(mRgba, keypoints, mRgba); 
     return mRgba; 
    } 

(在下一步的描述同樣的故事,順便說一句。 )

+0

謝謝,但現在它給出了一個異常,這是「輸入圖像的類型不正確」 – user3719132

+0

我找到了一種方法。感謝您的幫助。 – user3719132

0

它必須改變inputFrame.rgba();到inputFrame.gray();以及