简体   繁体   English

将Mat元素(uchar)写入.csv

[英]Writing Mat elements (uchar) to .csv

I want to output the content of a Mat object (OpenCV uchar) to a .csv file. 我想将Mat对象(OpenCV uchar)的内容输出到.csv文件。

Right now, I am able to write ints and doubles and even (it seems) the uchars but when opening the csv in Excel, the uchar are interpreted as characters so I can't inspect the values. 现在,我可以编写整数和双精度数,甚至可以编写uchar,但在Excel中打开csv时,uchar被解释为字符,因此我无法检查这些值。 So I tried to cast them to a string before saving them to csv, but it failed. 因此,在将它们保存到csv之前,我尝试将它们转换为字符串,但是失败了。

My code so far: 到目前为止,我的代码:

Ofstream Datalogger(path);
// Some code

for (int i = 0; i< vector1.size();i++)
        {
            Datalogger << i << ";"
                << vector1[i].someint << ";"     
                << vector1[i].somedouble << ";" 

            const uchar* Mi = Mat1.ptr<uchar>(i);
            for(int k = 0; k < Mat1.cols; k++)
            {               
                std::string descr( Mi[k]);
                Datalogger << descr << ";" ; 
            }        
            Datalogger << endl;
        }

But the error code is: 但是错误代码是:

1   IntelliSense: no instance of constructor "std::basic_string<_Elem, _Traits, _Alloc>::basic_string [with _Elem=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]" matches the argument list argument types are: (const uchar)

Which probably means the string descr() cannot accept uchars. 这可能意味着字符串descr()无法接受uchar。

Any idea how to log the content to .csv AND being able to see the numerical value of the uchar instead of it being interpreted as a special character by Excel? 任何想法如何将内容记录到.csv并能够查看uchar的数值,而不是由Excel解释为特殊字符?

Ok, since don't really need to have a string and only to be able to read the numerical value in Excel, I simply did a cast to an int. 好的,因为不需要真正的字符串,只需要能够在Excel中读取数字值,我就简单地将其强制转换为int。

for (int i = 0; i< vector1.size();i++)
        {
            Datalogger << i << ";"
                << vector1[i].someint << ";"     
                << vector1[i].somedouble << ";" 

            const uchar* Mi = Mat1.ptr<uchar>(i);
            for(int k = 0; k < Mat1.cols; k++)
            {               
                int descr = int( Mi[k]);
                Datalogger << descr << ";" ; 
            }        
            Datalogger << endl;
        }

cv::format() may be also useful: cv :: format()可能也有用:

cv::Mat M = cv::Mat::ones(3, 3, CV_8UC3);
std::cout << cv::format(M, "csv") << std::endl;

There are also some other options to print/format mat to the output: 还有一些其他选项可以将输出打印/格式化为输出:
http://docs.opencv.org/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html#output-formatting http://docs.opencv.org/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html#output-formatting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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