Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to use OpenCV's Canny method, but after a certain amount of time, the screen crashes. #75

Open
yprogbest opened this issue May 23, 2023 · 0 comments

Comments

@yprogbest
Copy link

I want to use OpenCV's Canny method, but after a certain period of time, the screen crashes, and when I check the Profiler, even though I have released the mat, the MEMORY keeps increasing and when it reaches 2G, the screen crashes. The code and the Profiler screen are shown below.

`
class MainActivity : AppCompatActivity(), CameraBridgeViewBase.CvCameraViewListener2 {

private var mOpenCvCameraViewCanny:CameraBridgeViewBase?=null

private var mRgba:Mat? = null
private var mRgbaCanny:Mat?=null
private var mGray:Mat?=null


private var mCameraId:Int = JavaCameraView.CAMERA_ID_BACK// リアカメラをデフォルトで使用


private val mLoaderCallback = object :BaseLoaderCallback(this){
    override fun onManagerConnected(status: Int) {

        when(status){
            LoaderCallbackInterface.SUCCESS->{
                mOpenCvCameraViewCanny?.enableView()
            }
            else->{
                super.onManagerConnected(status)
            }
        }
    }
}


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val btnChange:Button = findViewById(R.id.btnChange)

    mOpenCvCameraViewCanny = findViewById<JavaCameraView>(R.id.cameraViewCanny)
    mOpenCvCameraViewCanny?.visibility = SurfaceView.VISIBLE
    mOpenCvCameraViewCanny?.setCvCameraViewListener(this)



    //ボタンが押された時に,frontとbackを入れ替える
    btnChange.setOnClickListener {

        if(btnChange.text == "BACK"){
            mCameraId= JavaCameraView.CAMERA_ID_FRONT// フロントカメラを使用
            mOpenCvCameraViewCanny?.disableView()
            mOpenCvCameraViewCanny?.setCameraIndex(mCameraId)
            mOpenCvCameraViewCanny?.enableView()

            btnChange.text = "FRONT"
        }else{
            mCameraId= JavaCameraView.CAMERA_ID_BACK// バックカメラを使用
            mOpenCvCameraViewCanny?.disableView()
            mOpenCvCameraViewCanny?.setCameraIndex(mCameraId)
            mOpenCvCameraViewCanny?.enableView()

            btnChange.text = "BACK"
        }


    }


    // プレビューを有効にする
    mOpenCvCameraViewCanny?.setCameraPermissionGranted()
    mOpenCvCameraViewCanny?.enableView()


}


override fun onResume() {
    super.onResume()

    if (!OpenCVLoader.initDebug()) {
        Log.d(TAG, "OpenCV library not found!")
    } else {
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS)
    }

}


//アクティビティが一時停止されたときに呼び出されるコールバックメソッド
override fun onPause() {
    super.onPause()
    mOpenCvCameraViewCanny?.disableView()
}

override fun onDestroy() {
    super.onDestroy()
    mOpenCvCameraViewCanny?.disableView()
}


override fun onCameraViewStarted(width: Int, height: Int) {

    mGray = Mat(height, width, CvType.CV_8UC1)
    mRgbaCanny = Mat(height, width, CvType.CV_8UC4)
    mRgba = Mat(height, width, CvType.CV_8UC4)

}

override fun onCameraViewStopped() {
    mGray?.release()
    mRgba?.release()
    mRgbaCanny?.release()
}

override fun onCameraFrame(inputFrame: CameraBridgeViewBase.CvCameraViewFrame?): Mat {


    mRgba = inputFrame?.rgba()



    //回転
    when (mCameraId) {
        JavaCameraView.CAMERA_ID_FRONT ->{
            Core.flip(mRgba?.t(), mRgba, 0)
            Core.flip(mRgba, mRgba, 1)
        }

        JavaCameraView.CAMERA_ID_BACK -> {
            Core.flip(mRgba?.t(), mRgba, 1)
        }
    }


    //Canny法の実装
    Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_BGRA2GRAY)
    Imgproc.GaussianBlur(mGray, mGray, Size(3.0,3.0),0.0) //ガウシアンフィルタによるぼかし処理
    Imgproc.Canny(mGray, mGray, 50.0, 100.0)
    Core.bitwise_not(mGray, mGray)
    Imgproc.cvtColor(mGray, mRgbaCanny, Imgproc.COLOR_GRAY2RGBA)



    return mRgbaCanny!!
}

}`

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant