// get specifed element of array CV_IMPL CvScalar cvGetAt( const CvArr* arr, int y, int x ) { CvScalar scalar = {{ 0, 0, 0, 0 }}; CV_FUNCNAME( "cvGetAt" ); __BEGIN__; uchar* ptr; int type; CV_CALL( ptr = cvGetPtrAt( arr, y, x )); if( CV_IS_ARR( arr )) { type = CV_ARR_TYPE( ((CvMat*)arr)->type ); } else { IplImage* img = (IplImage*)arr; type = icvIplToCvDepth( img->depth ); if( type < 0 || (unsigned)(img->nChannels - 1) > 3 ) CV_ERROR( CV_StsUnsupportedFormat, "" ); type = type*4 + img->nChannels - 1; } CV_CALL( cvRawDataToScalar( ptr, type, &scalar )); __END__; return scalar; }
// Set array element to given value CV_IMPL void cvSetAt( CvArr* arr, CvScalar value, int y, int x ) { CV_FUNCNAME( "cvSetAt" ); __BEGIN__; uchar* ptr; int type; CV_CALL( ptr = cvGetPtrAt( arr, y, x )); if( CV_IS_ARR( arr )) { type = CV_ARR_TYPE( ((CvMat*)arr)->type ); } else { IplImage* img = (IplImage*)arr; type = icvIplToCvDepth( img->depth ); if( type < 0 || (unsigned)(img->nChannels - 1) > 3 ) CV_ERROR( CV_StsUnsupportedFormat, "" ); type = type*4 + img->nChannels - 1; } CV_CALL( cvScalarToRawData( &value, type, ptr )); __END__; }
// convert array (CvMat or IplImage) to CvMat CV_IMPL CvMat* cvGetMat( const CvArr* array, CvMat* mat, int* pCOI ) { CvMat* result = 0; CvMat* src = (CvMat*)array; int coi = 0; CV_FUNCNAME( "cvGetMat" ); __BEGIN__; if( !mat || !src ) CV_ERROR_FROM_CODE( CV_StsNullPtr ); if( !_CV_IS_ARR(src) ) { const IplImage* img = (const IplImage*)src; int depth, order; if( img->nSize != sizeof(IplImage)) CV_ERROR_FROM_CODE( CV_StsBadFlag ); if( img->imageData == 0 ) CV_ERROR_FROM_CODE( CV_StsNullPtr ); if( img->nChannels > 4 ) CV_ERROR_FROM_CODE( CV_BadNumChannels ); 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, "Planar order should be used with coi != 0" ); 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*icvPixSize[type], img->widthStep )); } else /* pixel order */ { int type = depth + (img->nChannels - 1)*8; coi = img->roi->coi; CV_CALL( cvInitMatHeader( mat, img->roi->height, img->roi->width, type, img->imageData + img->roi->yOffset*img->widthStep + img->roi->xOffset*icvPixSize[type], img->widthStep )); } } else { int type = depth + (img->nChannels - 1)*8; 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( !src->data.ptr ) CV_ERROR_FROM_CODE( CV_StsNullPtr ); result = (CvMat*)src; } __END__; if( pCOI ) *pCOI = coi; return result; }
static CvMat * cvCreateImageMat( CvSize size, int depth, int channels ) { depth = icvIplToCvDepth(depth); return cvCreateMat( size.height, size.width, CV_MAKE_TYPE(depth, channels)); }