Reputation: 69
UMat frame,gray;
VideoCapture cap(0);
if(!cap.isOpened())
return -1;
for(i=0;i<10;i++)
{
cap >> frame;
Canny(frame, frame, 0, 50);
imshow("canny", frame);
}
return 0;
here my doubt is that if the loop is running for 10 times and in line-11 I am applying the canny filter, but the src and dst are same(frame) so it will be inplace operation, so at each iteration what will happen in case of the memory allocations and deallocation!!
when the canny filter is applied will the result data replace the old matrix data, or it will allocate a new set of memory for the result data and pointing to it, and if so what will happen to the old matrix data?
Upvotes: 1
Views: 190
Reputation: 207510
The following line:
UMat frame
does not allocate any significant image memory. It just creates a header on the stack with space for:
On entry to the loop, the following line:
cap >> frame;
will allocate sufficient memory on the heap for the image's pixels, and initialise the dimensions, the reference count and make the data pointer point to the allocated chunk of image memory - obviously it will also fill the pixel-data from the video source.
When you call Canny with:
Canny(frame, frame, 0, 50);
it will see that the operation is in-place and re-use the same Mat that contains frame
and over-write it. No allocation, nor releasing is necessary.
The second, and subsequent, times you go around the loop, the line:
cap >> frame;
will see that there is already sufficient space allocated and load the data from the video stream into the same Mat
, thereby over-writing the results of the previous Canny()
.
When you return from the function at the end, the heap memory for the pixel data is released and the stack memory for the header is given up.
TLDR; There is nothing to worry about - memory allocation and releasing are taken care of for you!
Upvotes: 1