difference is that both p and q know that they are each one of two pointers. Should p disappear (such as by
falling out of scope), q knows that it is the only remaining reference to the original matrix. If q should then
disappear and its destructor is called (implicitly), q will know that is the last one left, and that it should
deallocate the original matrix. You can think of this like the last person out of a building being responsible
for turning out the lights and locking the door (and in this case, burning the building to the ground as well).
The cv::Ptr<> template class supports several additional functions in its interface related to the
reference counting functionality of the smart pointer. Specifically, the functions addref() and
release() increment and decrement the internal reference counter of the pointer. These are relatively
dangerous functions to use, but are available in case you need to micromanage the reference counters
yourself.
There is also a function empty(), which can be used to determine if a smart pointer is pointing to an
object that has been deallocated. This could happen if you called release() on the object one or more
times. In this case, you would still have a smart pointer around, but the object pointed to might already
have been destroyed. There is a second