void ofxCvOpticalFlowLK::calc( ofxCvGrayscaleImage & pastImage, ofxCvGrayscaleImage & currentImage, int size ) { cvCalcOpticalFlowLK( pastImage.getCvImage(), currentImage.getCvImage(), cvSize( size, size), vel_x, vel_y ); }
void LukasKanade::perform() { cvCalcOpticalFlowLK( &_imageArray[GR_INPUT_IMAGE], &_imageArray[GR_OUTPUT_IMAGE], _windowSize, &_imageArray[GR_VELX_IMAGE], &_imageArray[GR_VELY_IMAGE]); }
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); }
int EjemploSimple(algoritmo a) { GUI g; float optical_flow_feature_error[400]; char optical_flow_found_feature[400]; IplImage *imgA = NULL; IplImage *imgB = NULL; IplImage *grayA = NULL; IplImage *grayB = NULL; IplImage *velx = NULL; IplImage *vely = NULL; imgA = cvLoadImage("imageA.bmp", true); imgB = cvLoadImage("imageB.bmp", true); CvSize size = cvGetSize(imgA); g.Initialize(size); grayA = cvCreateImage(cvGetSize(imgA), IPL_DEPTH_8U, 1); grayB = cvCreateImage(cvGetSize(imgB), IPL_DEPTH_8U, 1); velx = cvCreateImage(size, IPL_DEPTH_32F, 1); // cvCreateImage(cvGetSize(imgA),32,1); vely = cvCreateImage(size, IPL_DEPTH_32F, 1); cvCvtColor(imgA, grayA, CV_BGR2GRAY); cvCvtColor(imgB, grayB, CV_BGR2GRAY); printf("pulse una tecla\n"); getchar(); CvSize tamanyoVentana={3,3}; int number_of_features=400; while(true) { cvCalcOpticalFlowLK( grayA,grayB, tamanyoVentana, velx, vely); LKPiramidResults lkData; CalcularLKPiramid(*grayA,*grayB,3,5,number_of_features, a,0.01,4,lkData); g.Refresh(*grayA); cvCopyImage(imgA,&g.GetWindowBackground()); PintarLK(*velx,*vely,g.GetWindowBackground()); PintarPiramide(g.GetWindowBackground(),lkData); g.Refresh(); int key_pressed = cvWaitKey(0); /* If the users pushes "b" or "B" go back one frame. * Otherwise go forward one frame. */ if(key_pressed=='l'&& tamanyoVentana.height<=13) { tamanyoVentana.height+=2; tamanyoVentana.width+=2; } else if (key_pressed=='k'&& tamanyoVentana.height>=5) { tamanyoVentana.height-=2; tamanyoVentana.width-=2; } } return 0; }
void ofxOpticalFlowLK :: update ( unsigned char* pixels, int width, int height, int imageType ) { bool rightSize = ( sizeSml.width == width && sizeSml.height == height ); //-- making the input the right size for optical flow to work with. if( rightSize ) { if( imageType == OF_IMAGE_COLOR ) { colrImgSml.setFromPixels( pixels, sizeSml.width, sizeSml.height ); greyImgSml.setFromColorImage( colrImgSml ); } else if( imageType == OF_IMAGE_GRAYSCALE ) { greyImgSml.setFromPixels( pixels, sizeSml.width, sizeSml.height ); } else { return; // wrong image type. } } else { bool sizeLrgChanged = ( sizeLrg.width != width || sizeLrg.height != height ); if( sizeLrgChanged ) // size of input has changed since last update. { sizeLrg.width = width; sizeLrg.height = height; colrImgLrg.clear(); greyImgLrg.clear(); colrImgLrg.allocate( sizeLrg.width, sizeLrg.height ); greyImgLrg.allocate( sizeLrg.width, sizeLrg.height ); colrImgLrg.set( 0 ); greyImgLrg.set( 0 ); } if( imageType == OF_IMAGE_COLOR ) { colrImgLrg.setFromPixels( pixels, sizeLrg.width, sizeLrg.height ); colrImgSml.scaleIntoMe( colrImgLrg ); greyImgSml.setFromColorImage( colrImgSml ); } else if( imageType == OF_IMAGE_GRAYSCALE ) { greyImgLrg.setFromPixels( pixels, sizeLrg.width, sizeLrg.height ); greyImgSml.scaleIntoMe( greyImgLrg ); } else { return; // wrong image type. } } if( bMirrorH || bMirrorV ) greyImgSml.mirror( bMirrorV, bMirrorH ); int opFlowSize = opticalFlowSize; // value must be 1, 3, 5, 7... etc. opFlowSize *= 2; opFlowSize += 1; cvCalcOpticalFlowLK( greyImgPrv.getCvImage(), greyImgSml.getCvImage(), cvSize( opFlowSize, opFlowSize), opFlowVelX, opFlowVelY ); int opFlowBlur = opticalFlowBlur; // value must be 1, 3, 5, 7... etc. opFlowBlur *= 2; opFlowBlur += 1; cvSmooth( opFlowVelX, opFlowVelX, CV_BLUR, opFlowBlur ); cvSmooth( opFlowVelY, opFlowVelY, CV_BLUR, opFlowBlur ); greyImgPrv = greyImgSml; }
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; }
void Flow::calculate_flow_lk() { cvCalcOpticalFlowLK(ampl_img_2, ampl_img_1, cvSize(5, 5), velx, vely); }