2

Is there anyway to convert Mat into array in opencv? (c++) I need to copy a matrix (which is output of a function) into where a pointer points to. Format of the output is Mat and pointer is float. How do I copy it?

Thanks in advance

user3641098
  • 23
  • 1
  • 5

1 Answers1

2

Mat is also two dimensional array covered by class. If you need data in it, you can access it with Mat::at(int x, int y) method. Or copy raw data:

cv::Mat matrix;
float *new_data = new float[matrix.rows*matrix.cols];
float *p = new_data;
for(int i  = 0; i < matrix.rows; i++){
    memcpy(p, matrix.ptr(i), matrix.cols*sizeof(float));
    p += matrix.cols;
}
Oleg Olivson
  • 489
  • 2
  • 5
  • 2
    Why not this?... `memcpy(p, matrix.ptr(0), matrix.rows*matrix.cols*sizeof(float));` – dividebyzero May 09 '16 at 16:45
  • 1
    @dividebyzero - You are not guaranteed to have a continuous memory matrix. You could add this: `if(!matrix.isContinuous()) matrix=matrix.clone();` and then it's fine. – Elad Joseph Apr 19 '17 at 14:51