我正在使用OpenCV的java包装器.我试图在电影的帧上写一个迭代器.我的问题是迭代器是一个巨大的内存泄漏.这是迭代器的一个非常简化的版本,它有这个漏洞:
public static final class SimpleIt implements Iterator<Mat> {
private final VideoCapture capture;
boolean hasNext;
public SimpleIt(final VideoCapture capture) {
this.capture = capture;
hasNext = capture.grab();
}
@Override
public boolean hasNext() {
return hasNext;
}
@Override
public Mat next() {
final Mat mat = new Mat();
capture.retrieve(mat);
hasNext = capture.grab();
return mat;
}
}
Run Code Online (Sandbox Code Playgroud)
我使用这个循环迭代这个代码:
final VideoCapture vc = new VideoCapture("/path/to/file");
final SimpleIt it = new SimpleIt(vc);
while (it.hasNext) {
it.next();
}
Run Code Online (Sandbox Code Playgroud)
只是迭代会增加线性内存消耗.我看到问题是next() - Method中的第一行.它总是创造一个新的垫子.但是只谈到java,只要迭代代码迭代到下一个图像,这个Mat就会超出范围.
我可以通过不每次使用新的Mat来克服这个问题,但是总是覆盖相同的Mat-Object,如下所示:
private final VideoCapture capture; …Run Code Online (Sandbox Code Playgroud) 我必须读取C#流的一些字节,然后将流传递给库方法,该方法应该读取其余部分.不幸的是,这个方法调用了一个stream.Seek(0,SeekOrigin.Begin).因此,它将读取它无法理解的第一个字节.
我可以将流的开头重置为当前位置吗?我不想将整个流的其余部分复制到MemoryStream中,因为它可能非常大.