Ask Your Question
1

cv::format using locale decimal mark

asked 2016-05-29 09:28:47 -0600

Benjamin M gravatar image

I'm using OpenCV 3.1 from C++ and I want to store my matrix in Python format:

errfile.open("errs.txt");
errfile << cv::format(errs, cv::Formatter::FMT_PYTHON) << std::endl;
errfile.close();

(errs is of type cv::Mat)

But the output is:

[[1271892,725376768, 65449,68687547074, 9149,382398115458, ... ]]

It seems like OpenCV is using my systems locale number format, using a comma as decimal separator. But Python expects a dot.

Although doing:

errfile << 6.66f << std::endl;

does print "6.66" not "6,66". Using errfile.embue to set the locale for the steam doesn't help either. I have to set the global locale:

std::setlocale(LC_NUMERIC, "en_US.UTF-8");

I don't think it makes sense to apply the locale here, since the format should be the python code format. Is this a bug I should report?

Additional Info: My system uses en_US locale for language, but German (de_AT) locale for numbers.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-05-30 05:15:08 -0600

mshabunin gravatar image

Formatter uses sprintf to print numbers and it uses application locale (modified by setlocale). Probably it can be rewritten using ostringstream and explicit locale setting.

Feel free to provide pull request ^_^

edit flag offensive delete link more

Comments

TY; Looked at opencv/modules/core/src/out.cpp For sprintf, there is no other way than setting the whole applications locale, which isn't safe. I have some time in a few weeks, maybe I can do it then. I'd love to contribute. Will only have to see, whether locale awerness isn't needed for some cases (e.g., default and CSV). Maybe it should be the default, with changing the format for the pogramming language formats only.

Benjamin M gravatar imageBenjamin M ( 2016-05-30 07:24:29 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-05-29 09:28:47 -0600

Seen: 532 times

Last updated: May 30 '16