?? cxarray.cpp
字號:
if( CV_IS_MAT_HDR(arr) || CV_IS_MATND_HDR(arr) || CV_IS_SPARSE_MAT_HDR(arr))
{
type = CV_MAT_TYPE( ((CvMat*)arr)->type );
}
else if( CV_IS_IMAGE(arr))
{
IplImage* img = (IplImage*)arr;
type = CV_MAKETYPE( icvIplToCvDepth(img->depth), img->nChannels );
}
else
CV_ERROR( CV_StsBadArg, "unrecognized or unsupported array type" );
__END__;
return type;
}
/****************************************************************************************\* Conversion to CvMat or IplImage *\****************************************************************************************/// convert array (CvMat or IplImage) to CvMatCV_IMPL CvMat*cvGetMat( const CvArr* array, CvMat* mat, int* pCOI, int allowND ){ CvMat* result = 0; CvMat* src = (CvMat*)array; int coi = 0; CV_FUNCNAME( "cvGetMat" ); __BEGIN__; if( !mat || !src ) CV_ERROR( CV_StsNullPtr, "NULL array pointer is passed" ); if( CV_IS_MAT_HDR(src)) { if( !src->data.ptr ) CV_ERROR( CV_StsNullPtr, "The matrix has NULL data pointer" ); result = (CvMat*)src; } else if( CV_IS_IMAGE_HDR(src) ) { const IplImage* img = (const IplImage*)src; int depth, order; if( img->imageData == 0 ) CV_ERROR( CV_StsNullPtr, "The image has NULL data pointer" ); depth = icvIplToCvDepth( img->depth ); if( depth < 0 ) CV_ERROR_FROM_CODE( CV_BadDepth ); order = img->dataOrder & (img->nChannels > 1 ? -1 : 0); if( img->roi ) { if( order == IPL_DATA_ORDER_PLANE ) { int type = depth; if( img->roi->coi == 0 ) CV_ERROR( CV_StsBadFlag, "Images with planar data layout should be used with COI selected" ); CV_CALL( cvInitMatHeader( mat, img->roi->height, img->roi->width, type, img->imageData + (img->roi->coi-1)*img->imageSize + img->roi->yOffset*img->widthStep + img->roi->xOffset*CV_ELEM_SIZE(type), img->widthStep )); } else /* pixel order */ { int type = CV_MAKETYPE( depth, img->nChannels ); coi = img->roi->coi; if( img->nChannels > CV_CN_MAX ) CV_ERROR( CV_BadNumChannels, "The image is interleaved and has over CV_CN_MAX channels" ); CV_CALL( cvInitMatHeader( mat, img->roi->height, img->roi->width, type, img->imageData + img->roi->yOffset*img->widthStep + img->roi->xOffset*CV_ELEM_SIZE(type), img->widthStep )); } } else { int type = CV_MAKETYPE( depth, img->nChannels ); if( order != IPL_DATA_ORDER_PIXEL ) CV_ERROR( CV_StsBadFlag, "Pixel order should be used with coi == 0" ); CV_CALL( cvInitMatHeader( mat, img->height, img->width, type, img->imageData, img->widthStep )); } result = mat; } else if( allowND && CV_IS_MATND_HDR(src) ) { CvMatND* matnd = (CvMatND*)src; int i; int size1 = matnd->dim[0].size, size2 = 1; if( !src->data.ptr ) CV_ERROR( CV_StsNullPtr, "Input array has NULL data pointer" ); if( !CV_IS_MAT_CONT( matnd->type )) CV_ERROR( CV_StsBadArg, "Only continuous nD arrays are supported here" ); if( matnd->dims > 2 ) for( i = 1; i < matnd->dims; i++ ) size2 *= matnd->dim[i].size; else size2 = matnd->dims == 1 ? 1 : matnd->dim[1].size; mat->refcount = 0; mat->hdr_refcount = 0; mat->data.ptr = matnd->data.ptr; mat->rows = size1; mat->cols = size2; mat->type = CV_MAT_TYPE(matnd->type) | CV_MAT_MAGIC_VAL | CV_MAT_CONT_FLAG; mat->step = size2*CV_ELEM_SIZE(matnd->type); mat->step &= size1 > 1 ? -1 : 0; icvCheckHuge( mat ); result = mat; } else { CV_ERROR( CV_StsBadFlag, "Unrecognized or unsupported array type" ); } __END__; if( pCOI ) *pCOI = coi; return result;}// convert array (CvMat or IplImage) to IplImageCV_IMPL IplImage*cvGetImage( const CvArr* array, IplImage* img ){ IplImage* result = 0; const IplImage* src = (const IplImage*)array; CV_FUNCNAME( "cvGetImage" ); __BEGIN__; int depth; if( !img ) CV_ERROR_FROM_CODE( CV_StsNullPtr ); if( !CV_IS_IMAGE_HDR(src) ) { const CvMat* mat = (const CvMat*)src; if( !CV_IS_MAT_HDR(mat)) CV_ERROR_FROM_CODE( CV_StsBadFlag ); if( mat->data.ptr == 0 ) CV_ERROR_FROM_CODE( CV_StsNullPtr ); depth = cvCvToIplDepth(mat->type); cvInitImageHeader( img, cvSize(mat->cols, mat->rows), depth, CV_MAT_CN(mat->type) ); cvSetData( img, mat->data.ptr, mat->step ); result = img; } else { result = (IplImage*)src; } __END__; return result;}// Assigns external data to arrayCV_IMPL voidcvSetData( CvArr* arr, void* data, int step ){ CV_FUNCNAME( "cvSetData" ); __BEGIN__; int pix_size, min_step; if( CV_IS_MAT_HDR(arr) || CV_IS_MATND_HDR(arr) ) cvReleaseData( arr ); if( CV_IS_MAT_HDR( arr )) { CvMat* mat = (CvMat*)arr; int type = CV_MAT_TYPE(mat->type); pix_size = CV_ELEM_SIZE(type); min_step = mat->cols*pix_size & ((mat->rows <= 1) - 1); if( step != CV_AUTOSTEP ) { if( step < min_step && data != 0 ) CV_ERROR_FROM_CODE( CV_BadStep ); mat->step = step & ((mat->rows <= 1) - 1); } else { mat->step = min_step; } mat->data.ptr = (uchar*)data; mat->type = CV_MAT_MAGIC_VAL | type | (mat->step==min_step ? CV_MAT_CONT_FLAG : 0); icvCheckHuge( mat ); } else if( CV_IS_IMAGE_HDR( arr )) { IplImage* img = (IplImage*)arr; pix_size = ((img->depth & 255) >> 3)*img->nChannels; min_step = img->width*pix_size; if( step != CV_AUTOSTEP && img->height > 1 ) { if( step < min_step && data != 0 ) CV_ERROR_FROM_CODE( CV_BadStep ); img->widthStep = step; } else { img->widthStep = min_step; } img->imageSize = img->widthStep * img->height; img->imageData = img->imageDataOrigin = (char*)data; if( (((int)(size_t)data | step) & 7) == 0 && cvAlign(img->width * pix_size, 8) == step ) { img->align = 8; } else { img->align = 4; } } else if( CV_IS_MATND_HDR( arr )) { CvMatND* mat = (CvMatND*)arr; int i; int cur_step; if( step != CV_AUTOSTEP ) CV_ERROR( CV_BadStep, "For multidimensional array only CV_AUTOSTEP is allowed here" ); mat->data.ptr = (uchar*)data; cur_step = CV_ELEM_SIZE(mat->type); for( i = mat->dims - 1; i >= 0; i-- ) { if( cur_step > INT_MAX ) CV_ERROR( CV_StsOutOfRange, "The array is too big" ); mat->dim[i].step = (int)cur_step; cur_step *= mat->dim[i].size; } } else { CV_ERROR( CV_StsBadArg, "unrecognized or unsupported array type" ); } __END__;}// Retrieves essential information about image ROI or CvMat dataCV_IMPL voidcvGetRawData( const CvArr* arr, uchar** data, int* step, CvSize* roi_size ){ CV_FUNCNAME( "cvGetRawData" ); __BEGIN__; if( CV_IS_MAT( arr )) { CvMat *mat = (CvMat*)arr; if( step ) *step = mat->step; if( data ) *data = mat->data.ptr; if( roi_size ) *roi_size = cvGetMatSize( mat ); } else if( CV_IS_IMAGE( arr )) { IplImage* img = (IplImage*)arr; if( step ) *step = img->widthStep; if( data ) CV_CALL( *data = cvPtr2D( img, 0, 0 )); if( roi_size ) { if( img->roi ) { *roi_size = cvSize( img->roi->width, img->roi->height ); } else { *roi_size = cvSize( img->width, img->height ); } } } else if( CV_IS_MATND( arr )) { CvMatND* mat = (CvMatND*)arr; if( !CV_IS_MAT_CONT( mat->type )) CV_ERROR( CV_StsBadArg, "Only continuous nD arrays are supported here" ); if( data ) *data = mat->data.ptr; if( roi_size || step ) { int i, size1 = mat->dim[0].size, size2 = 1; if( mat->dims > 2 ) for( i = 1; i < mat->dims; i++ ) size1 *= mat->dim[i].size; else size2 = mat->dim[1].size; if( roi_size ) { roi_size->width = size2; roi_size->height = size1; } if( step ) *step = size1 == 1 ? 0 : mat->dim[0].step; } } else { CV_ERROR( CV_StsBadArg, "unrecognized or unsupported array type" ); } __END__;}// Returns pointer to specified element of array (linear index is used)CV_IMPL uchar*cvPtr1D( const CvArr* arr, int idx, int* _type ){ uchar* ptr = 0; CV_FUNCNAME( "cvPtr1D" ); __BEGIN__; if( CV_IS_MAT( arr )) { CvMat* mat = (CvMat*)arr; int type = CV_MAT_TYPE(mat->type); int pix_size = CV_ELEM_SIZE(type); if( _type ) *_type = type; // the first part is mul-free sufficient check // that the index is within the matrix if( (unsigned)idx >= (unsigned)(mat->rows + mat->cols - 1) && (unsigned)idx >= (unsigned)(mat->rows*mat->cols)) CV_ERROR( CV_StsOutOfRange, "index is out of range" ); if( CV_IS_MAT_CONT(mat->type)) { ptr = mat->data.ptr + (size_t)idx*pix_size; } else { int row, col; if( mat->cols == 1 ) row = idx, col = 0; else row = idx/mat->cols, col = idx - row*mat->cols; ptr = mat->data.ptr + (size_t)row*mat->step + col*pix_size; } } else if( CV_IS_IMAGE_HDR( arr )) { IplImage* img = (IplImage*)arr; int width = !img->roi ? img->width : img->roi->width; int y = idx/width, x = idx - y*width; ptr = cvPtr2D( arr, y, x, _type ); } else if( CV_IS_MATND( arr )) { CvMatND* mat = (CvMatND*)arr; int j, type = CV_MAT_TYPE(mat->type); size_t size = mat->dim[0].size; if( _type ) *_type = type; for( j = 1; j < mat->dims; j++ ) size *= mat->dim[j].size; if((unsigned)idx >= (unsigned)size ) CV_ERROR( CV_StsOutOfRange, "index is out of range" ); if( CV_IS_MAT_CONT(mat->type)) { int pix_size = CV_ELEM_SIZE(type); ptr = mat->data.ptr + (size_t)idx*pix_size; } else { ptr = mat->data.ptr; for( j = mat->dims - 1; j >= 0; j-- ) { int sz = mat->dim[j].size; if( sz ) { int t = idx/sz; ptr += (idx - t*sz)*mat->dim[j].step; idx = t; } } } } else if( CV_IS_SPARSE_MAT( arr )) { CV_ERROR( CV_StsBadArg, "do not support sparse mat now" ); } else { CV_ERROR( CV_StsBadArg, "unrecognized or unsupported array type" ); } __END__; return ptr;}// Returns pointer to specified element of 2d arrayCV_IMPL uchar*cvPtr2D( const CvArr* arr, int y, int x, int* _type ){ uchar* ptr = 0; CV_FUNCNAME( "cvPtr2D" ); __BEGIN__; if( CV_IS_MAT( arr )) { CvMat* mat = (CvMat*)arr; int type; if( (unsigned)y >= (unsigned)(mat->rows) || (unsigned)x >= (unsigned)(mat->cols) ) CV_ERROR( CV_StsOutOfRange, "index is out of range" ); type = CV_MAT_TYPE(mat->type); if( _type ) *_type = type; ptr = mat->data.ptr + (size_t)y*mat->step + x*CV_ELEM_SIZE(type); } else if( CV_IS_IMAGE( arr )) { IplImage* img = (IplImage*)arr; int pix_size = (img->depth & 255) >> 3; int width, height; ptr = (uchar*)img->imageData; if( img->dataOrder == 0 ) pix_size *= img->nChannels; if( img->roi ) { width = img->roi->width; height = img->roi->height; ptr += img->roi->yOffset*img->widthStep + img->roi->xOffset*pix_size; if( img->dataOrder ) { int coi = img->roi->coi; if( !coi ) CV_ERROR( CV_BadCOI, "COI must be non-null in case of planar images" ); ptr += (coi - 1)*img->imageSize; } } else { width = img->width; height = img->height; } if( (unsigned)y >= (unsigned)height || (unsigned)x >= (unsigned)width ) CV_ERROR( CV_StsOutOfRange, "index is out of range" );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -