Bugfix 3186 second

As mentioned in bugfix 3186 (http://code.opencv.org/issues/3186)

Tried to incorporate remarks made in last pull request, which got merged but then reverted back due to breaking stuff down. (https://github.com/Itseez/opencv/pull/1376)