"When the cv::Mat object goes out of scope, the memory allocated is automatically released. This is very convenient because you avoid having problems with memory leaks. Moreover, the cv::Mat class implements reference counting and shallow copy such that when an image is assigned to another one, the i"
"When the ima local variable goes out of scope, this variable is de-allocated, but since the associated reference counter indicates that its internal image data is being referred by another instance (that is the gray variable) its memory block is not released."