Fix for OpenCV issue #2815

this is a backport of #510