Reputation: 18178
I used OpenCv and I used it in code similar to the following code:
Mat Create()
{
Mat myMat(10, 10, CV8U_C1);
int x=myMat.Rows; // I am accessing Mat like an object not a pointer.
Return myMat;
}
Main()
{
Mat aMat=Create(); // created inside this function
int x=aMat.Rows; // accessing it using . notation
// do some work
return; //I did not delete Mat, as it would release its memory.
}
How can create similar objects n my c++ code?
I am using STL, but if needed I can use Boost too.
Upvotes: 0
Views: 5157
Reputation: 11941
Your question is answered by just a quick look at the API reference
class CV_EXPORTS Mat
{
public:
// ... a lot of methods ...
...
The best thing to do would be to read the OpenCV tutorial on Mat
The first thing you need to know about Mat is that you no longer need to manually allocate its memory and release it as soon as you do not need it. ...
Mat is basically a class with two data parts: the matrix header (containing information such as the size of the matrix, the method used for storing, at which address is the matrix stored, and so on) and a pointer to the matrix containing the pixel values ...
Upvotes: 1
Reputation: 6177
The other answers are correct -- cv::Mat
is a resource-owning object like std::vector<>
that can be passed around and returned by value. However, there is one very important way in which cv::Mat
behaves like a pointer, and that's in regards to its aliasing behavior. For most types T
, the following is true:
T orig = initial_value;
T copy = orig;
mutate(copy);
assert(orig == initial_value);
That is, for most types, copies are independent. You can mutate one without affecting the other. This is not true for cv::Mat
. Instead, you have to watch out for stuff like this:
cv::Mat orig = /* ... matrix of all zeros ... */
cv::Mat copy = orig; // NOTE! Creates an alias, not a copy.
copy(2,2) = 42;
assert(orig(2,2) == 42); // Huh.
See? changing the copy changed the original. Speaking technically, cv::Mat
is not a regular type, which is a pity because most modern APIs including the STL assume that types are regular. With cv::Mat
, things silently alias, leading to hard-to-find, spooky-action-at-a-distance kinds of bugs. Use cv::Mat
with extreme care. And not ever with STL algorithms and containers.
Upvotes: 4
Reputation: 388
First of all, std::vector
, Mat
, and other data structures have destructors that deallocate the underlying memory buffers when needed. This means that the destructors do not always deallocate the buffers as in case of Mat
. They take into account possible data sharing. A destructor decrements the reference counter associated with the matrix data buffer. The buffer is deallocated if and only if the reference counter reaches zero. That is, when no other structures refer to the same buffer. Similarly, when a Mat
instance is copied, no actual data is really copied. Instead, the reference counter is incremented to memorize that there is another owner of the same data. There is also the Mat::clone()
method that creates a full copy of the matrix data.
Upvotes: 7