int main(int argc, char* argv[]) { //创建矩阵 方式1 直接创建 CvMat* pmat1; pmat1 = cvCreateMat(8, 9, CV_32FC1); //创建矩阵方式2 先创建矩阵头部 再创建矩阵的数据块的内存空间 CvMat* pmat2; pmat2 = cvCreateMatHeader(4, 5, CV_8UC1); cvCreateData(pmat2); //创建矩阵方式3 通过数据创建矩阵 float data[4] = { 3, 4, 6, 0 }; CvMat pmat3; cvInitMatHeader(&pmat3, 2, 2, CV_32FC1, data); //创建矩阵方式4 通过已有矩阵进行克隆 CvMat* pmat4; pmat4 = cvCloneMat(pmat2); //访问矩阵的相关属性 test(pmat2); //释放矩阵的内存空间 cvReleaseMat(&pmat1); cvReleaseMat(&pmat2); cvReleaseMat(&pmat4); return 0; }
void Classifier::optical_flow(const IplImage *frame, double *xD, double *yD) { double xDiff = 0; double yDiff = 0; //double xQDiff = 0; //double yQDiff = 0; if (prevFrame) { /* Optical flow for entire image */ CvSize img_sz = cvGetSize(frame); IplImage *imgA = cvCreateImage(img_sz, IPL_DEPTH_8U, 1); IplImage *imgB = cvCreateImage(img_sz, IPL_DEPTH_8U, 1); cvCvtColor(frame, imgA, CV_BGR2GRAY); cvCvtColor(prevFrame, imgB, CV_BGR2GRAY); CvMat* velx = cvCreateMatHeader( img_sz.height, img_sz.width, CV_32FC1 ); cvCreateData( velx ); CvMat* vely = cvCreateMatHeader( img_sz.height, img_sz.width, CV_32FC1 ); cvCreateData( vely ); cvCalcOpticalFlowLK( imgA, imgB, cvSize(15, 15), velx, vely ); xDiff = cvAvg(velx).val[0]; yDiff = cvAvg(vely).val[0]; *xD = xDiff; *yD = yDiff; } // if else { prevFrame = cvCreateImage ( cvGetSize(frame), frame->depth, frame->nChannels ); } // else cvCopy(frame, prevFrame); }
// FIXME: error handling static void opencv_exec(unsigned char* src, int src_size, unsigned char* path, int path_size, int w, int h, /*out*/ void** descriptor) { char ext[256]; // NULL terminated cstr int ext_pos = (sizeof(ext) < (path_size+1)) ? path_size+1 - sizeof(ext) : 0; memcpy(ext, path, path_size - ext_pos); ext[path_size - ext_pos] = 0; CvMat tmp = cvMat(16384, 16384, CV_8UC4, (void*)src); CvMat* src_img = cvDecodeImageM(&tmp, CV_LOAD_IMAGE_COLOR); CvMat dst_img = cvMat(h, w, src_img->type, NULL); cvCreateData(&dst_img); cvResize(src_img, &dst_img, CV_INTER_AREA); // FIXME: using CV_INTER_AREA only if got scaled down CvMat* enc_img = cvEncodeImage(ext, &dst_img, 0); //out: cvReleaseMat(&src_img); cvReleaseData(&dst_img); *descriptor = (void*)enc_img; }
// create CvMat and underlying date CV_IMPL CvMat* cvCreateMat( int height, int width, int type ) { CvMat* arr = 0; CV_FUNCNAME( "cvCreateMat" ); __BEGIN__; CV_CALL( arr = cvCreateMatHeader( height, width, type )); CV_CALL( cvCreateData( arr )); __END__; if( cvGetErrStatus() < 0 ) cvReleaseMat( &arr ); return arr; }
// create IplImage header and allocate underlying data CV_IMPL IplImage * cvCreateImage( CvSize size, int depth, int channels ) { IplImage *img = 0; CV_FUNCNAME( "cvCreateImage" ); __BEGIN__; CV_CALL( img = cvCreateImageHeader( size, depth, channels )); assert( img ); CV_CALL( cvCreateData( img )); __END__; if( cvGetErrStatus() < 0 ) cvReleaseImage( &img ); return img; }
CV_IMPL IplImage* cvCloneImage( const IplImage* src ) { IplImage* dst = 0; CV_FUNCNAME( "cvCloneImage" ); __BEGIN__; if( !_CV_IS_IMAGE( src )) CV_ERROR( CV_StsBadArg, "Bad image header" ); if( !CvIPL.cloneImage ) { CV_CALL( dst = (IplImage*)cvAlloc( sizeof(*dst))); memcpy( dst, src, sizeof(*src)); dst->roi = 0; if( src->roi ) { dst->roi = icvCreateROI( src->roi->coi, src->roi->xOffset, src->roi->yOffset, src->roi->width, src->roi->height ); } if( src->imageData ) { int size = src->imageSize; cvCreateData( dst ); memcpy( dst->imageData, src->imageData, size ); } } else { dst = CvIPL.cloneImage( src ); } __END__; return dst; }
CV_IMPL CvMat* cvCloneMat( const CvMat* src ) { CvMat* dst = 0; CV_FUNCNAME( "cvCloneMat" ); __BEGIN__; if( !_CV_IS_ARR( src )) CV_ERROR( CV_StsBadArg, "Bad CvMat header" ); CV_CALL( dst = cvCreateMatHeader( src->height, src->width, src->type )); if( src->data.ptr ) { CV_CALL( cvCreateData( dst )); CV_CALL( cvCopy( src, dst )); } __END__; return dst; }
// resize capture->frame appropriately depending on camera and capture settings static int icvResizeFrame(CvCaptureCAM_DC1394 * capture){ if(capture->convert){ // resize if sizes are different, formats are different // or conversion option has changed if(capture->camera->frame_width != capture->frame.width || capture->camera->frame_height != capture->frame.height || capture->frame.depth != 8 || capture->frame.nChannels != 3 || capture->frame.imageData == NULL || capture->buffer_is_writeable == 0) { if(capture->frame.imageData && capture->buffer_is_writeable){ cvReleaseData( &(capture->frame)); } cvInitImageHeader( &capture->frame, cvSize( capture->camera->frame_width, capture->camera->frame_height ), IPL_DEPTH_8U, 3, IPL_ORIGIN_TL, 4 ); cvCreateData( &(capture->frame) ); capture->buffer_is_writeable = 1; } } else { // free image data if allocated by opencv if(capture->buffer_is_writeable){ cvReleaseData(&(capture->frame)); } // figure out number of channels and bpp int bpp = 8; int nch = 3; int width = capture->camera->frame_width; int height = capture->camera->frame_height; double code = CV_FOURCC('B','G','R',0); switch(capture->color_mode){ case COLOR_FORMAT7_YUV422: nch = 2; code = CV_FOURCC('Y','4','2','2'); break; case COLOR_FORMAT7_MONO8: code = CV_FOURCC('Y',0,0,0); nch = 1; break; case COLOR_FORMAT7_YUV411: code = CV_FOURCC('Y','4','1','1'); width *= 2; nch = 3; //yy[u/v] break; case COLOR_FORMAT7_YUV444: code = CV_FOURCC('Y','U','V',0); nch = 3; break; case COLOR_FORMAT7_MONO16: code = CV_FOURCC('Y',0,0,0); bpp = IPL_DEPTH_16S; nch = 1; break; case COLOR_FORMAT7_RGB16: bpp = IPL_DEPTH_16S; nch = 3; break; default: break; } // reset image header cvInitImageHeader( &capture->frame,cvSize( width, height ), bpp, nch, IPL_ORIGIN_TL, 4 ); //assert(capture->frame.imageSize == capture->camera->quadlets_per_frame*4); capture->buffer_is_writeable = 0; } return 1; }
t_jit_err cv_jit_LKflow_matrix_calc(t_cv_jit_LKflow *x, void *inputs, void *outputs) { t_jit_err err=JIT_ERR_NONE; long in_savelock,out_savelock; t_jit_matrix_info in_minfo,out_minfo; char *in_bp,*out_bp; long i,j; void *in_matrix,*out_matrix; float *out, *outX, *outY; int stepX, stepY; int radius = x->radius * 2 + 1; CvMat current; CvMat *flowX=0, *flowY=0; //First, get pointers to matrices in_matrix = jit_object_method(inputs,_jit_sym_getindex,0); out_matrix = jit_object_method(outputs,_jit_sym_getindex,0); if (x&&in_matrix&&out_matrix) { in_savelock = (long) jit_object_method(in_matrix,_jit_sym_lock,1); out_savelock = (long) jit_object_method(out_matrix,_jit_sym_lock,1); //Get the matrix info jit_object_method(in_matrix,_jit_sym_getinfo,&in_minfo); jit_object_method(out_matrix,_jit_sym_getinfo,&out_minfo); //Get pointers to the actual matrix data jit_object_method(in_matrix,_jit_sym_getdata,&in_bp); jit_object_method(out_matrix,_jit_sym_getdata,&out_bp); //If something is wrong with the pointer... if (!in_bp) { err=JIT_ERR_INVALID_INPUT; goto out;} if (!out_bp) { err=JIT_ERR_INVALID_OUTPUT; goto out;} //compatible types? if ((in_minfo.type!=_jit_sym_char)||(out_minfo.type!=_jit_sym_float32)) { err=JIT_ERR_MISMATCH_TYPE; goto out; } //compatible planes? if ((in_minfo.planecount!=1)||(out_minfo.planecount!=2)) { //Accepts only 1-plane matrices err=JIT_ERR_MISMATCH_PLANE; goto out; } //compatible dims? if ((in_minfo.dimcount!=2)){ err=JIT_ERR_MISMATCH_DIM; goto out; } //Check to see if image isn't too small if((in_minfo.dim[0] < radius)||(in_minfo.dim[1] < radius)){ error("Matrix height and width must be at least %d", radius); err = JIT_ERR_GENERIC; goto out; } //Convert Jitter matrix to OpenCV matrix cvJitter2CvMat(in_matrix, ¤t); flowX = cvCreateMat(current.rows, current.cols,CV_32FC1); flowY = cvCreateMat(current.rows, current.cols,CV_32FC1); if(!flowX || !flowY){ error("Failed to create internal data."); goto out; } if(!x->previous){ x->previous = cvCreateMat(current.rows, current.cols, current.type); if(!x->previous){ error("Failed to create internal matrix."); goto out; } } else if((current.cols != x->previous->cols)||(current.rows != x->previous->rows)||(current.step != x->previous->step)||(x->previous->data.ptr == NULL)){ cvReleaseMat(&x->previous); //Because we cast a Jitter matrix into a CvMat, we cannot assume that the step is going to be the same as that //returned by cvCreateMat, hence we need to fudge things by hand. x->previous = cvCreateMatHeader(current.rows, current.cols, current.type); if(!x->previous){ error("Failed to create internal matrix (2)."); err = JIT_ERR_GENERIC; goto out; } cvInitMatHeader(x->previous, current.rows, current.cols, current.type, 0, current.step); cvCreateData(x->previous); if(!x->previous->data.ptr){ error("Failed to allocate internal memory."); err = JIT_ERR_GENERIC; cvReleaseMat(&x->previous); goto out; } //jit_object_method(out_matrix, _jit_sym_clear); }else { //Calculate optical flow cvCalcOpticalFlowLK(x->previous, ¤t, cvSize(radius, radius),flowX, flowY); //Copy to output out = (float *)out_bp; outX = flowX->data.fl; outY = flowY->data.fl; stepX = flowX->step / sizeof(float); stepY = flowY->step / sizeof(float); for(i=0;i<out_minfo.dim[1];i++){ out=(float *)(out_bp+i*out_minfo.dimstride[1]); outX=flowX->data.fl+i*stepX; outY=flowY->data.fl+i*stepY; for(j=0;j<out_minfo.dim[0];j++){ out[0] = *outX; out[1] = *outY; out+=2; outX++; outY++; } } } cvCopy(¤t, x->previous, 0); } else { return JIT_ERR_INVALID_PTR; } out: if(flowX)cvReleaseMat(&flowX); if(flowY)cvReleaseMat(&flowY); jit_object_method(out_matrix, gensym("lock"),out_savelock); jit_object_method(in_matrix, gensym("lock"),in_savelock); return err; }