curryage
curryage

Reputation: 491

Reassign to Mat in OpenCV

I have the following function which extracts a sub-image from an OpenCV cv::Mat

void Process(int a,int b,int c,int d)
{
    // Extract img(a:b,c:d) each time         
    subImg = img(cv::Range(a, b), cv::Range(c,d));
}

I call Process() in a loop. On each invocation, the values of a,b,c,d keep changing. If subImg has been declared as cv::Mat subImg;, can I do the above ? i.e. Can OpenCV dynamically resize subImg or do I have to go for a pointer-based approach where I declare:

cv::Mat* subImg; // Initialized to NULL in constructor

and modify the function as follows:

void Process(int a,int b,int c,int d)
{
    // Extract img(a:b,c:d) each time         
    if(subImg) delete subImg;        
    subImg = img(cv::Range(a, b), cv::Range(c,d)).clone();
}

Upvotes: 0

Views: 318

Answers (1)

vinograd47
vinograd47

Reputation: 6420

You can do this with cv::Mat subImg;. cv::Mat uses reference counting, and sub-matrix knows that it belongs to bigger matrix, so memory will be deallocated properly.

Upvotes: 1

Related Questions