Ask Your Question
0

cv::format does not format the way it should.

asked 2013-09-23 06:37:30 -0500

JNB gravatar image

updated 2014-03-31 03:49:16 -0500

Andrey Pavlenko gravatar image

I started to use cv::format() a while ago to format my data in a CSV like text stream, and noticed it worked oddely : no problem of importance when used on a N * P cv::Mat where P is greater or equal to 2. The only matter then would be the useless spaces before each row but the first, but we can trim them afterwards easily.

But when P is equal to 1 (the matrix is a column), cv::format() does not seem to add the end of line signals between lines and I end up with a single line full of characters with no separation in between.

Here is an exemple, where I fill up two matrices and display them before and after using cv::format() :

#include <iostream>
#include "opencv2/opencv.hpp"

using namespace std;

int main()
{
  int dimensions = 5;
  int sampleCount = 4;

  cv::Mat points(sampleCount,dimensions, CV_32F,cv::Scalar(10));
  cout<<points<<endl;
  cout<<cv::format(points,"csv")<<endl;

  cv::Mat points2(sampleCount,1, CV_32F,cv::Scalar(10));
  cout<<points2<<endl;
  cout<<cv::format(points2,"csv")<<endl;

  return 0;
}

The output given is :

[10, 10, 10, 10, 10;
  10, 10, 10, 10, 10;
  10, 10, 10, 10, 10;
  10, 10, 10, 10, 10]
10, 10, 10, 10, 10
  10, 10, 10, 10, 10
  10, 10, 10, 10, 10
  10, 10, 10, 10, 10

[10; 10; 10; 10]
10101010

The extra spaces in the first case seem to come from the << operator which gives a nicer display since it displays a bracket on first line.

As for the second and most important case, I don't know where it comes from but I think this should be fixed.

Note the "csv" format seems not to be the only one with this kind of output, I tried with "python" and got a similar [10101010] output.

edit retag flag offensive close merge delete

Comments

yea, odd. it's slightly different in master (you'd have to use Formatter::FMT_CSV instead of "csv"), and get at least spaces between the numbers, but still, no commas.

berak gravatar imageberak ( 2013-09-23 08:08:49 -0500 )edit

I can't seem to make your method work... OpenCV won't recognize FMT_CSV constant... (my version is 2.4.1)

JNB gravatar imageJNB ( 2013-09-24 03:07:51 -0500 )edit
1

oh, sorry for the confusion. i was just trying master version(3.0), to see if something changed there. with 2.4.2, i get exactly your results.

berak gravatar imageberak ( 2013-09-24 04:09:31 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-11-14 09:55:58 -0500

JNB gravatar image

I finally got into the code to understand why this bug occurred, and I think I figured it out, finally. But I don't know how to submit it so everybody's opencv is fixed in next release.

The problem was in file /ROOT/modules/core/src/out.cpp, at lines 151, 168, 190, 207, and 226, when calling the writeMat() method. The last parameter called singleLine is a Boolean which is true if the data matrix has only one line, so as not to print the final \n if it is the case.

But each time this function is called, the number of colons is given instead of the number of lines !

therefore the correction of the code would be changing writeMat(out, m, ';', ' ', m.cols == 1); into writeMat(out, m, ';', ' ', m.rows == 1); or similar process at each of the lines given above.

I tested it and it works fine !

edit flag offensive delete link more

Comments

Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2014-03-31 04:18:55 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-09-23 06:37:30 -0500

Seen: 2,026 times

Last updated: Mar 31 '14