Rewrite Ptr

This replaces OpenCV's Ptr with a rewritten version. The new version is safer, as well as more flexible, but is, unfortunately incompatible in some ways.

The bulk of the changes in this PR are trivial adaptations of the OpenCV code to the new interface. Advice for reviewers: look at individual commits and not at the whole thing.