java opencv错误内存不足和断言失败
Posted
技术标签:
【中文标题】java opencv错误内存不足和断言失败【英文标题】:java opencv error insufficient memory and assertion failed 【发布时间】:2015-08-11 16:29:59 【问题描述】:我正在使用 java 开发一个程序,该程序使用 opencv 库获取图像文件夹并仅裁剪人脸。它使用来自opencv的人脸识别。我让它工作了,但是当我尝试使用更大的图像文件夹时,它会因以下错误而停止:
OpenCV 错误:cv::OutOfMemoryError 中的内存不足(无法分配 411068928 字节),文件 C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\alloc.cpp,第 52 行 OpenCV错误: cv::Mat::create 中的断言失败 (u != 0),文件 C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\matrix.cpp,第 411 行线程中的异常"main" CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\matrix.cpp:411: 错误: (-215 ) u != 0 in function cv::Mat::create ] at org.opencv.objdetect.CascadeClassifier.detectMultiScale_1(Native Method) at org.opencv.objdetect.CascadeClassifier.detectMultiScale(CascadeClassifier.java:103) at FaceDetector.main (FaceDetector.java:42)
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.plaf.synth.SynthSeparatorUI;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
public class FaceDetector
public static void main(String[] args) throws IOException
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("\nRunning FaceDetector");
CascadeClassifier faceDetector = new CascadeClassifier("C:/Users/Family/workspace/detect face/haarcascade_frontalface_alt.xml");
File folder = new File("C:\\Users\\Family\\Downloads\\Photos (4)");
File[] listOfFiles = folder.listFiles();
for (int j = 0; j < listOfFiles.length; j++)
System.out.println(listOfFiles[j]);
for (int i = 0; i < listOfFiles.length-1; i++)
String picname = listOfFiles[i].toString();
System.out.println("reading: " + listOfFiles[i]);
Mat image = Imgcodecs.imread(picname);
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
for (Rect rect : faceDetections.toArray())
Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar (0, 255, 0));
Rect recta = new Rect();
for (Rect rect : faceDetections.toArray())
recta.x = rect.x;
recta.y = rect.y;
recta.width = rect.width;
recta.height = rect.height;
if (faceDetections.toArray().length == 1)
BufferedImage picture = ImageIO.read(new File(picname));
BufferedImage croppedImage = picture.getSubimage(recta.x, recta.y, recta.width, recta.height);
// File finalCropped = new File(listOfFiles[i].toString()+"cropped.jpg");
File finalCropped = new File("C:/Users/Family/Downloads/CroppedImages/" + "cropped" + (i + 140) + ".jpg");
ImageIO.write(croppedImage, "jpg", finalCropped);
System.out.println("Cropping " + i + "/" + listOfFiles.length);
faceDetections.release();
System.out.println("Process complete.");
【问题讨论】:
【参考方案1】:你也应该释放image
对象。我想建议使用 try catch finally 来正确释放。
image.release();
【讨论】:
我这样做了,程序比上次更进一步,但它在同样的错误处停止了。我只是尝试使用更短数量的图像。 好吧,什么也没做 如果循环计数可被 5 或 10 等数字整除,您可能需要调用 System.gc()。您还需要释放由 Imgcodecs.imread 加载的 Mat 对象。【参考方案2】:您是否尝试过增加堆空间?看看here。
java.lang.OutOfMemoryError: Java heap space 错误会在应用程序尝试向堆空间区域添加更多数据但没有足够空间时触发。
【讨论】:
以上是关于java opencv错误内存不足和断言失败的主要内容,如果未能解决你的问题,请参考以下文章
OPENCV / C++: approxpolydp 断言失败错误
使用大型数据结构时,避免 Java(eclipse) 中的“内存不足错误”?