3

I was testing around with OpenCV matrices and the display function and had this bug. It took me more than half a day to reveal it:

I originally tried to display OpenCV matrices regardless of the type of matric e.g. CvMat or Mat, ... with a display method recommended by Mr vasile from another post of mine Multi channel Mat display function

The display method simply fetches all data of the matrix to cout stream

this is my program:

// First: CV_32FC3 works OK

float objpts[12] = {0, 105, 105, 0, 0, 0, 105, 105, 0, 0, 0, 0};
CvMat objptsmat = cvMat( 1, 4, CV_32FC3, objpts);  
CvMat* objectPoints = &objptsmat;
CvMatShow(objectPoints);
getchar();

output:

enter image description here

// Second: CV_64FC3 crashes

float objpts[12] = {0, 105, 105, 0, 0, 0, 105, 105, 0, 0, 0, 0};
CvMat objptsmat = cvMat( 1, 4, CV_64FC3, objpts);  
CvMat* objectPoints = &objptsmat;
CvMatShow(objectPoints);
getchar();

output: enter image description here

they should be both the same. Right??!!

Community
  • 1
  • 1
TSL_
  • 2,049
  • 3
  • 34
  • 55

1 Answers1

6

In the second example, you should have the array declared as

double objpts[12] = {0, 105, 105, 0, 0, 0, 105, 105, 0, 0, 0, 0};

You can read CV_xxtCn as

  • xx: number of bits
  • t: type (F = floating point type, S = signed integer, U = unsigned integer)
  • n: number of channels
bjoernz
  • 3,852
  • 18
  • 30
  • thank you!! so when array is float(double), CvMat should be CV_32F(CV_64F) and I've tested both cases. This is more like strictly compatible, float cannot go with CV_64F and vice versa – TSL_ Aug 30 '12 at 07:50
  • Computers are dumb, so you need to tell them exactly what type to expect. When you tell `CvMat` that it should read 12 times 64 bits, but you only supply 12 times 32 bits, then your program will read/write memory, that it should not access. – bjoernz Aug 30 '12 at 08:28