我正在尝试制作一个从网络摄像头拍摄照片的程序,然后调整它的大小,将其转换为HSV,并对其进行一些阈值处理,以找到特定的颜色。完成此操作后,我使用阈值图像查找轮廓,并打印不同轮廓的x,y坐标。这是一遍又一遍地重复,以实时从网络摄像头进行处理。
这一切都运行得很好,除了我每运行2秒钟耗尽大约100 MB RAM的事实。
到目前为止,我发现如果我使用静态图片而不是网络摄像头的实时图像,我可以显着减少内存泄漏,尽管仍然会消耗内存。
我的代码下面是:
public class Application {
private CaptureImage ci;
private ImageUtils iu;
private CanvasFrame canvasContours;
IplImage grabbedFrame;
IplImage resizedFrame;
IplImage thresholdedFrame;
IplImage clonedImage;
public Application(){
ci = new CaptureImage();
iu = new ImageUtils();
canvasContours = new CanvasFrame("contours");
}
public void frameProcessing(){
grabbedFrame = ci.grabImage();
//below call used for testing purposes
//grabbedFrame = (IplImage) opencv_highgui.cvLoadImage("testingImage.jpg");
//cloning image due to highgui guidelines.
clonedImage = opencv_core.cvCloneImage(grabbedFrame);
resizedFrame = iu.resizeImage(clonedImage);
opencv_core.cvReleaseImage(clonedImage);
thresholdedFrame = iu.thresholdImage(resizedFrame);
IplImage contoursFrame = iu.findContours(thresholdedFrame, resizedFrame);
canvasContours.showImage(contoursFrame);
}
}
grabImage只是javacv的标准frameGrabber,如下所示:
public class CaptureImage {
private final OpenCVFrameGrabber grabber;
private IplImage img = null;
public CaptureImage(){
// 0-default camera, 1 - next...so on
grabber = new OpenCVFrameGrabber(0);
try {
grabber.start();
} catch (Exception e) {
System.err.print("Failed to initialize camera");
e.printStackTrace();
}
}
public IplImage grabImage(){
try {
//A grabbed image from Logitech webcam is in following resolution: 1200x800px
img = grabber.grab();
} catch (Exception e) {
e.printStackTrace();
}
return img;
}
感谢您提供给我的任何帮助,如果您需要更多信息,请直接询问!
/的Jesper
答案 0 :(得分:1)
从堆转储中,使用的内存是从本机代码引用的所有byte和int数组。查看您的代码,我发现您只能为克隆的图片调用cvReleaseImage
而不是原始图片。