videocapture reads the same frame differently on multiple calls
Hi, I am new to the forum. I have started using Opencv with Java a few weeks ago. In my program I use videocapture to read frames from an avi file. Yesterday I have noticed that, over different runs of my progrem, the n-th frame read from the file is different from the same n-th frame read from the same file, in a previous run. I have confirmed this by subtracting two copies of the same frame, after resetting the video capture to the same frame number. Is this expected, or am I doing something wrong?
for (int j = 0; j < 100; j++){
Mat img = new Mat();
Mat m1 = new Mat();
Mat m2 = new Mat();
VideoCapture vc = new VideoCapture(filename);
vc.set(Videoio.CAP_PROP_POS_FRAMES, 0);
for (int i = 0; i < 9; i++) vc.grab();
vc.read(img);
m1 = img.clone();
Imgproc.cvtColor(m1, m1, Imgproc.COLOR_BGR2GRAY);
vc.release();
vc = new VideoCapture(filename);
vc.set(Videoio.CAP_PROP_POS_FRAMES, 0);
for (int i = 0; i < 9; i++) vc.grab();
vc.read(img);
m2 = img.clone();
Imgproc.cvtColor(m2, m2, Imgproc.COLOR_BGR2GRAY);
vc.release();
Mat m3 = new Mat();
Core.absdiff(m1, m2, m3);
if (Core.countNonZero(m3) > 0)System.out.println(Core.sumElems(m3));
}
What if using 2 different capture obj like
VideoCapture vc1 = new VideoCapture(filename)
andVideoCapture vc2 = new VideoCapture(filename)
?You should put both
grab()
andread()
in the for loop and check if they returnstrue
.PS:
please provide a bit of your code..please next time update your question instead of add code using commentsIn this code I am interested in extracting only the first frame from the video. The "grab" calls are in the two inner loops to make sure to exhaust what it seems to be a frame buffer in the VideoCapture object; the buffer size is 9 in my case, although I am not sure why that is. The outer loop is just to test the overall program multiple times. The output of the System.out is occasionally non-zero, for example 5-6 times over the 100 loop iterations. Instead I would expect it to be always zero, as essentially the program is reading the same first frame twice and, as such, the output of the absdiff should be zero.