Ask Your Question
1

Store mat data in txt

asked 2014-02-24 03:38:48 -0600

souraklis gravatar image

updated 2014-02-24 03:45:29 -0600

berak gravatar image

I am trying to write a mat file to txt file. I am using the above function to do so:

void writeMatToFile(cv::Mat& m, const char* filename){

ofstream fout(filename);
for(int i=0; i<m.rows; i++){
    for(int j=0; j<m.cols; j++){
        fout<<m.at<float>(i,j)<<"\t";
    }
    fout<<endl;
}
fout.close();

}

And the main code:

 string file = "output.txt";
 writeMatToFile(image,file.c_str());

However I am receiving unhandled exceptions. Any idea how can i store mat data in txt?

edit retag flag offensive close merge delete

Comments

1

m.at<float>(i,j); // is your Mat really float type ?

berak gravatar imageberak ( 2014-02-24 03:44:49 -0600 )edit

I am trying to figure out what is the type of my Mat file. I ve just used mage = imread(filename, 0); to read the file, where filename is a .jpg file.

souraklis gravatar imagesouraklis ( 2014-02-24 03:49:21 -0600 )edit

m.at<uchar>(i,j); in this case. you can't choose the type arbitrarily, it has to fit the underlying data.

berak gravatar imageberak ( 2014-02-24 03:50:46 -0600 )edit

Ok know it works without exceptions, but in the txt file, weird characters it is stored.

souraklis gravatar imagesouraklis ( 2014-02-24 03:54:22 -0600 )edit

 h < E ^ W r ; C 9 1 6 7 4 %  m      b ¦ ¦ ž † ‹ J k ] < ( / O * C ¬ Ζ Ξ Τ Μ Ή  ‚ \ V B L D - 0 /     ' Ε  ά Τ Ψ Υ Ι Ά ‚ \ B V < 4 ? ;   | Ξ Σ ά Ω Ϊ Ϊ Ρ Ζ ± d B > F 6 A ¨    „ d Τ ή ά Ϊ Χ Υ Δ ± ˜ 2 H L 9 Q ³     r p † ² Χ Ψ ‹ X ž } œ p ? a 2 V     • ~ Σ  Ε Ύ 3 (  « Ÿ n – ‰ œ    

souraklis gravatar imagesouraklis ( 2014-02-24 03:55:40 -0600 )edit

lol.

the stream sees uchar and tries to print characters ... try:

fout<<(int)m.at<uchar>(i,j)

berak gravatar imageberak ( 2014-02-24 03:56:45 -0600 )edit

Yea, thanks int casting works!

souraklis gravatar imagesouraklis ( 2014-02-24 04:08:58 -0600 )edit

You guys realize that the Mat class has builtin write and read functions associated with the "<<" and ">>" operators ? To write a matrix to a stream just do : fout << m.

Guido gravatar imageGuido ( 2014-02-24 06:58:24 -0600 )edit

@Guido, unfortunately, the reverse op does not work.

berak gravatar imageberak ( 2014-02-24 07:54:39 -0600 )edit

Idd, the read operator doesn't work unless you use opencv's xml formatting. However, if you just need to write, the stream operator is a good alternative.

Guido gravatar imageGuido ( 2014-02-24 09:25:48 -0600 )edit

1 answer

Sort by » oldest newest most voted
-2

answered 2014-02-24 04:22:43 -0600

shiv.butekar gravatar image

here i have a code that works fine in my pc let me know if u have any problem

    int main(int argc, char** argv)
    {
  Mat image;
 VideoCapture cap(0);
 cap>>image;
int i=image.type();
 cout<<i;//if it prints 16 then your data pointer is int unsigned 

 ofstream fout("file.txt");
int unsigned *ptr;
ptr=(unsigned int *)image.data;

 for(int i=0; i<(image.rows); i++)
{
    for(int j=0; j<image.cols; j++){
        fout<<*ptr<<"\t";
        ptr++;
    }
    fout<<endl;
}
fout.close();

}

edit flag offensive delete link more

Comments

... just try to reconstruct an image from your own code ....

//if it prints 16 then your data pointer is int unsigned - wrong! (Vec3b)

berak gravatar imageberak ( 2014-02-24 07:57:58 -0600 )edit

Question Tools

Stats

Asked: 2014-02-24 03:38:48 -0600

Seen: 6,024 times

Last updated: Feb 24 '14