Sarah Sami
Sarah Sami

Reputation: 577

iterate over frames of video in openCV java

I am trying to loop on frames video in openCV java here is my code

    Mat frame = new Mat();
    int numOfFrames = 0;
    boolean hasNext = true;
    while(hasNext){
        hasNext = video.read(frame);
        numOfFrames++;
    }

the problem is , it loops forever with no result or error or memory leak ! any help please

Upvotes: 2

Views: 1879

Answers (1)

user_loser
user_loser

Reputation: 871

The while loop you coded needs some type of condition to break out the loop. As the code is now the while loop loops forever apparently.

So for instance you could do something like this to avoid an endless loop.

    boolean hasNext = true;
    while(hasNext) { 
        hasNext = video.read(frame);
        numOfFrames++;

        if(numOfFrames == 100) { 
              break;  // calling this will break out of the loop
        }
   } // end while loop

or you could simply add a break; but it will only iterate one time. Maybe a for loop is a better option? :D

Hope this helps.

That is cool there is a java wrapper for OpenCV. I have seen this program in a C programming book.

Upvotes: 1

Related Questions