CV_IMPL void cvCalcContrastHist( IplImage ** img, CvHistogram * hist, int dont_clear, IplImage * mask ) { CV_FUNCNAME( "cvCalcContrastHist" ); uchar *data[CV_HIST_MAX_DIM]; uchar *mask_data = 0; int step = 0; int mask_step = 0; CvSize roi = { 0, 0 }; __BEGIN__; { for( int i = 0; i < hist->c_dims; i++ ) CV_CALL( CV_CHECK_IMAGE( img[i] )); } if( mask ) { CV_CALL( CV_CHECK_IMAGE( mask )); if( mask->depth != IPL_DEPTH_8U ) CV_ERROR( CV_BadDepth, "bad mask depth" ); cvGetImageRawData( mask, &mask_data, &mask_step, 0 ); } { for( int i = 0; i < hist->c_dims; i++ ) cvGetImageRawData( img[i], &data[i], &step, &roi ); } if( img[0]->nChannels != 1 ) CV_ERROR( CV_BadNumChannels, "bad channels numbers" ); if( img[0]->depth != IPL_DEPTH_8U ) CV_ERROR( CV_BadDepth, "bad image depth" ); switch (img[0]->depth) { case IPL_DEPTH_8U: if( !mask ) { IPPI_CALL( icvCalcContrastHist8uC1R( data, step, roi, hist, dont_clear )); } else { IPPI_CALL( icvCalcContrastHistMask8uC1R( data, step, mask_data, mask_step, roi, hist, dont_clear )); } break; default: CV_ERROR( CV_BadDepth, "bad image depth" ); } __CLEANUP__; __END__; }
void show_points( IplImage* gray, CvPoint2D32f* u, int u_cnt, CvPoint2D32f* v, int v_cnt, CvSize etalon_size, int was_found ) { CvSize size; int i; cvGetImageRawData( gray, 0, 0, &size ); IplImage* rgb = cvCreateImage( size, 8, 3 ); cvMerge( gray, gray, gray, 0, rgb ); if( v ) { for( i = 0; i < v_cnt; i++ ) { cvCircle( rgb, cvPoint(cvRound(v[i].x), cvRound(v[i].y)), 3, CV_RGB(255,0,0), CV_FILLED); } } if( u ) { for( i = 0; i < u_cnt; i++ ) { cvCircle( rgb, cvPoint(cvRound(u[i].x), cvRound(u[i].y)), 3, CV_RGB(0,255,0), CV_FILLED); } } cvDrawChessboardCorners( rgb, etalon_size, v, v_cnt, was_found ); cvvNamedWindow( "test", 0 ); cvvShowImage( "test", rgb ); cvvWaitKey(0); }
/*F/////////////////////////////////////////////////////////////////////////////////////// // Name: cvPostWarpImage // Purpose: The functions postwarp the image after morphing // Context: // Parameters: img - initial image (in the beginning) // // Notes: //F*/ CV_IMPL void cvPostWarpImage( int numLines, /* number of scanlines */ uchar * src, /* source buffers */ int *src_nums, /* lens of buffers */ IplImage * img, /* dest image */ int *scanlines /* scanline */ ) { uchar *img_data = 0; int img_step = 0; CvSize img_size; CV_FUNCNAME( "cvPostWarpImage" ); __BEGIN__; cvGetImageRawData( img, &img_data, &img_step, &img_size ); if( img->nChannels != 3 ) CV_ERROR( CV_BadNumChannels, "Source image must have 3 channel." ); if( img->depth != IPL_DEPTH_8U ) CV_ERROR( CV_BadDepth, "Channel depth of image must be 8." ); CV_CALL( icvPostWarpImage8uC3R( numLines, /* number of scanlines */ src, /* source buffers */ src_nums, /* lens of buffers */ img_data, /* dest image */ img_step, /* dest image step */ img_size, /* dest image size */ scanlines /* scanline */ )); __END__; }
CV_IMPL void cvDeInterlace( IplImage* frame, IplImage* fieldEven, IplImage* fieldOdd ) { CV_FUNCNAME("icvDeInterlace"); __BEGIN__; uchar *framedata, *f0data, *f1data; int framestep = 0, f0step = 0, f1step = 0; CvSize framesize, f0size, f1size; int y; if( !frame || !fieldEven || !fieldOdd ) CV_ERROR_FROM_STATUS( CV_NULLPTR_ERR ); cvGetImageRawData( frame, &framedata, &framestep, &framesize ); cvGetImageRawData( fieldEven, &f0data, &f0step, &f0size ); cvGetImageRawData( fieldOdd, &f1data, &f1step, &f1size ); if( f0size.width != f1size.width || f0size.height != f1size.height || f0size.width != framesize.width || f0size.height != framesize.height/2 ) CV_ERROR( IPL_StsBadArg, "source image and destination image have incorrect combination of sizes" ); if( frame->depth != fieldEven->depth || frame->depth != fieldOdd->depth ) CV_ERROR_FROM_STATUS( CV_UNMATCHED_FORMATS_ERR ); if( frame->nChannels != fieldEven->nChannels || frame->nChannels != fieldOdd->nChannels ) CV_ERROR_FROM_STATUS( CV_UNMATCHED_FORMATS_ERR ); f0size.width *= icvGetBtPix( frame ); for( y = 0; y < f0size.height; y++, framedata += frame->widthStep*2, f0data += f0step, f1data += f1step ) { memcpy( f0data, framedata, f0size.width ); memcpy( f1data, framedata + frame->widthStep, f0size.width ); } __CLEANUP__; __END__; }
static int myThreshR( IplImage* Src8u, IplImage* Src8s, IplImage* Src32f, float Thresh, float Max, CvThreshType Type ) { char* s8u; char* s8s; char* s32f; int step8u, step8s, step32f; CvSize size; cvGetImageRawData(Src8u, (uchar**)&s8u, &step8u, &size); cvGetImageRawData(Src8s, (uchar**)&s8s, &step8s, 0); cvGetImageRawData(Src32f, (uchar**)&s32f, &step32f, 0); int y; for( y = 0; y < size.height; y++, s8u += step8u, s8s += step8s, s32f += step32f ) myThresh( (uchar*)s8u, s8s, (float*)s32f, Thresh, Max, size.width, Type ); return 0; }
void CKalmTrack::ApplyCamShift( CvImage* image, bool initialize ) { CvSize size; int bins = 20; m_cCamShift.set_hist_dims( 1, &bins ); m_cCamShift.set_thresh( 0, 1, 180 ); m_cCamShift.set_min_ch_val( 1, m_params.Smin ); m_cCamShift.set_max_ch_val( 1, 255 ); m_cCamShift.set_min_ch_val( 2, m_params.Vmin ); m_cCamShift.set_max_ch_val( 2, 255 ); cvGetImageRawData( image, 0, 0, &size ); if( m_object.x < 0 ) m_object.x = 0; if( m_object.x > size.width - m_object.width - 1 ) m_object.x = size.width - m_object.width - 1; if( m_object.y < 0 ) m_object.y = 0; if( m_object.y > size.height - m_object.height - 1 ) m_object.y = size.height - m_object.height - 1; m_cCamShift.set_window(m_object); if( initialize ) { m_cCamShift.reset_histogram(); m_cCamShift.update_histogram( image ); } m_cCamShift.track_object( image ); m_object = m_cCamShift.get_window(); Measurement[0] = (float)m_object.x + m_object.width*0.5f; Measurement[1] = initialize ? 0 : Measurement[0] - m_Old.x; Measurement[2] = (float)m_object.y + m_object.height*0.5f; Measurement[3] = initialize ? 0 : Measurement[2] - m_Old.y; m_Old.x = cvRound( Measurement[0] ); m_Old.y = cvRound( Measurement[2] ); cvKalmanUpdateByMeasurement(Kalman,&MVect); }
/*F/////////////////////////////////////////////////////////////////////////////////////// // Name: cvDeleteMoire // Purpose: The functions delete moire on the image after ViewMorphing // Context: // Parameters: img - image on which will delete moire // // Notes: //F*/ CV_IMPL void cvDeleteMoire( IplImage * img ) { uchar *img_data = 0; int img_step = 0; CvSize img_size; CV_FUNCNAME( "cvDeleteMoire" ); __BEGIN__; cvGetImageRawData( img, &img_data, &img_step, &img_size ); if( img->nChannels != 1 && img->nChannels != 3 ) CV_ERROR( CV_BadNumChannels, "Source image must have 3 channel." ); if( img->depth != IPL_DEPTH_8U ) CV_ERROR( CV_BadDepth, "Channel depth of source image must be 8." ); CV_CALL( icvDeleteMoire8u( img_data, img_step, img_size, img->nChannels )); __END__; }
void CamShift::DoHSV(IplImage* image, bool initialize) { m_cCamShift.set_hist_dims( 1, &m_bins ); m_cCamShift.set_hist_bin_range( 0, 0, 180 ); m_cCamShift.set_threshold( 0 ); m_cCamShift.set_min_ch_val( 1, m_Smin ); m_cCamShift.set_max_ch_val( 1, 255 ); m_cCamShift.set_min_ch_val( 2, m_Vmin ); m_cCamShift.set_max_ch_val( 2, m_Vmax ); CvSize size; cvGetImageRawData( image, 0, 0, &size ); if( m_object.x < 0 ) m_object.x = 0; if( m_object.x > size.width - m_object.width - 1 ) m_object.x = MAX(0, size.width - m_object.width - 1); if( m_object.y < 0 ) m_object.y = 0; if( m_object.y > size.height - m_object.height - 1 ) m_object.y = MAX(0, size.height - m_object.height - 1); if( m_object.width > size.width - m_object.x ) m_object.width = MIN(size.width, size.width - m_object.x); if( m_object.height > size.height - m_object.y ) m_object.height = MIN(size.height, size.height - m_object.y); m_cCamShift.set_window(m_object); if (initialize) { m_cCamShift.reset_histogram(); m_cCamShift.update_histogram( image ); } m_cCamShift.track_object( image ); m_object = m_cCamShift.get_window(); }
void CamShift::CheckBackProject(IplImage* image, int view) const { if( view == 1 ) { IplImage* src = ((CvCamShiftTracker&)m_cCamShift).get_back_project(); if( src && src->imageData && image ) { cvSetImageROI( image, cvRect( m_object.x, m_object.y, m_object.width, m_object.height )); cvSetImageROI( src, cvRect( m_object.x, m_object.y, m_object.width, m_object.height )); cvCvtColor( src, image, CV_GRAY2BGR ); cvResetImageROI( image ); cvResetImageROI( src ); } } else if( view == 2 && m_tracking ) { int i, dims; CvSize size; m_cCamShift.get_hist_dims( &dims ); cvGetImageRawData( image, 0, 0, &size ); for( i = 0; i < dims; i++ ) { int val = cvRound(m_cCamShift.query(&i)); CvPoint p[4]; p[0].x = p[1].x = i*size.width/(2*dims); p[2].x = p[3].x = (i+1)*size.width/(2*dims); p[1].y = p[2].y = 0; p[0].y = p[3].y = (val*size.height)/(3*255); cvFillConvexPoly( image, p, 4, CV_RGB(255,0,0)); } } }
CV_IMPL CvGLCM* cvCreateGLCM( const IplImage* srcImage, int stepMagnitude, const int* srcStepDirections,/* should be static array.. or if not the user should handle de-allocation */ int numStepDirections, int optimizationType ) { static const int defaultStepDirections[] = { 0,1, -1,1, -1,0, -1,-1 }; int* memorySteps = 0; CvGLCM* newGLCM = 0; int* stepDirections = 0; CV_FUNCNAME( "cvCreateGLCM" ); __BEGIN__; uchar* srcImageData = 0; CvSize srcImageSize; int srcImageStep; int stepLoop; const int maxNumGreyLevels8u = CV_MAX_NUM_GREY_LEVELS_8U; if( !srcImage ) CV_ERROR( CV_StsNullPtr, "" ); if( srcImage->nChannels != 1 ) CV_ERROR( CV_BadNumChannels, "Number of channels must be 1"); if( srcImage->depth != IPL_DEPTH_8U ) CV_ERROR( CV_BadDepth, "Depth must be equal IPL_DEPTH_8U"); if(!srcStepDirections) { numStepDirections = 4 ; } // no Directions provided, use the default ones - 0 deg, 45, 90, 135 if( !srcStepDirections ) { srcStepDirections = defaultStepDirections; } /* CV_CALL( stepDirections = (int*)cvAlloc( numStepDirections*2*sizeof(stepDirections[0]))); memcpy( stepDirections, srcStepDirections, numStepDirections*2*sizeof(stepDirections[0])); */ CV_CALL( stepDirections = new int[numStepDirections*2]); memcpy( stepDirections, srcStepDirections, numStepDirections*2*sizeof(int)); cvGetImageRawData( srcImage, &srcImageData, &srcImageStep, &srcImageSize ); // roll together Directions and magnitudes together with knowledge of image (step) // CV_CALL( memorySteps = (int*)cvAlloc( numStepDirections*sizeof(memorySteps[0]))); CV_CALL( memorySteps = new int[ numStepDirections]); for( stepLoop = 0; stepLoop < numStepDirections; stepLoop++ ) { stepDirections[stepLoop*2 + 0] *= stepMagnitude; stepDirections[stepLoop*2 + 1] *= stepMagnitude; memorySteps[stepLoop] = stepDirections[stepLoop*2 + 0]*srcImageStep + stepDirections[stepLoop*2 + 1]; } // CV_CALL( newGLCM = (CvGLCM*)cvAlloc(sizeof(newGLCM))); CV_CALL(newGLCM = new CvGLCM()); // CV_CALL( newGLCM = new CvGLCM[sizeof(CvGLCM)]); memset( newGLCM, 0, sizeof(newGLCM) ); newGLCM->matrices = 0; newGLCM->numMatrices = numStepDirections; newGLCM->optimizationType = optimizationType; if( optimizationType <= CV_GLCM_OPTIMIZATION_LUT ) { int lookupTableLoop, imageColLoop, imageRowLoop, lineOffset = 0; // if optimization type is set to lut, then make one for the image if( optimizationType == CV_GLCM_OPTIMIZATION_LUT ) { for( imageRowLoop = 0; imageRowLoop < srcImageSize.height; imageRowLoop++, lineOffset += srcImageStep ) { for( imageColLoop = 0; imageColLoop < srcImageSize.width; imageColLoop++ ) { newGLCM->forwardLookupTable[srcImageData[lineOffset+imageColLoop]]=1; } } newGLCM->numLookupTableElements = 0; for( lookupTableLoop = 0; lookupTableLoop < maxNumGreyLevels8u; lookupTableLoop++ ) { if( newGLCM->forwardLookupTable[ lookupTableLoop ] != 0 ) { newGLCM->forwardLookupTable[ lookupTableLoop ] = newGLCM->numLookupTableElements; newGLCM->reverseLookupTable[ newGLCM->numLookupTableElements ] = lookupTableLoop; newGLCM->numLookupTableElements++; } } } // otherwise make a "LUT" which contains all the gray-levels (for code-reuse) else if( optimizationType == CV_GLCM_OPTIMIZATION_NONE ) { for( lookupTableLoop = 0; lookupTableLoop <maxNumGreyLevels8u; lookupTableLoop++ ) { newGLCM->forwardLookupTable[ lookupTableLoop ] = lookupTableLoop; newGLCM->reverseLookupTable[ lookupTableLoop ] = lookupTableLoop; } newGLCM->numLookupTableElements = maxNumGreyLevels8u; } newGLCM->matrixSideLength = newGLCM->numLookupTableElements; icvCreateGLCM_LookupTable_8u_C1R( srcImageData, srcImageStep, srcImageSize, newGLCM, stepDirections, numStepDirections, memorySteps ); } else if( optimizationType == CV_GLCM_OPTIMIZATION_HISTOGRAM ) { CV_ERROR( CV_StsBadFlag, "Histogram-based method is not implemented" ); /* newGLCM->numMatrices *= 2; newGLCM->matrixSideLength = maxNumGreyLevels8u*2; icvCreateGLCM_Histogram_8uC1R( srcImageStep, srcImageSize, srcImageData, newGLCM, numStepDirections, stepDirections, memorySteps ); */ } __END__; // cvFree( &memorySteps ); // cvFree( &stepDirections ); delete[] memorySteps; delete[] stepDirections; if( cvGetErrStatus() < 0 ) { // cvFree( &newGLCM ); delete[] newGLCM; } return newGLCM; }
void Run() { int w, h; IplImage *pCapImage; PBYTE pCapBuffer = NULL; // Create camera instance _cam = CLEyeCreateCamera(_cameraGUID, _mode, _resolution, _fps); if(_cam == NULL) return; // Get camera frame dimensions CLEyeCameraGetFrameDimensions(_cam, w, h); // Depending on color mode chosen, create the appropriate OpenCV image if(_mode == CLEYE_COLOR_PROCESSED || _mode == CLEYE_COLOR_RAW) pCapImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 4); else pCapImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1); // Set some camera parameters //CLEyeSetCameraParameter(_cam, CLEYE_GAIN, 30); //CLEyeSetCameraParameter(_cam, CLEYE_EXPOSURE, 500); //CLEyeSetCameraParameter(_cam, CLEYE_AUTO_EXPOSURE, false); //CLEyeSetCameraParameter(_cam, CLEYE_AUTO_GAIN, false); //CLEyeSetCameraParameter(_cam, CLEYE_AUTO_WHITEBALANCE, false); //CLEyeSetCameraParameter(_cam, CLEYE_WHITEBALANCE_RED, 100); //CLEyeSetCameraParameter(_cam, CLEYE_WHITEBALANCE_BLUE, 200); //CLEyeSetCameraParameter(_cam, CLEYE_WHITEBALANCE_GREEN, 200); // Start capturing CLEyeCameraStart(_cam); CvMemStorage* storage = cvCreateMemStorage(0); IplImage* hsv_frame = cvCreateImage(cvSize(pCapImage->width, pCapImage->height), IPL_DEPTH_8U, 3); IplImage* thresholded = cvCreateImage(cvSize(pCapImage->width, pCapImage->height), IPL_DEPTH_8U, 1); IplImage* temp = cvCreateImage(cvSize(pCapImage->width >> 1, pCapImage->height >> 1), IPL_DEPTH_8U, 3); // Create a window in which the captured images will be presented cvNamedWindow( "Camera" , CV_WINDOW_AUTOSIZE ); cvNamedWindow( "HSV", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "EdgeDetection", CV_WINDOW_AUTOSIZE ); //int hl = 100, hu = 115, sl = 95, su = 135, vl = 115, vu = 200; int hl = 5, hu = 75, sl = 40, su = 245, vl = 105, vu = 175; // image capturing loop while(_running) { // Detect a red ball CvScalar hsv_min = cvScalar(hl, sl, vl, 0); CvScalar hsv_max = cvScalar(hu, su, vu, 0); cvGetImageRawData(pCapImage, &pCapBuffer); CLEyeCameraGetFrame(_cam, pCapBuffer); cvConvertImage(pCapImage, hsv_frame); // Get one frame if( !pCapImage ) { fprintf( stderr, "ERROR: frame is null...\n" ); getchar(); break; } // Covert color space to HSV as it is much easier to filter colors in the HSV color-space. cvCvtColor(pCapImage, hsv_frame, CV_RGB2HSV); // Filter out colors which are out of range. cvInRangeS(hsv_frame, hsv_min, hsv_max, thresholded); // Memory for hough circles CvMemStorage* storage = cvCreateMemStorage(0); // hough detector works better with some smoothing of the image cvSmooth( thresholded, thresholded, CV_GAUSSIAN, 9, 9 ); CvSeq* circles = cvHoughCircles(thresholded, storage, CV_HOUGH_GRADIENT, 2, thresholded->height/4, 100, 50, 10, 400); for (int i = 0; i < circles->total; i++) { float* p = (float*)cvGetSeqElem( circles, i ); //printf("Ball! x=%f y=%f r=%f\n\r",p[0],p[1],p[2] ); cvCircle( pCapImage, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 ); cvCircle( pCapImage, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 ); } cvShowImage( "Camera", pCapImage ); // Original stream with detected ball overlay cvShowImage( "HSV", hsv_frame); // Original stream in the HSV color space cvShowImage( "EdgeDetection", thresholded ); // The stream after color filtering cvReleaseMemStorage(&storage); // Do not release the frame! //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), //remove higher bits using AND operator int key = cvWaitKey(10); switch(key){ case 'q' : hu += 5; break; case 'Q' : hu -= 5; break; case 'a' : hl -= 5; break; case 'A' : hl += 5; break; case 'w' : su += 5; break; case 'W' : su -= 5; break; case 's' : sl -= 5; break; case 'S' : sl += 5; break; case 'e' : vu += 5; break; case 'E' : vu -= 5; break; case 'd' : vl -= 5; break; case 'D' : vl += 5; break; } if (key != -1){ printf("H: %i, S: %i, V: %i\nH: %i, S: %i, V: %i\n\n", hu, su, vu, hl, sl, vl); } } cvReleaseImage(&temp); cvReleaseImage(&pCapImage); // Stop camera capture CLEyeCameraStop(_cam); // Destroy camera object CLEyeDestroyCamera(_cam); // Destroy the allocated OpenCV image cvReleaseImage(&pCapImage); _cam = NULL; }
CV_IMPL void cvCalcCovarMatrixEx( int nObjects, void* input, int ioFlags, int ioBufSize, uchar* buffer, void* userData, IplImage* avg, float* covarMatrix ) { float *avg_data; int avg_step = 0; CvSize avg_size; int i; CV_FUNCNAME( "cvCalcCovarMatrixEx" ); __BEGIN__; cvGetImageRawData( avg, (uchar **) & avg_data, &avg_step, &avg_size ); if( avg->depth != IPL_DEPTH_32F ) CV_ERROR( CV_BadDepth, cvUnsupportedFormat ); if( avg->nChannels != 1 ) CV_ERROR( CV_BadNumChannels, cvUnsupportedFormat ); if( ioFlags == CV_EIGOBJ_NO_CALLBACK ) { IplImage **images = (IplImage **) (((CvInput *) & input)->data); uchar **objects = (uchar **) cvAlloc( sizeof( uchar * ) * nObjects ); int img_step = 0, old_step = 0; CvSize img_size = avg_size, old_size = avg_size; if( objects == NULL ) CV_ERROR( CV_StsBadArg, "Insufficient memory" ); for( i = 0; i < nObjects; i++ ) { IplImage *img = images[i]; uchar *img_data; cvGetImageRawData( img, &img_data, &img_step, &img_size ); if( img->depth != IPL_DEPTH_8U ) CV_ERROR( CV_BadDepth, cvUnsupportedFormat ); if( img_size != avg_size || img_size != old_size ) CV_ERROR( CV_StsBadArg, "Different sizes of objects" ); if( img->nChannels != 1 ) CV_ERROR( CV_BadNumChannels, cvUnsupportedFormat ); if( i > 0 && img_step != old_step ) CV_ERROR( CV_StsBadArg, "Different steps of objects" ); old_step = img_step; old_size = img_size; objects[i] = img_data; } CV_CALL( icvCalcCovarMatrixEx_8u32fR( nObjects, (void*) objects, img_step, CV_EIGOBJ_NO_CALLBACK, 0, NULL, NULL, avg_data, avg_step, avg_size, covarMatrix )); cvFree( &objects ); } else { CV_CALL( icvCalcCovarMatrixEx_8u32fR( nObjects, input, avg_step / 4, ioFlags, ioBufSize, buffer, userData, avg_data, avg_step, avg_size, covarMatrix )); } __END__; }
/*F/////////////////////////////////////////////////////////////////////////////////////// // Name: cvCalcEigenObjects // Purpose: The function calculates an orthonormal eigen basis and a mean (averaged) // object for a group of input objects (images, vectors, etc.). // Context: // Parameters: nObjects - number of source objects // input - pointer either to array of input objects // or to read callback function (depending on ioFlags) // output - pointer either to output eigen objects // or to write callback function (depending on ioFlags) // ioFlags - input/output flags (see Notes) // ioBufSize - input/output buffer size // userData - pointer to the structure which contains all necessary // data for the callback functions // calcLimit - determines the calculation finish conditions // avg - averaged object (has the same size as ROI) // eigVals - pointer to corresponding eigen values (array of <nObjects> // elements in descending order) // // Notes: 1. input/output data (that is, input objects and eigen ones) may either // be allocated in the RAM or be read from/written to the HDD (or any // other device) by read/write callback functions. It depends on the // value of ioFlags paramater, which may be the following: // CV_EIGOBJ_NO_CALLBACK, or 0; // CV_EIGOBJ_INPUT_CALLBACK; // CV_EIGOBJ_OUTPUT_CALLBACK; // CV_EIGOBJ_BOTH_CALLBACK, or // CV_EIGOBJ_INPUT_CALLBACK | CV_EIGOBJ_OUTPUT_CALLBACK. // The callback functions as well as the user data structure must be // developed by the user. // // 2. If ioBufSize = 0, or it's too large, the function dermines buffer size // itself. // // 3. Depending on calcLimit parameter, calculations are finished either if // eigenfaces number comes up to certain value or the relation of the // current eigenvalue and the largest one comes down to certain value // (or any of the above conditions takes place). The calcLimit->type value // must be CV_TERMCRIT_NUMB, CV_TERMCRIT_EPS or // CV_TERMCRIT_NUMB | CV_TERMCRIT_EPS. The function returns the real // values calcLimit->max_iter and calcLimit->epsilon. // // 4. eigVals may be equal to NULL (if you don't need eigen values in further). // //F*/ CV_IMPL void cvCalcEigenObjects( int nObjects, void* input, void* output, int ioFlags, int ioBufSize, void* userData, CvTermCriteria* calcLimit, IplImage* avg, float* eigVals ) { float *avg_data; int avg_step = 0; CvSize avg_size; int i; int nEigens = nObjects - 1; CV_FUNCNAME( "cvCalcEigenObjects" ); __BEGIN__; cvGetImageRawData( avg, (uchar **) & avg_data, &avg_step, &avg_size ); if( avg->depth != IPL_DEPTH_32F ) CV_ERROR( CV_BadDepth, cvUnsupportedFormat ); if( avg->nChannels != 1 ) CV_ERROR( CV_BadNumChannels, cvUnsupportedFormat ); if( nEigens > calcLimit->max_iter && calcLimit->type != CV_TERMCRIT_EPS ) nEigens = calcLimit->max_iter; switch (ioFlags) { case CV_EIGOBJ_NO_CALLBACK: { IplImage **objects = (IplImage **) (((CvInput *) & input)->data); IplImage **eigens = (IplImage **) (((CvInput *) & output)->data); uchar **objs = (uchar **) cvAlloc( sizeof( uchar * ) * nObjects ); float **eigs = (float **) cvAlloc( sizeof( float * ) * nEigens ); int obj_step = 0, old_step = 0; int eig_step = 0, oldeig_step = 0; CvSize obj_size = avg_size, old_size = avg_size, eig_size = avg_size, oldeig_size = avg_size; if( objects == NULL || eigens == NULL ) CV_ERROR( CV_StsBadArg, "Insufficient memory" ); for( i = 0; i < nObjects; i++ ) { IplImage *img = objects[i]; uchar *obj_data; cvGetImageRawData( img, &obj_data, &obj_step, &obj_size ); if( img->depth != IPL_DEPTH_8U ) CV_ERROR( CV_BadDepth, cvUnsupportedFormat ); if( obj_size != avg_size || obj_size != old_size ) CV_ERROR( CV_StsBadArg, "Different sizes of objects" ); if( img->nChannels != 1 ) CV_ERROR( CV_BadNumChannels, cvUnsupportedFormat ); if( i > 0 && obj_step != old_step ) CV_ERROR( CV_StsBadArg, "Different steps of objects" ); old_step = obj_step; old_size = obj_size; objs[i] = obj_data; } for( i = 0; i < nEigens; i++ ) { IplImage *eig = eigens[i]; float *eig_data; cvGetImageRawData( eig, (uchar **) & eig_data, &eig_step, &eig_size ); if( eig->depth != IPL_DEPTH_32F ) CV_ERROR( CV_BadDepth, cvUnsupportedFormat ); if( eig_size != avg_size || eig_size != oldeig_size )
void search_template(const CvArr* previous, const CvArr* current, CvPoint2D32f previous_feature, CvPoint2D32f *current_feature_p, double *zncc_array, double *maximum_p, double *minimum_p, CvSize template_size, int step) { uchar *dataR, *dataS; int stepR, stepS; CvSize sizeR, sizeS; int u, v; int uu, vv; double sum_i; double sum_t; double avg_i; double avg_t; double sum_iimttm; double sum_iim; double sum_ttm; double zncc; double max_zncc; double min_zncc; int max_u, max_v; uchar *pR, *pS; uchar r, s; cvGetImageRawData(previous, &dataR, &stepR, &sizeR); cvGetImageRawData(current, &dataS, &stepS, &sizeS); max_zncc = 0.0; min_zncc = 1.0; max_u = 8; max_v = 8; for (v = 0; v < 16; v++) { for (u = 0; u < 16; u++) { sum_i = 0.0; sum_t = 0.0; for (vv = 0; vv < template_size.height; vv += step) { for (uu = 0; uu < template_size.width; uu += step) { pR = dataR + (int) previous_feature.x + uu - template_size.width / 2 + stepR * ((int) previous_feature.y + vv - template_size.height / 2); r = *pR; pS = dataS + u * step + ((int) previous_feature.x - 8 * step) + uu - template_size.width / 2 + stepS * (v * step + ((int) previous_feature.y - 8 * step) + vv - template_size.height / 2); s = *pS; sum_t += r; sum_i += s; } } avg_t = sum_t * step * step / (double) (template_size.height * template_size.width); avg_i = sum_i * step * step / (double) (template_size.height * template_size.width); sum_iim = 0.0; sum_ttm = 0.0; sum_iimttm = 0.0; for (vv = 0; vv < template_size.height; vv += step) { for (uu = 0; uu < template_size.width; uu += step) { pR = dataR + (int) previous_feature.x + uu - template_size.width / 2 + stepR * ((int) previous_feature.y + vv - template_size.height / 2); r = *pR; pS = dataS + u * step + ((int) previous_feature.x - 8 * step) + uu - template_size.width / 2 + stepS * (v * step + ((int) previous_feature.y - 8 * step) + vv - template_size.height / 2); s = *pS; sum_iimttm += (s - avg_i) * (r - avg_t); sum_iim += (s - avg_i) * (s - avg_i); sum_ttm += (r - avg_t) * (r - avg_t); } } zncc = sum_iimttm / sqrt(sum_iim * sum_ttm); zncc_array[u + 16 * v] = zncc; if (zncc > max_zncc) { max_zncc = zncc; max_u = u; max_v = v; } if (zncc < min_zncc) { min_zncc = zncc; } } } *minimum_p = min_zncc; *maximum_p = max_zncc; (*current_feature_p).x = max_u * step + ((int) previous_feature.x - 8 * step); (*current_feature_p).y = max_v * step + ((int) previous_feature.y - 8 * step); if ((*current_feature_p).y <= template_size.height / 2 + 8 * step) { (*current_feature_p).y = template_size.height / 2 + 8 * step; } if ((*current_feature_p).y >= sizeS.height - template_size.height / 2 - 8 * step - 1) { (*current_feature_p).y = sizeS.height - template_size.height / 2 - 8 * step - 1; } if ((*current_feature_p).x <= template_size.width / 2 + 8 * step) { (*current_feature_p).x = template_size.width / 2 + 8 * step; } if ((*current_feature_p).x >= sizeS.width - template_size.width / 2 - 8 * step - 1) { (*current_feature_p).x = sizeS.width - template_size.width / 2 - 8 * step - 1; } /* fprintf(stderr, "x=%lf, y=%lf, min_zncc=%lf, max_zncc=%lf, thr=%lf, t_width=%d, t_height=%d, step=%d\n", (*current_feature_p).x, (*current_feature_p).y, min_zncc, max_zncc, THRESHOLD, template_size.width, template_size.height, step); */ }
void CLEyeCameraCapture::Run() { // Create camera instance _cam = CLEyeCreateCamera(_cameraGUID, _mode, _resolution, _fps); if(_cam == NULL) return; // Get camera frame dimensions CLEyeCameraGetFrameDimensions(_cam, w, h); // Depending on color mode chosen, create the appropriate OpenCV image if(_mode == CLEYE_COLOR_PROCESSED || _mode == CLEYE_COLOR_RAW) pCapImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 4); else pCapImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1); // Set some camera parameters //CLEyeSetCameraParameter(_cam, CLEYE_GAIN, 20); //CLEyeSetCameraParameter(_cam, CLEYE_EXPOSURE, 511); CLEyeSetCameraParameter(_cam, CLEYE_AUTO_GAIN, true); CLEyeSetCameraParameter(_cam, CLEYE_AUTO_EXPOSURE, true); CLEyeSetCameraParameter( _cam, CLEYE_HFLIP, true); // Start capturing CLEyeCameraStart(_cam); cvGetImageRawData(pCapImage, &pCapBuffer); pCapture = pCapImage; long frames = 0; long count = GetTickCount(); long prevCount = 0; double fps = 0; // image capturing loop Mat src_gray, subImage, subImage_gray; vector<Vec3f> circles; Point center; Point n_center; int radius = 0; int counter = 0; char* fpsText = new char[5]; char* pos_text = new char[10]; while(_running) { CLEyeCameraGetFrame(_cam, pCapBuffer); //check fps every 100 frames frames++; if((frames % 100) == 0){ prevCount = count; count = GetTickCount(); fps = 100000.0/(count - prevCount); //std::cout << "fps: " << fps << endl; sprintf(fpsText, "fps: %f", fps); } if(frames > 100) putText(pCapture, fpsText, Point(5, 20), CV_FONT_HERSHEY_PLAIN, 1, Scalar(0, 255, 0)); else putText(pCapture, "calculating fps...", Point(5, 20), CV_FONT_HERSHEY_PLAIN, 1, Scalar(0, 255, 0)); //find circle in whole area of frame first if(!_isTracking){ CircleDetector(pCapture, src_gray, circles, center, radius); if(circles.size() != 0) _isTracking = true; n_center = center; } //dynamically move subimage area by tracking the object else { int subImage_size = 30; Point temp = FixSubImageSize(n_center, 320, 240, subImage_size); Rect t_rect(temp.x - subImage_size, temp.y - subImage_size, subImage_size*2, subImage_size*2); subImage = pCapture(t_rect); CircleDetector(subImage, subImage_gray, circles, center, radius); imshow(trackingWindowName, subImage); if(circles.size() == 0) { counter++; if(counter == 3) { _isTracking = false; counter = 0; cout << "Lost tracking! Search whole frame." << endl; } } else { counter = 0; n_center.x = temp.x - subImage_size + center.x; n_center.y = temp.y - subImage_size + center.y; cout << "fps: " << fps << " x:" << n_center.x << ", y:" << n_center.y << endl; } } sprintf(pos_text, "x=%d,y=%d", n_center.x, n_center.y); if(circles.size() != 0){ putText(pCapture, pos_text, Point(n_center.x + radius, n_center.y - radius), CV_FONT_HERSHEY_PLAIN, 1, Scalar(0, 255, 0)); } imshow(_windowName, pCapture); } // Stop camera capture CLEyeCameraStop(_cam); // Destroy camera object CLEyeDestroyCamera(_cam); // Destroy the allocated OpenCV image cvReleaseImage(&pCapImage); _cam = NULL; }
void CCondens::ApplyCamShift( CvImage* image, bool initialize ) { CvSize size; int bins = 20; m_cCamShift.set_hist_dims( 1, &bins ); m_cCamShift.set_thresh( 0, 1, 180 ); m_cCamShift.set_min_ch_val( 1, m_params.Smin ); m_cCamShift.set_max_ch_val( 1, 255 ); m_cCamShift.set_min_ch_val( 2, m_params.Vmin ); m_cCamShift.set_max_ch_val( 2, 255 ); cvGetImageRawData( image, 0, 0, &size ); if( m_object.x > size.width - m_object.width - 1 ) m_object.x = size.width - m_object.width - 1; if( m_object.x < 0 ) m_object.x = 0; if( m_object.y > size.height - m_object.height - 1 ) m_object.y = size.height - m_object.height - 1; if( m_object.y < 0 ) m_object.y = 0; m_cCamShift.set_window(m_object); if( initialize ) { m_cCamShift.reset_histogram(); m_cCamShift.update_histogram( image ); } m_cCamShift.track_object( image ); m_object = m_cCamShift.get_window(); LBound[0] = (float)m_object.x; LBound[1] = (float)-m_object.width*0.5f; LBound[2] = (float)m_object.y; LBound[3] = (float)- m_object.height*0.5f; UBound[0] = (float)m_object.x + m_object.width; UBound[1] = (float)m_object.width*0.5f; UBound[2] = (float)m_object.y + m_object.height; UBound[3] = (float)m_object.height*0.5f; Measurement[0] = (float)m_object.x+m_object.width*0.5f; Measurement[1] = initialize ? 0 : (float)(Measurement[0] - m_Old.x); Measurement[2] = (float)m_object.y+m_object.height*0.5f; Measurement[3] = initialize ? 0 : (float)(Measurement[2] - m_Old.y); m_Old.x = cvRound( Measurement[0] ); m_Old.y = cvRound( Measurement[2] ); if( initialize ) { CvMat LB = cvMat(4,1,CV_MAT4x1_32F,LBound); CvMat UB = cvMat(4,1,CV_MAT4x1_32F,UBound); cvConDensInitSampleSet(ConDens,&LB,&UB); } XCor = 1.5f/m_object.width; VXCor = 3.0f/m_object.width; YCor = 1.5f/m_object.height; VYCor = 3.0f/m_object.height; CondProbDens(ConDens,Measurement); m_Old.x = cvRound( Measurement[0] ); m_Old.y = cvRound( Measurement[2] ); }
double TestNormMask( IplImage* A, IplImage* B, IplImage* M, int type ) { uchar *A_data = 0, *B_data = 0, *M_data = 0, *m; int A_step = 0, B_step = 0, M_step = 0; CvSize A_size, B_size, M_size; int i, j, j4, k, coi, channels, nx, ny, n; double *a=0, *b=0, norm; cvGetImageRawData( A, &A_data, &A_step, &A_size ); coi = A->roi->coi; channels = A->nChannels; if( coi<0 || coi>3 ) return -1.0; if( coi > 0 && channels != 3 ) return -2.0; cvGetImageRawData( M, &M_data, &M_step, &M_size ); if( B ) { cvGetImageRawData( B, &B_data, &B_step, &B_size ); if( A_step!=B_step ) return -3.0; if( A_size.height!=B_size.height || A_size.width!=B_size.width ) return -4.0; if( coi!=B->roi->coi ) return -5.0; } else if( type==_CV_RELATIVE_C || type==_CV_RELATIVE_L1 || type==_CV_RELATIVE_L2 || type==_CV_DIFF_C || type==_CV_DIFF_L1 || type==_CV_DIFF_L2 ) return -10.0; nx = A_size.width; ny = A_size.height; n = nx*ny; m = (uchar*) icvAlloc( n*sizeof(uchar)); a = (double*)icvAlloc( n*sizeof(double)); if(B) b = (double*)icvAlloc( n*sizeof(double)); k = 0; if( channels < 3 ) { if( B ) { if( A->depth == IPL_DEPTH_8U ) { for( i=0; i<ny; i++, A_data+=A_step, B_data+=B_step, M_data+=M_step ) for( j=0; j<nx; j++, k++ ) { a[k] = A_data[j]; b[k] = B_data[j]; m[k] = M_data[j]; } } if( A->depth == IPL_DEPTH_8S ) { for( i=0; i<ny; i++, A_data+=A_step, B_data+=B_step, M_data+=M_step ) for( j=0; j<nx; j++, k++ ) { a[k] = *(char*)(A_data+j); b[k] = *(char*)(B_data+j); m[k] = M_data[j]; } } if( A->depth == IPL_DEPTH_32F ) { for( i=0; i<ny; i++, A_data+=A_step, B_data+=B_step, M_data+=M_step ) for( j=0, j4=0; j<nx; j++, j4+=4, k++ ) { a[k] = *(float*)(A_data+j4); b[k] = *(float*)(B_data+j4); m[k] = M_data[j]; } } } else { if( A->depth == IPL_DEPTH_8U ) { for( i=0; i<ny; i++, A_data+=A_step, M_data+=M_step ) for( j=0; j<nx; j++, k++ ) { a[k] = A_data[j]; m[k] = M_data[j]; } } if( A->depth == IPL_DEPTH_8S ) { for( i=0; i<ny; i++, A_data+=A_step, M_data+=M_step ) for( j=0; j<nx; j++, k++ ) { a[k] = *(char*)(A_data+j); m[k] = M_data[j]; } } if( A->depth == IPL_DEPTH_32F ) { for( i=0; i<ny; i++, A_data+=A_step, M_data+=M_step ) for( j=0, j4=0; j<nx; j++, j4+=4, k++ ) { a[k] = *(float*)(A_data+j4); m[k] = M_data[j]; } } } } else /* channels = 3 */ { coi--; if( B ) { if( A->depth == IPL_DEPTH_8U ) { A_data += coi; B_data += coi; for( i=0; i<ny; i++, A_data+=A_step, B_data+=B_step, M_data+=M_step ) for( j=0, j4=0; j<nx; j++, j4+=3, k++ ) { a[k] = A_data[j4]; b[k] = B_data[j4]; m[k] = M_data[j]; } } if( A->depth == IPL_DEPTH_8S ) { A_data += coi; B_data += coi; for( i=0; i<ny; i++, A_data+=A_step, B_data+=B_step, M_data+=M_step ) for( j=0, j4=0; j<nx; j++, j4+=3, k++ ) { a[k] = *(char*)(A_data+j4); b[k] = *(char*)(B_data+j4); m[k] = M_data[j]; } } if( A->depth == IPL_DEPTH_32F ) { A_data += 4*coi; B_data += 4*coi; for( i=0; i<ny; i++, A_data+=A_step, B_data+=B_step, M_data+=M_step ) for( j=0, j4=0; j<nx; j++, j4+=12, k++ ) { a[k] = *(float*)(A_data+j4); b[k] = *(float*)(B_data+j4); m[k] = M_data[j]; } } } else { if( A->depth == IPL_DEPTH_8U ) { A_data += coi; for( i=0; i<ny; i++, A_data+=A_step, M_data+=M_step ) for( j=0, j4=0; j<nx; j++, j4+=3, k++ ) { a[k] = A_data[j4]; m[k] = M_data[j]; } } if( A->depth == IPL_DEPTH_8S ) { A_data += coi; for( i=0; i<ny; i++, A_data+=A_step, M_data+=M_step ) for( j=0, j4=0; j<nx; j++, j4+=3, k++ ) { a[k] = *(char*)(A_data+j4); m[k] = M_data[j]; } } if( A->depth == IPL_DEPTH_32F ) { A_data += 4*coi; for( i=0; i<ny; i++, A_data+=A_step, M_data+=M_step ) for( j=0, j4=0; j<nx; j++, j4+=12, k++ ) { a[k] = *(float*)(A_data+j4); m[k] = M_data[j]; } } } } /* channels */ norm = calcTestNormMask( a, b, m, n, type ); icvFree((void**)&a ); icvFree((void**)&m ); if(b) icvFree((void**)&b ); return norm; }