App & Web/Android

[코틀린] 이미지 이진화 하기

sillon 2022. 10. 11. 11:33
728x90
반응형

모듈 다운 참고 사이트

https://jade314.tistory.com/entry/Android-Kotlin-OpenCV

 

Android Kotlin + OpenCV

1. 최신 OpenCV sdk 다운로드 다운로드 : opencv-4.5.1-android-sdk.zip 2 .OpenCV를 모듈로 추가 File > New > Import Module 메뉴 선택후 압축파일을 푼 경로에서 sdk 디렉토리를 선택한다. OpenCV를 안드로이..

jade314.tistory.com

 

 

 

클래스 상단에 먼저 모듈 임포트

    // OPEN CV LOAD
    init {
        val isIntialized = OpenCVLoader.initDebug()
    }
    fun makeGray(bitmap: Bitmap) : Bitmap {

        // Create OpenCV mat object and copy content from bitmap
        val graySrc = Mat()
        Utils.bitmapToMat(bitmap, graySrc)

        // Convert to grayscale
        Imgproc.cvtColor(graySrc, graySrc, Imgproc.COLOR_RGB2GRAY)

        // 이진화
        val binarySrc = Mat()
        Imgproc.threshold(graySrc, binarySrc, 0.0, 255.0, Imgproc.THRESH_OTSU)

        // Make a mutable bitmap to copy grayscale image
        val grayBitmap = bitmap.copy(bitmap.config, true)
        Utils.matToBitmap(binarySrc, grayBitmap)

        return grayBitmap
    }

728x90
반응형