Bugfix 3186 new

Tried applying fix in earlier pull request (https://github.com/Itseez/opencv/pull/1464#issuecomment-25147689), but there was still a fault after merging. Hoping to fix this now.