Ask Your Question

Convert double[2048]to Mat

asked 2016-01-22 10:32:42 -0500

Guyygarty gravatar image

updated 2016-01-22 11:02:33 -0500

I have a spectrometer that receives an address and throws 2048 doubles into memory starting at that address. I would like to run it 500 times and at the end have a 500x2048 Mat file where each row corresponds to one spectrum. If the spectrometer was returning int, I would define a Mat(500,2048,CV_16UC1) and send the spectrometer the address of each row sequentially.

How do I need to define the Mat so that I can do this with doubles?

I am using windows 7/64 , Visual studio 2013 and OpenCV 2.4.9


edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2016-02-10 10:28:22 -0500

Guyygarty gravatar image

updated 2016-02-10 10:50:42 -0500

This is what I ended up using:

double Spectrum[N_SPECT][PIXEL_COUNT]; //define contiguous memory block
DLL_Get_Spectra(Spectrum[SpectrumCount], &CaseTemp);// Grab data into Spectrum[][] one row at a time
cv::Mat_<double> Image(SpectrumCount, PIXEL_COUNT, &Spectrum[0][0]);//build Mat around Spectrum[][]

It works quite well. I decided to use Mat_<double> instead of Mat(,,CV_64F) to be absolutely sure that I get the pixel type right - I was somewhat fuzzy on the exact size of a double. Why the spectrometer is returning 16 bit integers as double in the first place is beyond me.


edit flag offensive delete link more

answered 2016-01-22 11:12:32 -0500

kbarni gravatar image

Create a row Mat for each data,then use hconcat to concatenate them in a final image. It should be something like (untested code):

Mat result;
double *data;
    Mat datMat(1,2048,CV_64F,data);
imshow("Captures spectra",result);
edit flag offensive delete link more



i think you could use vconcat(result,datMat,result); or result.push_back(datMat) instead of hconcat(result,datMat,result);

sturkmen gravatar imagesturkmen ( 2016-01-22 14:32:00 -0500 )edit


I have a very strong aversion to memory allocation in loops. I believe in your solution I would either have to reallocate data or (implicitly) result at each cycle.


Guyygarty gravatar imageGuyygarty ( 2016-02-10 10:57:56 -0500 )edit

I liked your solution

sturkmen gravatar imagesturkmen ( 2016-02-10 12:25:23 -0500 )edit

Question Tools

1 follower


Asked: 2016-01-22 10:32:42 -0500

Seen: 454 times

Last updated: Feb 10 '16