gpt4 book ai didi

java - Android OpenCV 内存泄漏

转载 作者:行者123 更新时间:2023-12-01 16:59:40 25 4
gpt4 key购买 nike

我正在开发一个使用 opencv 处理视频的应用程序。

这是我的函数的 opencv 部分:

 //Open the streams
var merged = VideoCapture()
merged.open(file.path)
var flicker = VideoCapture()
flicker.open("$cacheLocation/flicker.avi")
var grain = VideoCapture()
grain.open("$cacheLocation/oldgrain.avi")

//Check if all streams are opened
if (!merged.isOpened || !flicker.isOpened || !grain.isOpened){
Log.e("OpenCVHelper", "One or more streams did not open, ABORT!")
throw Exception()
}

val videoWriter = VideoWriter()
videoWriter.open("$location/$fileName", VideoWriter.fourcc('M', 'J', 'P', 'G'), merged.get(Videoio.CAP_PROP_FPS), Size(merged.get(Videoio.CAP_PROP_FRAME_WIDTH), merged.get(Videoio.CAP_PROP_FRAME_HEIGHT)))

//Setup variables
var totalFrames = merged.get(Videoio.CAP_PROP_FRAME_COUNT)
var grainFrames = grain.get(Videoio.CAP_PROP_FRAME_COUNT)
var flickerFrames = flicker.get(Videoio.CAP_PROP_FRAME_COUNT)
var grainCounter = 0
var flickerCounter = 0

//Setup mat's
var frameMat = Mat()
var rgbaFrame = Mat()
var whiteAlphaMat = Mat.ones(Size(merged.get(Videoio.CAP_PROP_FRAME_WIDTH), merged.get(Videoio.CAP_PROP_FRAME_HEIGHT)),0)
whiteAlphaMat.setTo(Scalar((255).toDouble()))
var videoTmpMat = mutableListOf<Mat>()
var videoRgbaMat = mutableListOf<Mat>()
var flickerMat = Mat()
var grainMat = Mat()
var tmp = Mat()
var alpha = Mat()
var rgb = mutableListOf<Mat>()
var rgba = mutableListOf<Mat>()
var flickerAlphaMat = Mat()
var grainAplhaMat = Mat()


for(frameCounter in 0..totalFrames.toInt()){
System.gc()
//Set the position of the readers
merged.set(Videoio.CAP_PROP_POS_FRAMES, frameCounter.toDouble())
grain.set(Videoio.CAP_PROP_POS_FRAMES, grainCounter.toDouble())
flicker.set(Videoio.CAP_PROP_POS_FRAMES, flickerCounter.toDouble())

//Create material of the current frame
merged.read(frameMat)

//Create an rgba mat of the frame

videoTmpMat.clear()
Core.split(frameMat, videoTmpMat)
videoRgbaMat.clear()
videoRgbaMat.add(videoTmpMat[0])
videoRgbaMat.add(videoTmpMat[1])
videoRgbaMat.add(videoTmpMat[2])
videoRgbaMat.add(whiteAlphaMat)
Core.merge(videoRgbaMat, rgbaFrame)

//Create the overlay materials
flicker.read(flickerMat)
grain.read(grainMat)

//Create rgba of the overlays
rgb.clear()
rgba.clear()

Imgproc.cvtColor(flickerMat, tmp, Imgproc.COLOR_BGR2GRAY)
Imgproc.threshold(tmp, alpha, (100).toDouble(), (255).toDouble(), Imgproc.THRESH_BINARY)
Core.split(flickerMat, rgb)
rgba.add(rgb[0])
rgba.add(rgb[1])
rgba.add(rgb[2])
rgba.add(alpha)

Core.merge(rgba, flickerAlphaMat)

//tmp = Mat()
//alpha = Mat()
//rgb = mutableListOf()
//rgba = mutableListOf()
rgb.clear()
rgba.clear()

Imgproc.cvtColor(grainMat, tmp, Imgproc.COLOR_BGR2GRAY)
Imgproc.threshold(tmp, alpha, (100).toDouble(), (255).toDouble(), Imgproc.THRESH_BINARY)
Core.split(grainMat, rgb)
rgba.add(rgb[0])
rgba.add(rgb[1])
rgba.add(rgb[2])
rgba.add(alpha)
Core.merge(rgba, grainAplhaMat)

//Apply blur to the frame
Imgproc.blur(rgbaFrame, rgbaFrame, Size((8).toDouble(),(8).toDouble()))

//Apply flicker to the frame
Core.addWeighted(rgbaFrame, 1.0, flickerAlphaMat, 0.5, 0.0, rgbaFrame)

//Apply grain to the frame
Core.addWeighted(rgbaFrame, 1.0, grainAplhaMat, 0.5, 0.0, rgbaFrame)

//Add a title
Imgproc.putText(rgbaFrame, "Made with Super8Life", Point(10.0, 1050.0), 7, 2.0, Scalar(255.0,255.0,255.0), 2);

//Write frame to file
videoWriter.write(rgbaFrame)

//Clear up memory
//frameMat.release()
//flickerAlphaMat.release()
//flickerMat.release()
//grainAplhaMat.release()
//grainMat.release()
//videoTmpMat.forEach {
// it.release()
//}
//videoRgbaMat.forEach {
// it.release()
//}
//whiteAlphaMat.release()

//Handle the counters
Log.d("OpenCVHelper", "Frame: $frameCounter")
//val percentage = frameCounter.toDouble() / totalFrames * 100.toDouble()
//Log.d("OpenCVHelper", "$percentage%")

if (grainCounter < grainFrames -1){
grainCounter ++;
} else {
grainCounter = 0;
}
if (flickerCounter < flickerFrames -1){
flickerCounter ++;
} else {
flickerCounter = 0;
}
}
videoWriter.release()

在测试应用程序的 5 次中,它消耗了 4 次内存,导致应用程序崩溃: enter image description here

我不明白我做错了什么?我已经将 Mat 的声明移到了循环之外。 Mat.release() 不执行任何操作

非常感谢,朱尔斯

最佳答案

我遇到了同样的问题,但我看到 this video并将我的manifest.xml更改为:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myopencvandroid">

<uses-permission android:name="android.permission.CAMERA"/>

<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar">
<activity android:name=".MainActivity"
android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

我的 RAM 内存已使用 60MB !!!

关于java - Android OpenCV 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61528915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com