2017-04-11 10 views
0

OpenCVのMSERアルゴリズムを使用してテキストを検出するアンドロイドアプリケーションを開発しています。アプリケーションはうまく動作し、何をすべきかを実行していますが、1分未満で終了します。エラーや何かがないことに注意してください。私は問題がメモリであると思う、アプリケーションが実行されている間に私のデバイスのメモリを監視し、私のデバイスにメモリが残っていてもそれは動作を停止し、私の電話のホームページに戻り、何かにアプリケーションが非常に重くて、カメラが遅くなって突然閉じてしまうような多くの作業をしていると思います。遅いカメラとアプリが分未満で閉じる

私はこの問題を解決する方法を知りません。誰かがこれを解決する方法を教えてくれますか?

マイコード:

のxml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.lama.myapplication.MainActivity"> 



<org.opencv.android.JavaCameraView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentLeft="true" 
    android:gravity="center" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="10dp" 
    android:layout_marginBottom="10dp" 
    android:id="@+id/java_camera_view" 
    /> 


</android.support.constraint.ConstraintLayout> 

Javaクラス:

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 { 


private static final String TAG = "MainActivity"; 
JavaCameraView javaCameraView; 
Mat mRgba; 
//imgGray, imgCanny; 
private Mat mGrey,mIntermediateMat; 


BaseLoaderCallback mLoaderCallBack = new BaseLoaderCallback(this) { 
    @Override 
    public void onManagerConnected(int status) { 
     switch(status){ 
      case BaseLoaderCallback.SUCCESS:{ 
       javaCameraView.enableView(); 
       break; 
      } 
      default:{ 
       super.onManagerConnected(status); 
       break; 
      } 
     } 
    } 
}; 

static { 

} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

    //camera permission 
    String[] perms = {"android.permission.CAMERA"}; 
    int permsRequestCode = 200; 
    requestPermissions(perms, permsRequestCode); 

    javaCameraView=(JavaCameraView) findViewById(R.id.java_camera_view); 
    javaCameraView.setVisibility(SurfaceView.VISIBLE); 
    javaCameraView.setCvCameraViewListener(this); 


} 

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

@Override 
protected void onDestroy(){ 
    super.onDestroy(); 
    if (javaCameraView!=null) 
     javaCameraView.disableView(); 
    System.gc(); 

} 

@Override 
protected void onResume(){ 
    super.onResume(); 
    if(!OpenCVLoader.initDebug()){ 
     Log.d(TAG, "OpenCV not loaded"); 
     mLoaderCallBack.onManagerConnected(LoaderCallbackInterface.SUCCESS); 

    } else { 
     Log.d(TAG, "OpenCV loaded"); 
     OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9,this,mLoaderCallBack); 
    } 
} 

/** 
* A native method that is implemented by the 'native-lib' native library, 
* which is packaged with this application. 
*/ 
public native String stringFromJNI(); 

// Used to load the 'native-lib' library on application startup. 
static { 
    System.loadLibrary("native-lib"); 
} 

@Override 
public void onCameraViewStarted(int width, int height) { 
    /*mRgba=new Mat(height,width, CvType.CV_8UC4); 
    imgGray=new Mat(height,width, CvType.CV_8UC1); 
    imgCanny=new Mat(height,width, CvType.CV_8UC1);*/ 
    mIntermediateMat = new Mat(); 
    mGrey = new Mat(height, width, CvType.CV_8UC4); 
    mRgba = new Mat(height, width, CvType.CV_8UC4); 
} 

@Override 
public void onCameraViewStopped() { 
    mRgba.release(); 
} 

@Override 
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) { 
    // mRgba=inputFrame.rgba(); 

    /* Imgproc.cvtColor(mRgba,imgGray,Imgproc.COLOR_RGB2GRAY); 
    return imgGray; 

    Imgproc.Canny(imgGray,imgCanny, 50,150); 
    return imgCanny;*/ 

    // return mRgba; 

    mGrey = inputFrame.gray(); 
    mRgba = inputFrame.rgba(); 

    detectText(); 
    return mRgba; 
} 

private void detectText() { 
    Scalar CONTOUR_COLOR = new Scalar(255); 
    MatOfKeyPoint keypoint = new MatOfKeyPoint(); 
    List<KeyPoint> listpoint; 
    KeyPoint kpoint; 
    Mat mask = Mat.zeros(mGrey.size(), CvType.CV_8UC1); 
    int rectanx1; 
    int rectany1; 
    int rectanx2; 
    int rectany2; 
    int imgsize = mGrey.height() * mGrey.width(); 
    Scalar zeos = new Scalar(0, 0, 0); 

    List<MatOfPoint> contour2 = new ArrayList<MatOfPoint>(); 
    Mat kernel = new Mat(1, 50, CvType.CV_8UC1, Scalar.all(255)); 
    Mat morbyte = new Mat(); 
    Mat hierarchy = new Mat(); 



    Rect rectan3; 
    // 
    FeatureDetector detector = FeatureDetector.create(FeatureDetector.MSER); 
    detector.detect(mGrey, keypoint); 
    listpoint = keypoint.toList(); 
    // 
    for (int ind = 0; ind < listpoint.size(); ind++) { 
     kpoint = listpoint.get(ind); 
     rectanx1 = (int) (kpoint.pt.x - 0.5 * kpoint.size); 
     rectany1 = (int) (kpoint.pt.y - 0.5 * kpoint.size); 
     rectanx2 = (int) (kpoint.size); 
     rectany2 = (int) (kpoint.size); 
     if (rectanx1 <= 0) 
      rectanx1 = 1; 
     if (rectany1 <= 0) 
      rectany1 = 1; 
     if ((rectanx1 + rectanx2) > mGrey.width()) 
      rectanx2 = mGrey.width() - rectanx1; 
     if ((rectany1 + rectany2) > mGrey.height()) 
      rectany2 = mGrey.height() - rectany1; 
     Rect rectant = new Rect(rectanx1, rectany1, rectanx2, rectany2); 
     try { 
      Mat roi = new Mat(mask, rectant); 
      roi.setTo(CONTOUR_COLOR); 
      roi.release(); 
     } catch (Exception ex) { 
      Log.d("mylog", "mat roi error " + ex.getMessage()); 
     } 
    } 

    Imgproc.morphologyEx(mask, morbyte, Imgproc.MORPH_DILATE, kernel); 
    Imgproc.findContours(morbyte, contour2, hierarchy, 
      Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); 
    for (int ind = 0; ind < contour2.size(); ind++) { 
     // rectan3 = Imgproc.boundingRect(contour2.get(ind)); 
     rectan3 = Imgproc.boundingRect(contour2.get(ind)); 
     if (rectan3.area() > 0.5 * imgsize || rectan3.area() < 100 
       || rectan3.width/rectan3.height < 2) { 
      Mat roi = new Mat(morbyte, rectan3); 
      roi.setTo(zeos); 

     } else 
      Imgproc.rectangle(mRgba, rectan3.br(), rectan3.tl(), CONTOUR_COLOR); 
    } 

} 


//camera permission cont. 
@Override 
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){ 
    switch(permsRequestCode){ 
     case 200: 
      boolean cameraAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED; 
      break; 
    } 
} 


} 

誰かがどのように私は自分のアプリケーションを最適化し、この問題を解決することができます教えてもらえますか?

編集:私は次のようにマットオブジェクトのすべてを解放しようとした

@Override 
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) { 
    if(mRgba != null){ 
     mRgba.release(); 
    } 

    mRgba = inputFrame.rgba().clone(); 

    mGrey = inputFrame.gray(); 
    mRgba = inputFrame.rgba(); 

    detectText(); 
    return mRgba; 

} 

private void detectText() { 
    Scalar CONTOUR_COLOR = new Scalar(255); 
    MatOfKeyPoint keypoint = new MatOfKeyPoint(); 
    List<KeyPoint> listpoint; 
    KeyPoint kpoint; 
    Mat mask = Mat.zeros(mGrey.size(), CvType.CV_8UC1); 
    int rectanx1; 
    int rectany1; 
    int rectanx2; 
    int rectany2; 
    int imgsize = mGrey.height() * mGrey.width(); 
    Scalar zeos = new Scalar(0, 0, 0); 

    List<MatOfPoint> contour2 = new ArrayList<MatOfPoint>(); 
    Mat kernel = new Mat(1, 50, CvType.CV_8UC1, Scalar.all(255)); 
    Mat morbyte = new Mat(); 
    Mat hierarchy = new Mat(); 


    Rect rectan3; 
    // 
    FeatureDetector detector = FeatureDetector.create(FeatureDetector.MSER); 
    detector.detect(mGrey, keypoint); 
    listpoint = keypoint.toList(); 
    // 
    for (int ind = 0; ind < listpoint.size(); ind++) { 
     kpoint = listpoint.get(ind); 
     rectanx1 = (int) (kpoint.pt.x - 0.5 * kpoint.size); 
     rectany1 = (int) (kpoint.pt.y - 0.5 * kpoint.size); 
     rectanx2 = (int) (kpoint.size); 
     rectany2 = (int) (kpoint.size); 
     if (rectanx1 <= 0) 
      rectanx1 = 1; 
     if (rectany1 <= 0) 
      rectany1 = 1; 
     if ((rectanx1 + rectanx2) > mGrey.width()) 
      rectanx2 = mGrey.width() - rectanx1; 
     if ((rectany1 + rectany2) > mGrey.height()) 
      rectany2 = mGrey.height() - rectany1; 
     Rect rectant = new Rect(rectanx1, rectany1, rectanx2, rectany2); 
     try { 
      Mat roi = new Mat(mask, rectant); 
      roi.setTo(CONTOUR_COLOR); 
      roi.release(); 
     } catch (Exception ex) { 
      Log.d("mylog", "mat roi error " + ex.getMessage()); 
     } 
    } 

    Imgproc.morphologyEx(mask, morbyte, Imgproc.MORPH_DILATE, kernel); 
    Imgproc.findContours(morbyte, contour2, hierarchy, 
      Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); 
    for (int ind = 0; ind < contour2.size(); ind++) { 
     // rectan3 = Imgproc.boundingRect(contour2.get(ind)); 
     rectan3 = Imgproc.boundingRect(contour2.get(ind)); 
     if (rectan3.area() > 0.5 * imgsize || rectan3.area() < 100 
       || rectan3.width/rectan3.height < 2) { 
      Mat roi = new Mat(morbyte, rectan3); 
      roi.setTo(zeos); 
      roi.release(); 
     } else 
      Imgproc.rectangle(mRgba, rectan3.br(), rectan3.tl(), CONTOUR_COLOR); 
    } 

    mask.release(); 
    kernel.release(); 
    morbyte.release(); 
    hierarchy.release(); 
    mGrey.release(); 
    mIntermediateMat.release(); 

} 

それでもまだ私は同じ問題を抱えています! 誰かがこれを解決するのを手伝ってもらえますか?

答えて

0

メモリの問題のため、近くにアプリケーションがあります。関数の

  1. まず、あなたが関数の最後に割り当てられたすべてのマットオブジェクトを解放する必要が** detectText()**
  2. onCameraFrame(...)は、再割り当てする前にメモリを解放します。

例あなたは

@Override 
public Mat onCameraFrame(final CameraBridgeViewBase.CvCameraViewFrame inputFrame) { 

    if(mRgba != null){ 
     mRgba.release(); 
    } 

    mRgba = inputFrame.rgba().clone(); 

    //your code here 
} 

// EDITを試すことができます:2017年12月4日

あなたのコードは

@Override 
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) 
{ 
if(mRgba != null){ 
    mRgba.release(); 
} 

if(mGrey != null){ 
    mGrey.release(); 
} 

mRgba = inputFrame.rgba().clone(); 
mGrey = inputFrame.gray().clone(); 

detectText(); 
return mRgba; 

} 

private void detectText() { 
Scalar CONTOUR_COLOR = new Scalar(255); 
MatOfKeyPoint keypoint = new MatOfKeyPoint(); 
List<KeyPoint> listpoint; 
KeyPoint kpoint; 
Mat mask = Mat.zeros(mGrey.size(), CvType.CV_8UC1); 
int rectanx1; 
int rectany1; 
int rectanx2; 
int rectany2; 
int imgsize = mGrey.height() * mGrey.width(); 
Scalar zeos = new Scalar(0, 0, 0); 

List<MatOfPoint> contour2 = new ArrayList<MatOfPoint>(); 
Mat kernel = new Mat(1, 50, CvType.CV_8UC1, Scalar.all(255)); 
Mat morbyte = new Mat(); 
Mat hierarchy = new Mat(); 


Rect rectan3; 
// 
FeatureDetector detector = FeatureDetector.create(FeatureDetector.MSER); 
detector.detect(mGrey, keypoint); 
listpoint = keypoint.toList(); 
// 
for (int ind = 0; ind < listpoint.size(); ind++) { 
    kpoint = listpoint.get(ind); 
    rectanx1 = (int) (kpoint.pt.x - 0.5 * kpoint.size); 
    rectany1 = (int) (kpoint.pt.y - 0.5 * kpoint.size); 
    rectanx2 = (int) (kpoint.size); 
    rectany2 = (int) (kpoint.size); 
    if (rectanx1 <= 0) 
     rectanx1 = 1; 
    if (rectany1 <= 0) 
     rectany1 = 1; 
    if ((rectanx1 + rectanx2) > mGrey.width()) 
     rectanx2 = mGrey.width() - rectanx1; 
    if ((rectany1 + rectany2) > mGrey.height()) 
     rectany2 = mGrey.height() - rectany1; 
    Rect rectant = new Rect(rectanx1, rectany1, rectanx2, rectany2); 
    try { 
     Mat roi = new Mat(mask, rectant); 
     roi.setTo(CONTOUR_COLOR); 
     roi.release(); 
    } catch (Exception ex) { 
     Log.d("mylog", "mat roi error " + ex.getMessage()); 
    } 
} 

Imgproc.morphologyEx(mask, morbyte, Imgproc.MORPH_DILATE, kernel); 
Imgproc.findContours(morbyte, contour2, hierarchy, 
     Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); 
for (int ind = 0; ind < contour2.size(); ind++) { 
    // rectan3 = Imgproc.boundingRect(contour2.get(ind)); 
    rectan3 = Imgproc.boundingRect(contour2.get(ind)); 
    if (rectan3.area() > 0.5 * imgsize || rectan3.area() < 100 
      || rectan3.width/rectan3.height < 2) { 
     Mat roi = new Mat(morbyte, rectan3); 
     roi.setTo(zeos); 
     roi.release(); 
    } else 
     Imgproc.rectangle(mRgba, rectan3.br(), rectan3.tl(), CONTOUR_COLOR); 
} 

mask.release(); 
kernel.release(); 
morbyte.release(); 
hierarchy.release(); 
mIntermediateMat.release(); 

} 
+0

する必要があります私ははならあなたが全体のコードを投稿してくださいすることができますそれぞれのマットを解放してください。私はそれを試して、黒い画面を持っているので、マットリリースでdetectTextメソッドのコード全体を投稿してください。 –

+0

私の更新されたポストを見て、あなたが示唆したようにすべてのマットオブジェクトをリリースしましたが、同じ問題が表示されます:( –

+0

私は投稿を更新しました – Kheuch

関連する問題