Announce

PukiWiki contents have been moved into SONOTS Plugin (20070703)

OpenCV Snippets

Table of Contents

cvReshape for vector transpose is smarter than cvTranspose

cvMat* matT, hdr;
matT = cvReshape( mat, &hdr, CV_MAT_CN(mat->type), mat->cols );

is smarter than

CvMat* matT = cvCreateMat( mat->cols, mat->rows, mat->types );
cvTranpose( mat, matT );
cvReleaseMat( &matT );

because cvReshape does not require memory allocation.

But, this technique is available only for a vector (not available for a vector obtained from cvGetRow or cvGetCols too because memory is not consecuitive)

cvQueryFrame may give flipped image

under windows under less than or equal to OpenCV 1.0

if( img = cvQueryFrame( cap ) )
{
#if (defined(WIN32) || defined(WIN64)) && (CV_MAJOR_VERSION < 1 || (CV_MAJOR_VERSION == 1 && CV_MINOR_VERSION < 1))
        img->origin = 0;
        cvFlip( img );
#endif
}

cvScale( mat, mat, scale ) would cause an error

It would looks as working in most cases, but it in fact results in an error. Do as

CvMat *sub_mat = cvCreateMat( mat->rows, mat->cols, mat->type );
cvScale( mat, sub_mat, scale );

tell me why?

cvReshape does not require memory allocation

CvMat* mat, hdr;
mat = cvReshape( orig, &hdr, nChannel, nRow );

Address of mat refers the address of the original.

Mostly, functions whose interfaces are as

CvMat* cvXxx( const CvMat*, &hdr )

does not require memory allocation such as cvGetRow, cvGetCols.

cvSetRow via cvGetRow

CvMat *row, hdr;
row = cvGetRow( mat, &hdr, 1 );
cvCopy( another_row, row );
// mat also modified.