How to Write a Float Mat to a File in Opencv

How to write a Float Mat to a file in OpenCV

Using pure OpenCV API calls:

// Declare what you need
cv::FileStorage file("some_name.ext", cv::FileStorage::WRITE);
cv::Mat someMatrixOfAnyType;

// Write to file!
file << "matName" << someMatrixOfAnyType;

The file extension can be xml or yml.
In both cases you get a small header that you can easily remove/parse, then you have access to the data in a floating point format.
I used this approach successfully (with yml files) to get data into Matlab and Matplotlib

To get the data:

  1. open the file with any editor
  2. then suppress all the text and numbers except the content of the data tag (i.e., the pixel values).
  3. When done, save your file with a txt or csv extension and open it with matlab (drag-and-drop works).

Voilà. You may have to reshape the resulting matrix in matlab command line if it didn't guess correctly the image size.

OpenCV create Mat of float from binary

#include "opencv2/opencv.hpp"
using namespace cv;
#include <assert.h>

int main()
{
Mat floatOrig = Mat::zeros(1,64,CV_32FC1);
Mat ucharConverted = Mat::zeros(1,256,CV_8UC1);
Mat floatConverted = Mat::zeros(1,64,CV_32FC1);

//construct some data
RNG rng = theRNG();
for(int i=0;i<floatOrig.cols;++i)
{
floatOrig.at<float>(0,i)=rng.gaussian(1.);
}

//save them into uchar first
for(int i=0;i<ucharConverted.cols;++i)
{
ucharConverted.at<uchar>(0,i)= floatOrig.at<uchar>(0,i);
}

//now convert them back into float
//uchar b[4] = {0}; uncomment for big endian data
for(int i=0;i<floatConverted.cols;++i)
{
/* uncomment for big endian ordering
b[0]=ucharConverted.at<uchar>(0,i*4+3);
b[1]=ucharConverted.at<uchar>(0,i*4+2);
b[2]=ucharConverted.at<uchar>(0,i*4+1);
b[3]=ucharConverted.at<uchar>(0,i*4+0);
memcpy(&floatConverted.at<float>(0,i),&b, sizeof(float));
*/
memcpy(&floatConverted.at<float>(0,i),&ucharConverted.at<uchar>(0,i*4), sizeof(float));
}

//verify
for(int i=0;i<floatConverted.cols;++i)
{
assert(floatConverted.at<float>(0,i)-floatOrig.at<float>(0,i)==0.);
}

// now lets try saving that to file
FILE* fp = fopen("c:/data/float64.bin","wb");
for(size_t i=0;i<floatConverted.cols;++i)
{
fwrite( &floatConverted.at<float>(0,i),sizeof(float),1,fp);
}
fclose(fp);

floatConverted=0;//we gonna try to load it back

fp = fopen("c:/data/float64.bin","rb");
for(size_t i=0;i<floatConverted.cols;++i)
{
fread( &floatConverted.at<float>(0,i),sizeof(float),1,fp);
}
fclose(fp);

//verify data read from file
for(int i=0;i<floatConverted.cols;++i)
{
assert(floatConverted.at<float>(0,i)-floatOrig.at<float>(0,i)==0.);
}
getchar();
}

opencv: how to save float array as an image

You need to consturct a cv::Mat object before saving it via cv::imwrite().

cv::imwrite("~/imgOut.png",  cv::Mat(512, 512, CV_32FC1, gray));

Note: All values will be rounded up to the nearest integer in the resulted image though.

Read from Txt file and convert to float mat opencv

You can fill Mat directly without auxiliary array train_data.

for (int j = 0; j < 2; j++)
{
for (int i = 0; i < x_coord.size(); i++)
{
int index = j + i * x_coord.size();
training_data.at<float>(i, j) = x_coord.at(index);
}
}

Also you can do the same while reading file. For more info please read Mat::at() documentation.

Copy Float point data to Mat

It seems that you're trying to do something without reading documentation what is wrong way.

Why did you create Mat with 3 channels? Does your input image has 3 channels? It seems that you have to do the following:

Mat image(Size(640,480) CV_32FC1);
int k=0;
for (int i = 0; i < image.rows; ++i)
{
for (int j = 0; j < image.cols; ++j)
{
image.at<float>(i,j) = iArray[k];
++k;
}
}

How to convert an OpenCV cv::Mat into a float* that can be fed into Vlfeat vl_dsift_process ?

float* matData = (float*)myMat.data;

Make sure the matrix is not deleted/goes out of scope before finishing using the pointer to data. And make sure the matrix contain floats.



Related Topics



Leave a reply



Submit