简体   繁体   中英

OpenCV Construct Mat from float* array

I am trying to use the OpenCV Mat::Mat constructor with the option of passing in data.

I have read on this forum that you can pass in a float pointer to an array, but every time I do that it tries to pass in the address (I think that is what is happening) rather than the actual values from the array.

I have tried two different way of creating the float array:

float *outputimg = (float*)malloc(img1.rows*img1.cols*sizeof(float));

and

float *outputimg = (float*)malloc(img1.step*img1.elemSize());

then I have tried the Mat constructor as follows:

Mat contrastimg(img1.rows, img1.cols, CV_32F, outputimg, img1.step);

Mat contrastimg(img1.rows, img1.cols, CV_32F, outputimg);

Mat contrastimg(img1.rows, img1.cols, CV_32F, outputimg, img1.cols*sizeof(float));

none of these are working for me. Any ideas?

I think, the best way to do what you need is to first convert your pointer to CvMat matrix and then construct a Mat object from it. It should look something like this:

CvMat* m = new CvMat;
m->type = yourType;
m->data.fl = outputimg;
m->rows = img1.rows;
m->cols = img1.cols;

Mat matrix(m);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM