CvMat, CvSeq and CvArr Data Structure


CvMat is multi-dimensional array structure, CvSeq a sequence of points, CvArr is their super structure.

  • CvArr is used only a function parameter to specify that the function accepts arrays of more than a single type, for example IplImage*, CvMat * or even CvSeq *.
  • CvMat Multi-dimensional matrix

Example: Calculating Product of Two Matrices

   double a[] = { 1, 2, 3, 4
                  5, 6, 7, 8,
                  9, 10, 11, 12 };

   double b[] = { 1, 5, 9,
                  2, 6, 10,
                  3, 7, 11,
                  4, 8, 12 };

   double c[9];
   CvMat Ma, Mb, Mc ;

   cvInitMatHeader( &Ma, 3, 4, CV_64FC1, a );
   cvInitMatHeader( &Mb, 4, 3, CV_64FC1, b );
   cvInitMatHeader( &Mc, 3, 3, CV_64FC1, c );

   cvMatMulAdd( &Ma, &Mb, 0, &Mc );
   // c array now contains product of a(3x4) and b(4x3) matrices
  • CvSeq is a base for all of OpenCV dynamic data structures.
functions:

Compute Bounding Boxes:

CvRect cvBoundingRect( CvArr* points, int update=0)
    points: 2D point set, either a sequence (CvContour, CvSeq) or vector (CvMat) of points

Example:

CvSeq *contour;
double area;
CvRect rect;

cvFindContours(img, storage, &contour, size(CvContour),
    CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
//go through each contour in the contours and compute their area as well as
//bounding box
while(contours!=NULL){
    area = fabs(cvContourArea(contour, CV_WHOLE));
    rect = cvBoundingRect(contour);
    cout << "area: " << area << ", bounding box width/height: " << rect.width <<
         "/" << rect.height << endl;
    contour = contour->h_next; //get next contour
}