This is known behaviour and sadly there is no straightforward way of solving it. There is also no way of telling OpenCV to always flush the buffer, because that depends on the manufacturer of the camera. So there are several solutions to this problem.
- Install the development API of the camera provided and use the developer settings to put the camera on a buffer lenght of a single frame. Most camera's with a buffer do support this.
- If your camera does not support this, then use OpenCV to discard the buffer size, before grabbing a new frame. This can be done with a simple for loop, which is a loop simply reading headers of frames, calling them from buffer but immediatly discarding them, your loop should for example run 50 times before using the frame that was lastly captured.
The overhead on the second approach is so minimal that it can be ignored in most cases.