我正在尝试制作一个程序,该程序从网络摄像头拍摄照片,然后调整其大小,将其转换为 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;
}
感谢您给我的任何帮助,如果您需要更多信息,请尽管询问!
/杰斯珀