anraT
anraT

Reputation: 543

Transform 2D Mat object into 1D array

I would like to transform a mat object m2 which size is 100x100 to a double row array like:

double[] matrizvector=new double[10000];

int mm=0;
for (int nr=0; nr<100; nr++){ 
    for (int nc=0; nc<100; nc++){ 
        matrizvector[mm]=m2.get(nr,nc)[0];
        mm=mm+1;
    }
}

Is there other way to do it, for example using reshape?

Upvotes: 3

Views: 2692

Answers (1)

remi
remi

Reputation: 3988

You can do it with a combination of reshape and convertTo functions:

Mat reshaped = m2.reshape(1,1);
Mat reshapedInDouble;
reshaped.convertTo(reshapedInDouble, CV_64F);

double* matrixzvector = (double*)(reshapedInDouble.data);

If your original matrix is already a matrix of double, you dont need to use convertTo.

Upvotes: 2

Related Questions