Fixed bugs in capturing of image sequences.

Adapted old pull request changes.
See http://code.opencv.org/issues/3306