0

我编写了迭代图像处理函数和下面列出的部分函数。

            Log.d(TAG, "#1 $iterNum")

            val alphaBlendedMat = Mat(targetMat.height(), targetMat.width(), CvType.CV_8UC4)
            for (i in 0..iterNum) {
                val alpha = fromAlpha + i * (toAlpha - fromAlpha) / iterNum

//            Border thickness
                val windowMaskIter = Mat()
                windowMask.copyTo(windowMaskIter)
                erode(windowMask, windowMask, kernel)
                Core.bitwise_xor(windowMaskIter, windowMask, windowMaskIter)

//            Src+target alpha blending
                val alphaBlendedMatTmp = Mat(targetMat.height(), targetMat.width(), CvType.CV_8UC4)
                Core.addWeighted(srcMat,
                        alpha,
                        targetMat.submat(roiTarget),
                        1 - alpha,
                        0.0,
                        alphaBlendedMatTmp.submat(roiTarget))
                Core.copyTo(alphaBlendedMatTmp.submat(roiTarget), alphaBlendedMat.submat(roiTarget), windowMaskIter)

                windowMaskIter.release()
                alphaBlendedMatTmp.release()
            }

            Log.d(TAG, "#2")

当 iterNum 接近 60 时,该代码段无任何异常地失败。Profiler 显示的内存使用量超过了模拟器实例上允许的内存使用量(~0.8Gb)。当我以迭代方式创建和发布新 Mats 时,我做错了什么?我相信如果我手动释放垫子,那么 GC 应该在需要新分配时释放内存。GC有问题还是我对事物的理解有问题?


解决者:

           for (i in 0..iterNum) {
                val alpha = fromAlpha + i * (toAlpha - fromAlpha) / iterNum

//            Border thickness
                val windowMaskIter = Mat()
                windowMask.copyTo(windowMaskIter)
                erode(windowMask, windowMask, kernel)
                Core.bitwise_xor(windowMaskIter, windowMask, windowMaskIter)

//            Src+target alpha blending
                val alphaBlendedMatTmp = Mat(targetMat.height(), targetMat.width(), CvType.CV_8UC4)
                val targetRoi = targetMat.submat(roiTarget)
                val alphaBlendedMatTmpRoi = alphaBlendedMatTmp.submat(roiTarget)
                Core.addWeighted(srcMat,
                        alpha,
                        targetRoi,
                        1 - alpha,
                        0.0,
                        alphaBlendedMatTmpRoi)
                val alphaBlendedMatRoi = alphaBlendedMat.submat(roiTarget)
                Core.copyTo(alphaBlendedMatTmpRoi, alphaBlendedMatRoi, windowMaskIter)

                targetRoi.release()
                windowMaskIter.release()
                alphaBlendedMatRoi.release()
                alphaBlendedMatTmp.release()
                alphaBlendedMatTmpRoi.release()
            }
4

0 回答 0