Mikael S.
Mikael S.

Reputation: 1015

OpenCV create Mat of float from binary

I have a binary Mat (1x256) (CV_8UC1) with 256 bytes. I need to create another Mat (CV_32F) of floats with 1x64 dimension out of it. Meaning that each 4 bytes construct a float value in result matrix.

Is there any way to do that in OpenCV? Or any other C++ way?

Upvotes: 2

Views: 4106

Answers (1)

Zaw Lin
Zaw Lin

Reputation: 5708

#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();
}

Upvotes: 4

Related Questions