cv::Ptr of Mat[]: How?

asked 2014-11-17 08:14:17 -0500

meleagrosy gravatar image

Hello, I am new to OpenCV and would have the following question:

I have several methods which have to be applied to 31 feature channels separately. Thus, I wanted to use an array of Mats:

Mat features[31];

Obviously, this array is only useable in the current scope. I would like to return it with automatic memory management:

Ptr<Mat> matPtr = new Mat[31];
matPtr[0] = M1;
matPtr[1] = M2;
matPtr[2] = M3;

This does not work and stops with an assertion. What is the correct syntax to achieve automatic memory management with arrays of matrices?


struct Features

{ Mat channels[3]; };

int main(int, char**) {

Ptr<features> matPtr = new Features();

    Mat M(3, 3, CV_8UC3, Scalar(0, 0, 255));
    matPtr->channels[0] = M;
    matPtr->channels[1] = M;
    matPtr->channels[2] = M;

std::cout << matPtr->channels[0] << endl;
return 0;


seems to work. Is this code correct?

edit retag flag offensive close merge delete