Android - Opencv - 错误:(-215)u!= 0 in function void cv :: Mat :: create

时间:2016-04-13 09:16:03

标签: java android opencv opencv4android

我的应用不断出现此错误

FATAL EXCEPTION: Thread-22883
    Process: com.aar.Cline, PID: 7270
    CvException [org.opencv.core.CvException: cv::Exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/core/src/matrix.cpp:424: error: (-215) u != 0 in function void cv::Mat::create(int, const int*, int)
    ]
    at org.opencv.imgproc.Imgproc.cvtColor_1(Native Method)
    at org.opencv.imgproc.Imgproc.cvtColor(Imgproc.java:1724)
    at com.aar.Cline.TrackBallActivity.detectBall(TrackBallActivity.java:592)
    at com.aar.Cline.TrackBallActivity.onCameraFrame(TrackBallActivity.java:190)
    at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:391)
    at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:350)
    at java.lang.Thread.run(Thread.java:818)

该应用程序正常运行约3分钟,当突然在中间时,此错误显示在这里它不断崩溃的行

Mat grayImage1 = new Mat();
Imgproc.cvtColor(frame1, grayImage1, Imgproc.COLOR_BGR2GRAY);

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

通过使用new关键字,您将为free store上的Mat()对象分配内存(通常与heap相同)。这要求您稍后明确delete此对象。由于您没有这样做,您创建了内存泄漏,导致您的应用程序崩溃。因此,对于每个new,您必须拥有相应的delete

在你的情况下,它会是这样的:

Mat *grayImage1 = new Mat();
delete grayImage1;

为了避免动态分配,我只想做这样的事情:

Mat grayImage1 = frame1.gray();