Memory Leak from iterating Opencv frames

前端 未结 5 2093
囚心锁ツ
囚心锁ツ 2020-12-14 20:20

I am using the java wrapper of OpenCV. I tried to write an Iterator over frames of a film. My problem is that the iterator is a huge memory leak. Here is a very simplified v

5条回答
  •  太阳男子
    2020-12-14 20:52

    I would modify your .hasNext method to:

    public boolean hasNext() {
        return hasNext;
    }
    

    And then the method you described, copied below, should work fine... You will iterate until nothing is left, at which point you can assign that last image to a new Mat object...

    public Mat next() {
        capture.retrieve(mat);
        hasNext = capture.grab();
        return mat;
    }
    

    and then:

    final VideoCapture vc = new VideoCapture("/path/to/file");
    final SimpleIt it = new SimpleIt(vc);
    final Mat lastFrame = new Mat();
    while (it.hasNext) {
        lastFrame = it.next();
    }
    

    I do realize this creates additional memory usage. There is probably a way around this, but it should work fine...

提交回复
热议问题