0

I did a loop do apply alpha mask in a OpenCV Mat, but to execute this loop take more than 5 seconds to execute in a RGB image of 1200x800, I don't understand why, because the same operation in C++ takes no more than some milliseconds.

I use the Android log to monitor when it enters on the loop, and when it goes out.

fun cutMask(background: Mat, foreground: Mat, mask: Mat) {
        val maskMat = Mat()
        val rows = background.rows()
        val cols = background.cols()

        val valueBack = ByteArray(4)
        val valueFore = ByteArray(4)
        val ValueAlpha = ByteArray(1)

        //Log.d(TAG, "Entering cut loop")

        for (r in 0 until rows) {
            for (c in 0 until cols) {
                background.get(r, c, valueBack)
                foreground.get(r, c, valueFore)
                mask.get(r, c, ValueAlpha)
                val alpha = ValueAlpha[0].toUByte().toFloat()/255f

                valueBack[0] = (valueBack[0].toUByte().toFloat()*(1f - alpha) + valueFore[0].toUByte().toFloat()*alpha).toByte()
                valueBack[1] = (valueBack[1].toUByte().toFloat()*(1f - alpha) + valueFore[1].toUByte().toFloat()*alpha).toByte()
                valueBack[2] = (valueBack[2].toUByte().toFloat()*(1f - alpha) + valueFore[2].toUByte().toFloat()*alpha).toByte()
                background.put(r, c, valueBack)
            }
        }

        //Log.d(TAG, "Out cut loop")
    }

PS: Even when I take out OpenCV and put simple arithmetic accounts just for testing, Kotlin remains extremely slow.

Alex
  • 3,301
  • 4
  • 29
  • 43
  • 1
    Image processing isn't performant at the Java/Kotlin level on Android (even with AOT/JIT). Look at JNI C/C++, OpenGL ES shaders, or Renderscript - see [old blog post with performance numbers at bottom](https://android-developers.googleblog.com/2012/01/levels-in-renderscript.html). – Morrison Chang Sep 28 '20 at 03:13
  • 1
    You may be better off using OpenCV C/C++ code and avoid the [JNI cost](https://stackoverflow.com/q/7699020/295004), based on this older 2017 paper: http://www.diva-portal.org/smash/get/diva2:1114955/FULLTEXT01.pdf – Morrison Chang Sep 28 '20 at 03:45
  • Well, of course I expected Java/Kotlin to be slower than C ++, I just didn't think it would be on that scale, a few tens of times it would be possible to support it. – Alex Sep 28 '20 at 03:57
  • Not sure if your C++ code was done on the same Android environment and only using a single thread like your example, and even then benchmark testing is tricky to get right, and the poor performance with Java/Kotlin is not unexpected. Regardless threading helps and the OpenCV C/C++ library has been tuned to some degree. – Morrison Chang Sep 28 '20 at 04:17

0 Answers0