/************************************************************************* * @函数名称: * calGradSim() * @输入: * const IplImage* image1 - 输入图像1 * const IplImage* image2 - 输入图像2 * @返回值: * double g - 梯度相似性 * @说明: * 计算图像梯度相似性 *************************************************************************/ double calGradSim(const IplImage* image1, const IplImage* image2) { double c4 = 0; double g = 0; IplImage* g1; IplImage* g2; IplImage* tmp; g1=gradientImage(image1); g2=gradientImage(image2); tmp = cvCloneImage(g1); cvMul(g1, g2, tmp); cvMul(g1, g1, g1); cvMul(g2, g2, g2); CvScalar s1 = cvSum(tmp); CvScalar s2 = cvSum(g1); CvScalar s3 = cvSum(g2); c4 = (0.03 * 255) * (0.03 * 255); g = (2 * s1.val[0] + c4) / (s2.val[0] +s3.val[0] + c4); cvReleaseImage(&g1); cvReleaseImage(&g2); cvReleaseImage(&tmp); return g; }
double CalcVectorDist( CvMat *target, CvMat *query ) { // use histogram intersection on each block // then fuse them in score level, with the weight of subspace dim of each block. double score = 0; if (! g_bNoWeights) { CvMat subT, subQ; int prjCnt = 0; for (int i = 0; i < g_blkCnt; i++) { cvGetRows(target, &subT, prjCnt, prjCnt+g_blkInputDim); cvGetRows(query, &subQ, prjCnt, prjCnt+g_blkInputDim); prjCnt += g_blkInputDim; cvMin(&subT,&subQ, g_tm); score += (cvSum(g_tm).val[0] * g_blkWeights[i]); } } else { cvMin(target,query, g_tm); score = +cvSum(g_tm).val[0]; } double normSum = cvSum(target).val[0]; //double norm1Sum = cvSum(query).val[0]; score /= normSum; return 1-score; }
int FrameLoader::getFrameNormFactor (int frameNumber, _frame_normalization_methodT fnm) { _TICTOC_TIC_FUNC; if (lastFrameLoaded != frameNumber) { if (loadWholeFrame(frameNumber) != 0){ _TICTOC_TOC_FUNC; return -1; }; lastFrameLoaded = frameNumber; } switch (fnm){ case _frame_none: _TICTOC_TOC_FUNC; return 0; break; case _frame_wholeImage: _TICTOC_TOC_FUNC; return (int) (cvSum(loadIm).val[0]); break; case _frame_excerptedRect: checkAr(); CvRect roi = cvGetImageROI(loadIm); cvSetImageROI(loadIm, ar); int rv = (int) (cvSum(loadIm).val[0]); cvSetImageROI(loadIm, roi); _TICTOC_TOC_FUNC; return rv; break; } _TICTOC_TOC_FUNC; return 0; //should never reach this point }
/// <summary> /// Finds min and max Y of the data present in given image. /// </summary> /// <params name="imsSrc"> /// Source image for which min and max Y has to be found. /// </params> /// <params name="min"> /// Int pointer where the min Y has to saved. /// </params> /// <params name="max"> /// Int pointer where the max Y has to saved. /// </params> /// <returns> Nothing. </returns> void OCR::findY(IplImage* imgSrc,int* min, int* max) { int i; int minFound=0; CvMat data; CvScalar maxVal=cvRealScalar(imgSrc->width * 255); CvScalar val=cvRealScalar(0); //For each col sum, if sum < width*255 then we find the min //then continue to end to search the max, if sum< width*255 then is new max for (i=0; i< imgSrc->height; i++) { val = cvRealScalar(0); cvGetRow(imgSrc, &data, i); val= cvSum(&data); if(val.val[0] < maxVal.val[0]) { *max=i; if(!minFound) { *min= i; minFound= 1; } } } }
static float CalcAverageMask(CvBlob* pBlob, IplImage* pImgFG ) { /* Calculate sum of mask: */ double Area, Aver = 0; CvRect r; CvMat mat; if(pImgFG==NULL) return 0; r.x = cvRound(pBlob->x - pBlob->w*0.5); r.y = cvRound(pBlob->y - pBlob->h*0.5); r.width = cvRound(pBlob->w); r.height = cvRound(pBlob->h); Area = r.width*r.height; if(r.x<0){r.width += r.x;r.x = 0;} if(r.y<0){r.height += r.y;r.y = 0;} if((r.x+r.width)>=pImgFG->width){r.width=pImgFG->width-r.x-1;} if((r.y+r.height)>=pImgFG->height){r.height=pImgFG->height-r.y-1;} if(r.width>0 && r.height>0) { double Sum = cvSum(cvGetSubRect(pImgFG,&mat,r)).val[0]/255.0; assert(Area>0); Aver = Sum/Area; } return (float)Aver; } /* Calculate sum of mask. */
match_direction_t catcierge_haar_guess_direction(catcierge_haar_matcher_t *ctx, IplImage *thr_img, int inverted) { int left_sum; int right_sum; catcierge_haar_matcher_args_t *args = ctx->args; match_direction_t dir = MATCH_DIR_UNKNOWN; CvRect roi = cvGetImageROI(thr_img); assert(ctx); assert(ctx->args); // Left. cvSetImageROI(thr_img, cvRect(0, 0, 1, roi.height)); left_sum = (int)cvSum(thr_img).val[0]; // Right. cvSetImageROI(thr_img, cvRect(roi.width - 1, 0, 1, roi.height)); right_sum = (int)cvSum(thr_img).val[0]; if (abs(left_sum - right_sum) > 25) { if (ctx->super.debug) printf("Left: %d, Right: %d\n", left_sum, right_sum); if (right_sum > left_sum) { // Going right. dir = (args->in_direction == DIR_RIGHT) ? MATCH_DIR_IN : MATCH_DIR_OUT; } else { // Going left. dir = (args->in_direction == DIR_LEFT) ? MATCH_DIR_IN : MATCH_DIR_OUT; } } cvSetImageROI(thr_img, roi); if (inverted && (dir != MATCH_DIR_UNKNOWN)) { if (dir == MATCH_DIR_IN) return MATCH_DIR_OUT; return MATCH_DIR_IN; } else { return dir; } }
void Update(DefHist* pH, float W) { /* Update histogram: */ double Vol, WM, WC; Vol = 0.5*(m_HistVolume + pH->m_HistVolume); WM = Vol*(1-W)/m_HistVolume; WC = Vol*(W)/pH->m_HistVolume; cvAddWeighted(m_pHist, WM, pH->m_pHist,WC,0,m_pHist); m_HistVolume = (float)cvSum(m_pHist).val[0]; } /* Update histogram: */
LabelMap AdaBoost::classify(CvMat* data) { if( !is_modelfile_loaded_ ) { printf("no model file is loaded"); exit(0); } LabelMap classification_result; LabelMap::iterator iter; CvMat* responses = 0; CvMat* var_type = 0; CvMat* temp_sample = 0; CvMat* weak_responses = 0; int var_count=0; int j=0, k=0; var_count = data->cols; temp_sample = cvCreateMat( 1, var_count + 1, CV_32F ); weak_responses = cvCreateMat( 1, this->classifier_.get_weak_predictors()->total, CV_32F ); int best_class = 0; double max_sum = -DBL_MAX; CvMat sample; cvGetRow( data, &sample, 0 ); for( k = 0; k < var_count; k++ ) temp_sample->data.fl[k] = (float)sample.data.db[k]; for( j = 0; j < this->number_of_classes_; j++ ) { temp_sample->data.fl[var_count] = (float)j; this->classifier_.predict( temp_sample, 0,weak_responses ); double sum = cvSum( weak_responses ).val[0]; classification_result[((char)(j + FIRST_LABEL))] = sum; if( max_sum < sum ) { max_sum = sum; best_class = j + FIRST_LABEL; } } cvReleaseMat( &temp_sample ); cvReleaseMat( &weak_responses ); cvReleaseMat( &var_type ); cvReleaseMat( &data ); cvReleaseMat( &responses ); return classification_result; }
/** * Note: if lowDensity < blankDensityThreshold -> blank; * else if highDensity > messyDensityThreshold -> messy; * else -> good; */ Smoothness compute_smoothness(const IplImage *pFourierImage, const double lowFreqRatio, const double blankDensity, const double messyDensity, const double highFreqRatio, double &outLowDensity, double &outHighDensity) { int low, high; IplImage *filteredFourierImage; int totalIntensity; double sum, den, totalArea; CvScalar scalar; if(! (pFourierImage->nChannels == 1 && pFourierImage->depth == 64) ) { cvError( CV_StsUnmatchedSizes, "compute_smoothness", "input image must contain only 1 channel and a depth of 64", __FILE__, __LINE__ ); } high_pass_range(pFourierImage, lowFreqRatio, low, high ); totalArea = M_PI * (high * high - low * low); filteredFourierImage = create_frequency_filtered_image(pFourierImage, low, high); scalar = cvSum(filteredFourierImage); totalIntensity = scalar.val[0]; cvReleaseImage(&filteredFourierImage); outLowDensity = den = totalIntensity / totalArea; if(den <= blankDensity) { return BLANK; } low = (int) (high * (1.0 - highFreqRatio)); filteredFourierImage = create_frequency_filtered_image(pFourierImage, low, high); scalar = cvSum(filteredFourierImage); totalIntensity = scalar.val[0]; cvReleaseImage(&filteredFourierImage); outHighDensity = den = totalIntensity / totalArea; if(den >= messyDensity) { return MESSY; } return SMOOTH; }
void CvOneWayDescriptor::EstimatePose(IplImage* patch, int& pose_idx, float& distance) const { distance = 1e10; pose_idx = -1; CvRect roi = cvGetImageROI(patch); IplImage* patch_32f = cvCreateImage(cvSize(roi.width, roi.height), IPL_DEPTH_32F, patch->nChannels); float sum = cvSum(patch).val[0]; cvConvertScale(patch, patch_32f, 1/sum); for(int i = 0; i < m_pose_count; i++) { if(m_samples[i]->width != patch_32f->width || m_samples[i]->height != patch_32f->height) { continue; } float dist = cvNorm(m_samples[i], patch_32f); //float dist = 0.0f; //float i1,i2; //for (int y = 0; y<patch_32f->height; y++) // for (int x = 0; x< patch_32f->width; x++) // { // i1 = ((float*)(m_samples[i]->imageData + m_samples[i]->widthStep*y))[x]; // i2 = ((float*)(patch_32f->imageData + patch_32f->widthStep*y))[x]; // dist+= (i1-i2)*(i1-i2); // } if(dist < distance) { distance = dist; pose_idx = i; } #if 0 IplImage* img1 = cvCreateImage(cvSize(roi.width, roi.height), IPL_DEPTH_8U, 1); IplImage* img2 = cvCreateImage(cvSize(roi.width, roi.height), IPL_DEPTH_8U, 1); double maxval; cvMinMaxLoc(m_samples[i], 0, &maxval); cvConvertScale(m_samples[i], img1, 255.0/maxval); cvMinMaxLoc(patch_32f, 0, &maxval); cvConvertScale(patch_32f, img2, 255.0/maxval); cvNamedWindow("1", 1); cvShowImage("1", img1); cvNamedWindow("2", 1); cvShowImage("2", img2); printf("Distance = %f\n", dist); cvWaitKey(0); #endif } cvReleaseImage(&patch_32f); }
void THISCLASS::OnStep() { // Get the input image IplImage* inputimage = mCore->mDataStructureImageGray.mImage; if (! inputimage) { AddError(wxT("No image on selected input.")); return; } // Calculate non-zero elements if (mCalculateNonZero) { int non_zero= cvCountNonZero(inputimage); CommunicationMessage m(wxT("STATS_NONZERO")); m.AddInt(non_zero); mCore->mCommunicationInterface->Send(&m); } // Calculate sum if (mCalculateSum) { CvScalar sum= cvSum(inputimage); CommunicationMessage m(wxT("STATS_SUM")); m.AddDouble(sum.val[0]); mCore->mCommunicationInterface->Send(&m); } // Calculate mean and standard deviation if (mCalculateMeanStdDev) { CvScalar mean; CvScalar std_dev; cvAvgSdv(inputimage, &mean, &std_dev, NULL); CommunicationMessage m(wxT("STATS_MEANSTDDEV")); m.AddDouble(mean.val[0]); m.AddDouble(std_dev.val[0]); mCore->mCommunicationInterface->Send(&m); } // Calculate min and max if (mCalculateMinMax) { double min_val; double max_val; cvMinMaxLoc(inputimage, &min_val, &max_val, NULL, NULL, NULL); CommunicationMessage m(wxT("STATS_MINMAX")); m.AddDouble(min_val); m.AddDouble(max_val); mCore->mCommunicationInterface->Send(&m); } // Set the display DisplayEditor de(&mDisplayOutput); if (de.IsActive()) { de.SetMainImage(inputimage); } }
int white_pixel_count() { int white_pix_count = 0; IplImage* frame = cvQueryFrame( capture ); if ( !frame ) { fprintf( stderr, "ERROR: frame is null...\n" ); return -1; } IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV IplImage* imgThresh = GetThresholdedImage(imgHSV); CvScalar sum = cvSum(imgThresh); white_pix_count = sum.val[0]/255; //printf("%f\n", avg.val[0]); return white_pix_count; }
double pkmGaussianMixtureModel::multinormalDistribution(const CvMat *pts, const CvMat *mean, const CvMat *covar) { int dimensions = 2; // add a tiny bit because of small samples CvMat *covarShifted = cvCreateMat(2, 2, CV_64FC1); cvAddS( covar, cvScalarAll(0.001), covarShifted); // calculate the determinant double det = cvDet(covarShifted); // invert covariance CvMat *covarInverted = cvCreateMat(2, 2, CV_64FC1); cvInvert(covarShifted, covarInverted); double ff = (1.0/(2.0*(double)PI))*(pow(det,-0.5)); CvMat *centered = cvCreateMat(2, 1, CV_64FC1); cvSub(pts, mean, centered); CvMat *invxmean = cvCreateMat(2, 1, CV_64FC1); //cvGEMM(covarInverted, centered, 1., NULL, 1., invxmean); cvMatMul(covarInverted, centered, invxmean); cvMul(centered, invxmean, invxmean); CvScalar sum = cvSum(invxmean); /* printf("covar: %f %f %f %f\n", cvmGet(covar, 0, 0), cvmGet(covar, 0, 1), cvmGet(covar, 1, 0), cvmGet(covar, 1, 1)); printf("covarShifted: %f %f %f %f\n", cvmGet(covarShifted, 0, 0), cvmGet(covarShifted, 0, 1), cvmGet(covarShifted, 1, 0), cvmGet(covarShifted, 1, 1)); printf("det: %f\n", det); printf("covarInverted: %f %f %f %f\n", cvmGet(covarInverted, 0, 0), cvmGet(covarInverted, 0, 1), cvmGet(covarInverted, 1, 0), cvmGet(covarShifted, 1, 1)); printf("ff: %f\n", ff); printf("pts: %f %f)\n", cvmGet(pts, 0, 0), cvmGet(pts, 1, 0)); printf("mean: %f %f)\n", cvmGet(mean, 0, 0), cvmGet(mean, 1, 0)); printf("centered: %f %f)\n", cvmGet(centered, 0, 0), cvmGet(centered, 1, 0)); printf("invxmean: %f %f)\n", cvmGet(invxmean, 0, 0), cvmGet(invxmean, 1, 0)); printf("scalar: %f %f %f %f\n", sum.val[0], sum.val[1], sum.val[2], sum.val[3]); */ cvReleaseMat(&covarShifted); cvReleaseMat(&covarInverted); cvReleaseMat(¢ered); cvReleaseMat(&invxmean); return ff * exp(-0.5*sum.val[0]); }
int cvL1QCSolve( CvMatOps AOps, CvMatOps AtOps, void* userdata, CvMat* B, CvMat* X, double epsilon, double mu, CvTermCriteria lb_term_crit, CvTermCriteria cg_term_crit ) { CvMat* Z = cvCreateMat( X->rows, 1, CV_MAT_TYPE(X->type) ); CvMat* W = cvCreateMat( B->rows, 1, CV_MAT_TYPE(B->type) ); CvAAtOpsData AAtData; AAtData.AOps = AOps; AAtData.AtOps = AtOps; AAtData.AtR = Z; AAtData.userdata = userdata; if ( cvCGSolve( icvAAtOps, &AAtData, B, W, cg_term_crit ) > .5 ) { cvReleaseMat( &W ); cvReleaseMat( &Z ); return -1; } AtOps( W, X, userdata ); AAtData.AR = W; CvMat* U = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) ); cvAbsDiffS( X, U, cvScalar(0) ); CvScalar sumAbsX = cvSum( U ); double minAbsX, maxAbsX; cvMinMaxLoc( U, &minAbsX, &maxAbsX ); cvConvertScale( U, U, .95, maxAbsX * .1 ); double tau = MAX( (2 * X->rows + 1) / sumAbsX.val[0], 1 ); if ( !(lb_term_crit.type & CV_TERMCRIT_ITER) ) lb_term_crit.max_iter = ceil( (log(2 * X->rows + 1) - log(lb_term_crit.epsilon) - log(tau)) / log(mu) ); CvTermCriteria nt_term_crit = cvTermCriteria( CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 50, lb_term_crit.epsilon ); int totaliter = 0; for ( int i = 0; i < lb_term_crit.max_iter; ++i ) { totaliter += icvL1QCNewton( AAtData, B, X, U, epsilon, tau, nt_term_crit, cg_term_crit ); tau *= mu; } cvReleaseMat( &U ); cvReleaseMat( &W ); cvReleaseMat( &Z ); return 0; }
//============================================================================ void AAM_TDM::DoPCA(const CvMat* AllTextures, double percentage) { LOGD("Doing PCA of textures datas..."); int nSamples = AllTextures->rows; int nPixels = AllTextures->cols; int nEigenAtMost = MIN(nSamples, nPixels); CvMat* tmpEigenValues = cvCreateMat(1, nEigenAtMost, CV_64FC1); CvMat* tmpEigenVectors = cvCreateMat(nEigenAtMost, nPixels, CV_64FC1); __MeanTexture = cvCreateMat(1, nPixels, CV_64FC1 ); cvCalcPCA(AllTextures, __MeanTexture, tmpEigenValues, tmpEigenVectors, CV_PCA_DATA_AS_ROW); double allSum = cvSum(tmpEigenValues).val[0]; double partSum = 0.0; int nTruncated = 0; double largesteigval = cvmGet(tmpEigenValues, 0, 0); for(int i = 0; i < nEigenAtMost; i++) { double thiseigval = cvmGet(tmpEigenValues, 0, i); if(thiseigval / largesteigval < 0.0001) break; // firstly check(remove small values) partSum += thiseigval; ++ nTruncated; if(partSum/allSum >= percentage) break; //secondly check } __TextureEigenValues = cvCreateMat(1, nTruncated, CV_64FC1); __TextureEigenVectors = cvCreateMat(nTruncated, nPixels, CV_64FC1); CvMat G; cvGetCols(tmpEigenValues, &G, 0, nTruncated); cvCopy(&G, __TextureEigenValues); cvGetRows(tmpEigenVectors, &G, 0, nTruncated); cvCopy(&G, __TextureEigenVectors); cvReleaseMat(&tmpEigenVectors); cvReleaseMat(&tmpEigenValues); LOGD("Done (%d/%d)\n", nTruncated, nEigenAtMost); }
void CvOneWayDescriptor::GenerateSamples(int pose_count, IplImage* frontal, int norm) { /* if(m_transforms) { GenerateSamplesWithTransforms(pose_count, frontal); return; } */ CvRect roi = cvGetImageROI(frontal); IplImage* patch_8u = cvCreateImage(cvSize(roi.width/2, roi.height/2), frontal->depth, frontal->nChannels); for(int i = 0; i < pose_count; i++) { if(!m_transforms) { m_affine_poses[i] = GenRandomAffinePose(); } //AffineTransformPatch(frontal, patch_8u, m_affine_poses[i]); generate_mean_patch(frontal, patch_8u, m_affine_poses[i], num_mean_components, noise_intensity); float scale = 1.0f; if(norm) { float sum = cvSum(patch_8u).val[0]; scale = 1/sum; } cvConvertScale(patch_8u, m_samples[i], scale); #if 0 double maxval; cvMinMaxLoc(m_samples[i], 0, &maxval); IplImage* test = cvCreateImage(cvSize(roi.width/2, roi.height/2), IPL_DEPTH_8U, 1); cvConvertScale(m_samples[i], test, 255.0/maxval); cvNamedWindow("1", 1); cvShowImage("1", test); cvWaitKey(0); #endif } cvReleaseImage(&patch_8u); }
struct POINTS2d* cvImage2Sub( IplImage* img) //works perfectly alright { // img is a binary image with 8bit depth with 0xff for 1 and 0x00 f0r 0; CvScalar lengthCVS; lengthCVS = cvSum( img ); int length = lengthCVS.val[ 0 ]/255; struct POINTS2d* points=NULL; if(( points = ( POINTS2d*) malloc( sizeof( POINTS2d ))) == NULL ) exit( 1 ); points->num = length; if(( points->x = ( int*) malloc( sizeof( int ) * (length + 20) )) == NULL ) exit( 1 ); if(( points->y = ( int*) malloc( sizeof( int ) * (length + 20) )) == NULL ) exit( 1 ); int baseIndex = -1*img->widthStep; int width = img->width; int height = img->height; int widthStep = img->widthStep; int i,j; uchar *imgData = (uchar *)img->imageData; int currentPoint = 0; unsigned char a = 0xff; for( int i =0; i<height; i++ ) { baseIndex +=widthStep; for( int j = 0 ; j<width; j++ ) { if( imgData[ baseIndex+j ] == a ) { points->y[ currentPoint ] = i+1; points->x[ currentPoint ] = j+1; currentPoint++; } } } return points; }
int cvL1QCSolve( CvMat* A, CvMat* B, CvMat* X, double epsilon, double mu, CvTermCriteria lb_term_crit, CvTermCriteria cg_term_crit ) { CvMat* AAt = cvCreateMat( A->rows, A->rows, CV_MAT_TYPE(A->type) ); cvGEMM( A, A, 1, NULL, 0, AAt, CV_GEMM_B_T ); CvMat* W = cvCreateMat( A->rows, 1, CV_MAT_TYPE(X->type) ); if ( cvCGSolve( AAt, B, W, cg_term_crit ) > .5 ) { cvReleaseMat( &W ); cvReleaseMat( &AAt ); return -1; } cvGEMM( A, W, 1, NULL, 0, X, CV_GEMM_A_T ); cvReleaseMat( &W ); cvReleaseMat( &AAt ); CvMat* U = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) ); cvAbsDiffS( X, U, cvScalar(0) ); CvScalar sumAbsX = cvSum( U ); double minAbsX, maxAbsX; cvMinMaxLoc( U, &minAbsX, &maxAbsX ); cvConvertScale( U, U, .95, maxAbsX * .1 ); double tau = MAX( (2 * X->rows + 1) / sumAbsX.val[0], 1 ); if ( !(lb_term_crit.type & CV_TERMCRIT_ITER) ) lb_term_crit.max_iter = ceil( (log(2 * X->rows + 1) - log(lb_term_crit.epsilon) - log(tau)) / log(mu) ); CvTermCriteria nt_term_crit = cvTermCriteria( CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 50, lb_term_crit.epsilon ); for ( int i = 0; i < lb_term_crit.max_iter; ++i ) { icvL1QCNewton( A, B, X, U, epsilon, tau, nt_term_crit, cg_term_crit ); tau *= mu; } cvReleaseMat( &U ); return 0; }
float CvEM::predict( const CvMat* _sample, CvMat* _probs ) const { float* sample_data = 0; void* buffer = 0; int allocated_buffer = 0; int cls = 0; CV_FUNCNAME( "CvEM::predict" ); __BEGIN__; int i, k, dims; int nclusters; int cov_mat_type = params.cov_mat_type; double opt = FLT_MAX; size_t size; CvMat diff, expo; dims = means->cols; nclusters = params.nclusters; CV_CALL( cvPreparePredictData( _sample, dims, 0, params.nclusters, _probs, &sample_data )); // allocate memory and initializing headers for calculating size = sizeof(double) * (nclusters + dims); if( size <= CV_MAX_LOCAL_SIZE ) buffer = cvStackAlloc( size ); else { CV_CALL( buffer = cvAlloc( size )); allocated_buffer = 1; } expo = cvMat( 1, nclusters, CV_64FC1, buffer ); diff = cvMat( 1, dims, CV_64FC1, (double*)buffer + nclusters ); // calculate the probabilities for( k = 0; k < nclusters; k++ ) { const double* mean_k = (const double*)(means->data.ptr + means->step*k); const double* w = (const double*)(inv_eigen_values->data.ptr + inv_eigen_values->step*k); double cur = log_weight_div_det->data.db[k]; CvMat* u = cov_rotate_mats[k]; // cov = u w u' --> cov^(-1) = u w^(-1) u' if( cov_mat_type == COV_MAT_SPHERICAL ) { double w0 = w[0]; for( i = 0; i < dims; i++ ) { double val = sample_data[i] - mean_k[i]; cur += val*val*w0; } } else { for( i = 0; i < dims; i++ ) diff.data.db[i] = sample_data[i] - mean_k[i]; if( cov_mat_type == COV_MAT_GENERIC ) cvGEMM( &diff, u, 1, 0, 0, &diff, CV_GEMM_B_T ); for( i = 0; i < dims; i++ ) { double val = diff.data.db[i]; cur += val*val*w[i]; } } expo.data.db[k] = cur; if( cur < opt ) { cls = k; opt = cur; } /* probability = (2*pi)^(-dims/2)*exp( -0.5 * cur ) */ } if( _probs ) { CV_CALL( cvConvertScale( &expo, &expo, -0.5 )); CV_CALL( cvExp( &expo, &expo )); if( _probs->cols == 1 ) CV_CALL( cvReshape( &expo, &expo, 0, nclusters )); CV_CALL( cvConvertScale( &expo, _probs, 1./cvSum( &expo ).val[0] )); } __END__; if( sample_data != _sample->data.fl ) cvFree( &sample_data ); if( allocated_buffer ) cvFree( &buffer ); return (float)cls; }
void CvBlobTrackerAuto1::Process(IplImage* pImg, IplImage* pMask) { int CurBlobNum = 0; int i; IplImage* pFG = pMask; /* Bump frame counter: */ m_FrameCount++; if(m_TimesFile) { static int64 TickCount = cvGetTickCount(); static double TimeSum = 0; static int Count = 0; Count++; if(Count%100==0) { #ifndef WINCE time_t ltime; time( <ime ); char* stime = ctime( <ime ); #else /* WINCE does not have above POSIX functions (time,ctime) */ const char* stime = " wince "; #endif FILE* out = fopen(m_TimesFile,"at"); double Time; TickCount = cvGetTickCount()-TickCount; Time = TickCount/FREQ; if(out){fprintf(out,"- %sFrame: %d ALL_TIME - %f\n",stime,Count,Time/1000);fclose(out);} TimeSum = 0; TickCount = cvGetTickCount(); } } /* Update BG model: */ TIME_BEGIN() if(m_pFG) { /* If FG detector is needed: */ m_pFG->Process(pImg); pFG = m_pFG->GetMask(); } /* If FG detector is needed. */ TIME_END("FGDetector",-1) m_pFGMask = pFG; /* For external use. */ /*if(m_pFG && m_pFG->GetParam("DebugWnd") == 1) {// debug foreground result IplImage *pFG = m_pFG->GetMask(); if(pFG) { cvNamedWindow("FG",0); cvShowImage("FG", pFG); } }*/ /* Track blobs: */ TIME_BEGIN() if(m_pBT) { int i; m_pBT->Process(pImg, pFG); for(i=m_BlobList.GetBlobNum(); i>0; --i) { /* Update data of tracked blob list: */ CvBlob* pB = m_BlobList.GetBlob(i-1); int BlobID = CV_BLOB_ID(pB); int i = m_pBT->GetBlobIndexByID(BlobID); m_pBT->ProcessBlob(i, pB, pImg, pFG); pB->ID = BlobID; } CurBlobNum = m_pBT->GetBlobNum(); } TIME_END("BlobTracker",CurBlobNum) /* This part should be removed: */ if(m_BTReal && m_pBT) { /* Update blob list (detect new blob for real blob tracker): */ int i; for(i=m_pBT->GetBlobNum(); i>0; --i) { /* Update data of tracked blob list: */ CvBlob* pB = m_pBT->GetBlob(i-1); if(pB && m_BlobList.GetBlobByID(CV_BLOB_ID(pB)) == NULL ) { CvBlobTrackAuto NewB; NewB.blob = pB[0]; NewB.BadFrames = 0; m_BlobList.AddBlob((CvBlob*)&NewB); } } /* Next blob. */ /* Delete blobs: */ for(i=m_BlobList.GetBlobNum(); i>0; --i) { /* Update tracked-blob list: */ CvBlob* pB = m_BlobList.GetBlob(i-1); if(pB && m_pBT->GetBlobByID(CV_BLOB_ID(pB)) == NULL ) { m_BlobList.DelBlob(i-1); } } /* Next blob. */ } /* Update bloblist. */ TIME_BEGIN() if(m_pBTPostProc) { /* Post-processing module: */ int i; for(i=m_BlobList.GetBlobNum(); i>0; --i) { /* Update tracked-blob list: */ CvBlob* pB = m_BlobList.GetBlob(i-1); m_pBTPostProc->AddBlob(pB); } m_pBTPostProc->Process(); for(i=m_BlobList.GetBlobNum(); i>0; --i) { /* Update tracked-blob list: */ CvBlob* pB = m_BlobList.GetBlob(i-1); int BlobID = CV_BLOB_ID(pB); CvBlob* pBN = m_pBTPostProc->GetBlobByID(BlobID); if(pBN && m_UsePPData && pBN->w >= CV_BLOB_MINW && pBN->h >= CV_BLOB_MINH) { /* Set new data for tracker: */ m_pBT->SetBlobByID(BlobID, pBN ); } if(pBN) { /* Update blob list with results from postprocessing: */ pB[0] = pBN[0]; } } } /* Post-processing module. */ TIME_END("PostProcessing",CurBlobNum) /* Blob deleter (experimental and simple): */ TIME_BEGIN() if(pFG) { /* Blob deleter: */ int i; if(!m_BTReal)for(i=m_BlobList.GetBlobNum();i>0;--i) { /* Check all blobs on list: */ CvBlobTrackAuto* pB = (CvBlobTrackAuto*)(m_BlobList.GetBlob(i-1)); int Good = 0; int w=pFG->width; int h=pFG->height; CvRect r = CV_BLOB_RECT(pB); CvMat mat; double aver = 0; double area = CV_BLOB_WX(pB)*CV_BLOB_WY(pB); if(r.x < 0){r.width += r.x;r.x = 0;} if(r.y < 0){r.height += r.y;r.y = 0;} if(r.x+r.width>=w){r.width = w-r.x-1;} if(r.y+r.height>=h){r.height = h-r.y-1;} if(r.width > 4 && r.height > 4 && r.x < w && r.y < h && r.x >=0 && r.y >=0 && r.x+r.width < w && r.y+r.height < h && area > 0) { aver = cvSum(cvGetSubRect(pFG,&mat,r)).val[0] / area; /* if mask in blob area exists then its blob OK*/ if(aver > 0.1*255)Good = 1; } else { pB->BadFrames+=2; } if(Good) { pB->BadFrames = 0; } else { pB->BadFrames++; } } /* Next blob: */ /* Check error count: */ for(i=0; i<m_BlobList.GetBlobNum(); ++i) { CvBlobTrackAuto* pB = (CvBlobTrackAuto*)m_BlobList.GetBlob(i); if(pB->BadFrames>3) { /* Delete such objects */ /* from tracker... */ m_pBT->DelBlobByID(CV_BLOB_ID(pB)); /* ... and from local list: */ m_BlobList.DelBlob(i); i--; } } /* Check error count for next blob. */ } /* Blob deleter. */ TIME_END("BlobDeleter",m_BlobList.GetBlobNum()) /* Update blobs: */ TIME_BEGIN() if(m_pBT) m_pBT->Update(pImg, pFG); TIME_END("BlobTrackerUpdate",CurBlobNum) /* Detect new blob: */ TIME_BEGIN() if(!m_BTReal && m_pBD && pFG && (m_FrameCount > m_FGTrainFrames) ) { /* Detect new blob: */ static CvBlobSeq NewBlobList; CvBlobTrackAuto NewB; NewBlobList.Clear(); if(m_pBD->DetectNewBlob(pImg, pFG, &NewBlobList, &m_BlobList)) { /* Add new blob to tracker and blob list: */ int i; IplImage* pMask = pFG; /*if(0)if(NewBlobList.GetBlobNum()>0 && pFG ) {// erode FG mask (only for FG_0 and MS1||MS2) pMask = cvCloneImage(pFG); cvErode(pFG,pMask,NULL,2); }*/ for(i=0; i<NewBlobList.GetBlobNum(); ++i) { CvBlob* pBN = NewBlobList.GetBlob(i); pBN->ID = m_NextBlobID; if(pBN && pBN->w >= CV_BLOB_MINW && pBN->h >= CV_BLOB_MINH) { CvBlob* pB = m_pBT->AddBlob(pBN, pImg, pMask ); if(pB) { NewB.blob = pB[0]; NewB.BadFrames = 0; m_BlobList.AddBlob((CvBlob*)&NewB); m_NextBlobID++; } } } /* Add next blob from list of detected blob. */ if(pMask != pFG) cvReleaseImage(&pMask); } /* Create and add new blobs and trackers. */ } /* Detect new blob. */ TIME_END("BlobDetector",-1) TIME_BEGIN() if(m_pBTGen) { /* Run track generator: */ for(i=m_BlobList.GetBlobNum(); i>0; --i) { /* Update data of tracked blob list: */ CvBlob* pB = m_BlobList.GetBlob(i-1); m_pBTGen->AddBlob(pB); } m_pBTGen->Process(pImg, pFG); } /* Run track generator: */ TIME_END("TrajectoryGeneration",-1) TIME_BEGIN() if(m_pBTA) { /* Trajectory analysis module: */ int i; for(i=m_BlobList.GetBlobNum(); i>0; i--) m_pBTA->AddBlob(m_BlobList.GetBlob(i-1)); m_pBTA->Process(pImg, pFG); } /* Trajectory analysis module. */ TIME_END("TrackAnalysis",m_BlobList.GetBlobNum()) } /* CvBlobTrackerAuto1::Process */
/// <summary> /// Given image with paragraph of characters, /// finds bounding box, resizes it to new_width and new_height, and if printResult is 1, prints result for each character. /// </summary> /// <params name="imsSrc"> /// Source image which has to be processed. /// </params> /// <params name="new_width"> /// Width of the image to be used for processing. /// </params> /// <params name="new_height"> /// Height of the image to be used for processing. /// </params> /// <params name="printResult"> /// Indicates whether result has be printed, if its 1, result are printed after running k-neares algorithm. /// </params> /// <params name="resultSize"> /// Number of resulting characters identified, size of the array to which result will be pointing to. /// </params> /// <returns> Pointer to array of result. </returns> float* OCR::preprocessPara(IplImage* imgSrc, int new_width, int new_height, int printResult, int* resultSize) { int minY, maxY; int i; int minYFound=0; float result; vector<float> resultVector; float* resultPointer; CvMat data; CvScalar maxVal=cvRealScalar(imgSrc->width * 255); CvScalar val=cvRealScalar(0); //For each col sum, if sum < width*255 then we find the min //then continue to end to search the max, if sum< width*255 then is new max. for (i=0; i< imgSrc->height; i++) { cvGetRow(imgSrc, &data, i); val= cvSum(&data); if(val.val[0] < maxVal.val[0]) { // some data is found! maxY = i; if(!minYFound) { minY = i; minYFound = 1; } } else if(minYFound == 1) { //some data was found previously, but current row 'i' doesn't have any data. //So process from row 'minY' till row maxY int j; int minX, maxX; int minXFound=0; //CvMat data; CvScalar maxValx=cvRealScalar((maxY - minY) * 255); CvScalar valx=cvRealScalar(0); //For each col sum, if sum < width*255 then we find the min //then continue to end to search the max, if sum< width*255 then is new max for (j=0; j< imgSrc->width - 1; j++) { valx=cvRealScalar(0); //instead of taking sum of entire column get sum of sub part of it. cvGetSubRect(imgSrc,&data, cvRect(j,minY,1,maxY-minY)); //cvGetCol(imgSrc, &data, i); valx= cvSum(&data); if(valx.val[0] < maxValx.val[0]) { //Some data found maxX= j; if(!minXFound) { minX= j; minXFound= 1; } } else if(minXFound == 1) { int maxYp; int minYp; int minYpFound = 0; CvScalar maxValyS = cvRealScalar((maxX-minX)*255); CvScalar valyS = cvRealScalar(0); // from minx to maxx and miny to maxy for(int k = minY; k <= maxY; k++) { cvGetSubRect(imgSrc, &data, cvRect(minX, k, maxX-minX,1)); valyS = cvSum(&data); if(valyS.val[0] - maxValyS.val[0]) { maxYp = k; if(minYpFound!=1) { minYp = k; minYpFound = 1; } } } // for(int k=maxY-1; k >= minY; k--) // { // cvGetSubRect(imgSrc, &data, cvRect(minX, k, maxX-minX,1)); // valyS = cvSum(&data); // if(valyS.val[0] < maxValyS.val[0]) // { // maxYp = k+1; // break; // } // } //Some data was found previosly but current column 'j' doesn't have any data. // so from minY to maxY and minX to maxX is the bounding box of character! result = process(imgSrc, new_width, new_height, printResult, cvRect(minX, minYp, maxX-minX, maxYp-minYp)); resultVector.push_back(result); // after finding each result push the result to the vector. // CvPoint pt1,pt2; // pt1.x = minX; // pt1.y = minYp; // pt2.x = minX; // pt2.y = maxYp; // cvLine(imgSrc, pt1, pt2, CV_RGB(0, 0, 0)); // // pt1.x = maxX; // pt2.x = maxX; // // cvLine(imgSrc, pt1, pt2, CV_RGB(0, 0, 0)); // // pt1.x = minX; // pt1.y = minYp; // pt2.x = maxX; // pt2.y = minYp; // // cvLine(imgSrc, pt1, pt2, CV_RGB(0, 0, 0)); // // pt1.y = maxYp; // pt2.y = maxYp; // cvLine(imgSrc, pt1, pt2, CV_RGB(0, 0, 0)); // // cvNamedWindow("scaled result", CV_WINDOW_AUTOSIZE); // cvShowImage("scaled result",imgSrc); // // cvWaitKey(0); minXFound = 0; } } minYFound = 0; } } //If exit from loop was because max height was reached, but minFound has been set, then process from minFound till height. //This will not happen in the ideal examples I take :) *resultSize = resultVector.size(); resultPointer = new float[*resultSize]; int k; for(k = 0; k < *resultSize; k++) { *(resultPointer+k) = resultVector[k]; } return resultPointer; }
int RandomTrees::train(const char* samples_filename, const char* model_filename, const double ratio, double &train_error, double &test_error) { CvMat* data = 0; CvMat* responses = 0; CvMat* var_type = 0; CvMat* sample_idx = 0; this->tree_parameters_.nactive_vars = (int)sqrt(this->number_of_features_); int ok = read_num_class_data( samples_filename, this->number_of_features_, &data, &responses ); int nsamples_all = 0, ntrain_samples = 0; int i = 0; double train_hr = 0, test_hr = 0; CvRTrees forest; CvMat* var_importance = 0; if( !ok ) { cout << "Could not read the sample in" << samples_filename << endl;; return -1; } cout << "The sample file " << samples_filename << " is loaded." << endl; nsamples_all = data->rows; ntrain_samples = (int)(nsamples_all * ratio); // create classifier by using <data> and <responses> cout << "Training the classifier ..." << endl; // 1. create type mask var_type = cvCreateMat( data->cols + 1, 1, CV_8U ); cvSet( var_type, cvScalarAll(CV_VAR_ORDERED) ); cvSetReal1D( var_type, data->cols, CV_VAR_CATEGORICAL ); // 2. create sample_idx sample_idx = cvCreateMat( 1, nsamples_all, CV_8UC1 ); { CvMat mat; cvGetCols( sample_idx, &mat, 0, ntrain_samples ); cvSet( &mat, cvRealScalar(1) ); cvGetCols( sample_idx, &mat, ntrain_samples, nsamples_all ); cvSetZero( &mat ); } // 3. train classifier forest.train( data, CV_ROW_SAMPLE, responses, 0, sample_idx, var_type, 0, this->tree_parameters_); cout << endl; // compute prediction error on train and test data for( i = 0; i < nsamples_all; i++ ) { double r; CvMat sample; cvGetRow( data, &sample, i ); r = forest.predict( &sample ); r = fabs((double)r - responses->data.fl[i]) <= FLT_EPSILON ? 1 : 0; if( i < ntrain_samples ) train_hr += r; else test_hr += r; } test_hr /= (double)(nsamples_all-ntrain_samples); train_hr /= (double)ntrain_samples; train_error = 1 - train_hr; test_error = 1 - test_hr; cout << "Recognition rate: train = " << train_hr*100 << ", test = " << test_hr*100 << endl; cout << "Number of trees: " << forest.get_tree_count() << endl; // Print variable importance var_importance = (CvMat*)forest.get_var_importance(); if( var_importance ) { double rt_imp_sum = cvSum( var_importance ).val[0]; printf("var#\timportance (in %%):\n"); for( i = 0; i < var_importance->cols; i++ ) printf( "%-2d\t%-4.1f\n", i,100.f*var_importance->data.fl[i]/rt_imp_sum); } // Save Random Trees classifier to file if needed if( model_filename ) forest.save( model_filename ); //cvReleaseMat( &var_importance ); //causes a segmentation fault cvReleaseMat( &sample_idx ); cvReleaseMat( &var_type ); cvReleaseMat( &data ); cvReleaseMat( &responses ); return 0; }
DMZ_INTERNAL void best_expiry_seg(IplImage *card_y, uint16_t starting_y_offset, GroupedRectsList &expiry_groups, GroupedRectsList &name_groups) { #if DEBUG_EXPIRY_SEGMENTATION_PERFORMANCE dmz_debug_timer_start(); #endif CvSize card_image_size = cvGetSize(card_y); // Look for vertical line segments -> sobel_image: IplImage *sobel_image = cvCreateImage(card_image_size, IPL_DEPTH_16S, 1); cvSetZero(sobel_image); CvRect below_numbers_rect = cvRect(0, starting_y_offset + kNumberHeight, card_image_size.width, card_image_size.height - (starting_y_offset + kNumberHeight)); cvSetImageROI(card_y, below_numbers_rect); cvSetImageROI(sobel_image, below_numbers_rect); #if DEBUG_EXPIRY_SEGMENTATION_PERFORMANCE dmz_debug_timer_print("set up for Sobel"); #endif llcv_scharr3_dx_abs(card_y, sobel_image); #if DEBUG_EXPIRY_SEGMENTATION_PERFORMANCE dmz_debug_timer_print("do Sobel [Scharr]"); #endif cvResetImageROI(card_y); cvResetImageROI(sobel_image); // Calculate relative vertical-line-segment-ness for each scan line (i.e., cvSum of the [x-axis] Sobel image for that line): int first_stripe_base_row = below_numbers_rect.y + 1; // the "+ 1" represents the tolerance above and below each stripe int last_stripe_base_row = card_image_size.height - (kSmallCharacterHeight + 1); // the "+ 1" represents the tolerance above and below each stripe long line_sum[card_image_size.height]; int left_edge = kSmallCharacterWidth * 3; // there aren't usually any actual characters this far to the left int right_edge = (card_image_size.width * 2) / 3; // beyond here lie logos for (int row = first_stripe_base_row - 1; row < card_image_size.height; row++) { cvSetImageROI(sobel_image, cvRect(left_edge, row, right_edge - left_edge, 1)); line_sum[row] = (long)cvSum(sobel_image).val[0]; } cvResetImageROI(sobel_image); #if DEBUG_EXPIRY_SEGMENTATION_PERFORMANCE dmz_debug_timer_print("line sums"); #endif // Determine the 3 most probable, non-overlapping stripes. (Where "stripe" == kSmallCharacterHeight contiguous scan lines.) // (Two will usually get us expiry and name, but some cards have additional distractions.) #define kNumberOfStripesToTry 3 int row; std::vector<StripeSum> stripe_sums; for (int base_row = first_stripe_base_row; base_row < last_stripe_base_row; base_row++) { long sum = 0; for (int row = base_row; row < base_row + kSmallCharacterHeight; row++) { sum += line_sum[row]; } // Calculate threshold = half the value of the maximum line-sum in the stripe: long threshold = 0; for (row = base_row; row < base_row + kSmallCharacterHeight; row++) { if (line_sum[row] > threshold) { threshold = line_sum[row]; } } threshold = threshold / 2; // Eliminate stripes that have a a much dimmer-than-average sub-stripe at their very top or very bottom: if (line_sum[base_row] + line_sum[base_row + 1] < threshold) { continue; } if (line_sum[base_row + kSmallCharacterHeight - 2] + line_sum[base_row + kSmallCharacterHeight - 1] < threshold) { continue; } // Eliminate stripes that contain a much dimmer-than-average sub-stripe, // since that usually means that we've been fooled into grabbing the bottom // of some card feature and the top of a different card feature. bool isGoodStrip = true; for (row = base_row; row < base_row + kSmallCharacterHeight - 3; row++) { if (line_sum[row + 1] < threshold && line_sum[row + 2] < threshold) { isGoodStrip = false; break; } } if (isGoodStrip) { StripeSum stripe_sum; stripe_sum.base_row = base_row; stripe_sum.sum = sum; stripe_sums.push_back(stripe_sum); } } #if DEBUG_EXPIRY_SEGMENTATION_PERFORMANCE dmz_debug_timer_print("sum stripes"); #endif std::sort(stripe_sums.begin(), stripe_sums.end(), StripeSumCompareDescending()); #if DEBUG_EXPIRY_SEGMENTATION_PERFORMANCE dmz_debug_timer_print("sort stripe sums"); #endif std::vector<StripeSum> probable_stripes; for (std::vector<StripeSum>::iterator stripe_sum = stripe_sums.begin(); stripe_sum != stripe_sums.end(); ++stripe_sum) { bool overlap = false; for (std::vector<StripeSum>::iterator probable_stripe = probable_stripes.begin(); probable_stripe != probable_stripes.end(); ++probable_stripe) { if (probable_stripe->base_row - kSmallCharacterHeight < stripe_sum->base_row && stripe_sum->base_row < probable_stripe->base_row + kSmallCharacterHeight) { overlap = true; break; } } if (!overlap) { probable_stripes.push_back(*stripe_sum); if (probable_stripes.size() >= kNumberOfStripesToTry) { break; } } } #if DEBUG_EXPIRY_SEGMENTATION_PERFORMANCE dmz_debug_timer_print("pick probable stripes"); #endif // For each stripe, find the potential expiry groups and name groups: for (std::vector<StripeSum>::iterator probable_stripe = probable_stripes.begin(); probable_stripe != probable_stripes.end(); ++probable_stripe) { find_character_groups_for_stripe(card_y, sobel_image, probable_stripe->base_row, probable_stripe->sum, expiry_groups, name_groups); } #if DEBUG_EXPIRY_SEGMENTATION_PERFORMANCE dmz_debug_timer_print("find character groups"); dmz_debug_print("Grand Total for Expiry segmentation: %.3f\n", ((float)dmz_debug_timer_stop()) / 1000.0); #endif cvReleaseImage(&sobel_image); }
CV_IMPL CvScalar cvAvg( const void* img, const void* maskarr ) { CvScalar mean = {{0,0,0,0}}; static CvBigFuncTable mean_tab; static CvFuncTable meancoi_tab; static int inittab = 0; CV_FUNCNAME("cvAvg"); __BEGIN__; CvSize size; double scale; if( !maskarr ) { CV_CALL( mean = cvSum(img)); size = cvGetSize( img ); size.width *= size.height; scale = size.width ? 1./size.width : 0; mean.val[0] *= scale; mean.val[1] *= scale; mean.val[2] *= scale; mean.val[3] *= scale; } else { int type, coi = 0; int mat_step, mask_step; CvMat stub, maskstub, *mat = (CvMat*)img, *mask = (CvMat*)maskarr; if( !inittab ) { icvInitMeanMRTable( &mean_tab ); icvInitMeanCnCMRTable( &meancoi_tab ); inittab = 1; } if( !CV_IS_MAT(mat) ) CV_CALL( mat = cvGetMat( mat, &stub, &coi )); if( !CV_IS_MAT(mask) ) CV_CALL( mask = cvGetMat( mask, &maskstub )); if( !CV_IS_MASK_ARR(mask) ) CV_ERROR( CV_StsBadMask, "" ); if( !CV_ARE_SIZES_EQ( mat, mask ) ) CV_ERROR( CV_StsUnmatchedSizes, "" ); type = CV_MAT_TYPE( mat->type ); size = cvGetMatSize( mat ); mat_step = mat->step; mask_step = mask->step; if( CV_IS_MAT_CONT( mat->type & mask->type )) { size.width *= size.height; size.height = 1; mat_step = mask_step = CV_STUB_STEP; } if( CV_MAT_CN(type) == 1 || coi == 0 ) { CvFunc2D_2A1P func; if( CV_MAT_CN(type) > 4 ) CV_ERROR( CV_StsOutOfRange, "The input array must have at most 4 channels unless COI is set" ); func = (CvFunc2D_2A1P)(mean_tab.fn_2d[type]); if( !func ) CV_ERROR( CV_StsBadArg, cvUnsupportedFormat ); IPPI_CALL( func( mat->data.ptr, mat_step, mask->data.ptr, mask_step, size, mean.val )); } else { CvFunc2DnC_2A1P func = (CvFunc2DnC_2A1P)( meancoi_tab.fn_2d[CV_MAT_DEPTH(type)]); if( !func ) CV_ERROR( CV_StsBadArg, cvUnsupportedFormat ); IPPI_CALL( func( mat->data.ptr, mat_step, mask->data.ptr, mask_step, size, CV_MAT_CN(type), coi, mean.val )); } } __END__; return mean; }
void cv_Sum(CvArr* arr, CvScalar* scalar) { CvScalar result = cvSum(arr); for(int i = 0; i < 4; i++) { scalar->val[i] = result.val[i]; } }
static CvTestSeqElem* icvTestSeqReadElemOne(CvTestSeq_* pTS, CvFileStorage* fs, CvFileNode* node) { int noise_type = CV_NOISE_NONE;; CvTestSeqElem* pElem = NULL; const char* pVideoName = cvReadStringByName( fs, node,"Video", NULL); const char* pVideoObjName = cvReadStringByName( fs, node,"VideoObj", NULL); if(pVideoName) { /* Check to noise flag: */ if( cv_stricmp(pVideoName,"noise_gaussian") == 0 || cv_stricmp(pVideoName,"noise_normal") == 0) noise_type = CV_NOISE_GAUSSIAN; if( cv_stricmp(pVideoName,"noise_uniform") == 0) noise_type = CV_NOISE_UNIFORM; if( cv_stricmp(pVideoName,"noise_speckle") == 0) noise_type = CV_NOISE_SPECKLE; if( cv_stricmp(pVideoName,"noise_salt_and_pepper") == 0) noise_type = CV_NOISE_SALT_AND_PEPPER; } if((pVideoName || pVideoObjName ) && noise_type == CV_NOISE_NONE) { /* Read other elements: */ if(pVideoName) pElem = icvTestSeqReadElemAll(pTS, fs, pVideoName); if(pVideoObjName) { CvTestSeqElem* pE; pElem = icvTestSeqReadElemAll(pTS, fs, pVideoObjName); for(pE=pElem;pE;pE=pE->next) { pE->ObjID = pTS->ObjNum; pE->pObjName = pVideoObjName; } pTS->ObjNum++; } } /* Read other elements. */ else { /* Create new element: */ CvFileNode* pPosNode = cvGetFileNodeByName( fs, node,"Pos"); CvFileNode* pSizeNode = cvGetFileNodeByName( fs, node,"Size"); int AutoSize = (pSizeNode && CV_NODE_IS_STRING(pSizeNode->tag) && cv_stricmp("auto",cvReadString(pSizeNode,""))==0); int AutoPos = (pPosNode && CV_NODE_IS_STRING(pPosNode->tag) && cv_stricmp("auto",cvReadString(pPosNode,""))==0); const char* pFileName = cvReadStringByName( fs, node,"File", NULL); pElem = (CvTestSeqElem*)cvAlloc(sizeof(CvTestSeqElem)); memset(pElem,0,sizeof(CvTestSeqElem)); pElem->ObjID = -1; pElem->noise_type = noise_type; cvRandInit( &pElem->rnd_state, 1, 0, 0,CV_RAND_NORMAL); if(pFileName && pElem->noise_type == CV_NOISE_NONE) { /* If AVI or BMP: */ size_t l = strlen(pFileName); pElem->pFileName = pFileName; pElem->type = SRC_TYPE_IMAGE; if(cv_stricmp(".avi",pFileName+l-4) == 0)pElem->type = SRC_TYPE_AVI; if(pElem->type == SRC_TYPE_IMAGE) { //pElem->pImg = cvLoadImage(pFileName); if(pElem->pImg) { pElem->FrameNum = 1; if(pElem->pImgMask)cvReleaseImage(&(pElem->pImgMask)); pElem->pImgMask = cvCreateImage( cvSize(pElem->pImg->width,pElem->pImg->height), IPL_DEPTH_8U,1); icvTestSeqCreateMask(pElem->pImg,pElem->pImgMask,FG_BG_THRESHOLD); } } if(pElem->type == SRC_TYPE_AVI && pFileName) { //pElem->pAVI = cvCaptureFromFile(pFileName); if(pElem->pAVI) { IplImage* pImg = 0;//cvQueryFrame(pElem->pAVI); pElem->pImg = cvCloneImage(pImg); pElem->pImg->origin = 0; //cvSetCaptureProperty(pElem->pAVI,CV_CAP_PROP_POS_FRAMES,0); pElem->FrameBegin = 0; pElem->AVILen = pElem->FrameNum = 0;//(int)cvGetCaptureProperty(pElem->pAVI, CV_CAP_PROP_FRAME_COUNT); //cvReleaseCapture(&pElem->pAVI); pElem->pAVI = NULL; } else { printf("WARNING!!! Cannot open avi file %s\n",pFileName); } } } /* If AVI or BMP. */ if(pPosNode) { /* Read positions: */ if(CV_NODE_IS_SEQ(pPosNode->tag)) { int num = pPosNode->data.seq->total; pElem->pPos = (CvPoint2D32f*)cvAlloc(sizeof(float)*num); cvReadRawData( fs, pPosNode, pElem->pPos, "f" ); pElem->PosNum = num/2; if(pElem->FrameNum == 0) pElem->FrameNum = pElem->PosNum; } } if(pSizeNode) { /* Read sizes: */ if(CV_NODE_IS_SEQ(pSizeNode->tag)) { int num = pSizeNode->data.seq->total; pElem->pSize = (CvPoint2D32f*)cvAlloc(sizeof(float)*num); cvReadRawData( fs, pSizeNode, pElem->pSize, "f" ); pElem->SizeNum = num/2; } } if(AutoPos || AutoSize) { /* Auto size and pos: */ int i; int num = (pElem->type == SRC_TYPE_AVI)?pElem->AVILen:1; if(AutoSize) { pElem->pSize = (CvPoint2D32f*)cvAlloc(sizeof(CvPoint2D32f)*num); pElem->SizeNum = num; } if(AutoPos) { pElem->pPos = (CvPoint2D32f*)cvAlloc(sizeof(CvPoint2D32f)*num); pElem->PosNum = num; } for(i=0; i<num; ++i) { IplImage* pFG = NULL; CvPoint2D32f* pPos = AutoPos?(pElem->pPos + i):NULL; CvPoint2D32f* pSize = AutoSize?(pElem->pSize + i):NULL; icvTestSeqQureyFrameElem(pElem,i); pFG = pElem->pImgMask; if(pPos) { pPos->x = 0.5f; pPos->y = 0.5f; } if(pSize) { pSize->x = 0; pSize->y = 0; } if(pFG) { double M00; CvMoments m; cvMoments( pElem->pImgMask, &m, 0 ); M00 = cvGetSpatialMoment( &m, 0, 0 ); if(M00 > 0 && pSize ) { double X = cvGetSpatialMoment( &m, 1, 0 )/M00; double Y = cvGetSpatialMoment( &m, 0, 1 )/M00; double XX = (cvGetSpatialMoment( &m, 2, 0 )/M00) - X*X; double YY = (cvGetSpatialMoment( &m, 0, 2 )/M00) - Y*Y; pSize->x = (float)(4*sqrt(XX))/(pElem->pImgMask->width-1); pSize->y = (float)(4*sqrt(YY))/(pElem->pImgMask->height-1); } if(M00 > 0 && pPos) { pPos->x = (float)(cvGetSpatialMoment( &m, 1, 0 )/(M00*(pElem->pImgMask->width-1))); pPos->y = (float)(cvGetSpatialMoment( &m, 0, 1 )/(M00*(pElem->pImgMask->height-1))); } if(pPos) { /* Another way to calculate y pos * using object median: */ int y0=0, y1=pFG->height-1; for(y0=0; y0<pFG->height; ++y0) { CvMat m; CvScalar s = cvSum(cvGetRow(pFG, &m, y0)); if(s.val[0] > 255*7) break; } for(y1=pFG->height-1; y1>0; --y1) { CvMat m; CvScalar s = cvSum(cvGetRow(pFG, &m, y1)); if(s.val[0] > 255*7) break; } pPos->y = (y0+y1)*0.5f/(pFG->height-1); } } /* pFG */ } /* Next frame. */ //if(pElem->pAVI) cvReleaseCapture(&pElem->pAVI); pElem->pAVI = NULL; } /* End auto position creation. */ } /* Create new element. */ if(pElem) { /* Read transforms and: */ int FirstFrame, LastFrame; CvTestSeqElem* p=pElem; CvFileNode* pTransNode = NULL; CvFileNode* pS = NULL; int ShiftByPos = 0; int KeyFrames[1024]; CvSeq* pTransSeq = NULL; int KeyFrameNum = 0; pTransNode = cvGetFileNodeByName( fs, node,"Trans"); while( pTransNode && CV_NODE_IS_STRING(pTransNode->tag) && cv_stricmp("auto",cvReadString(pTransNode,""))!=0) { /* Trans is reference: */ pTransNode = cvGetFileNodeByName( fs, NULL,cvReadString(pTransNode,"")); } pS = cvGetFileNodeByName( fs, node,"Shift"); ShiftByPos = 0; pTransSeq = pTransNode?(CV_NODE_IS_SEQ(pTransNode->tag)?pTransNode->data.seq:NULL):NULL; KeyFrameNum = pTransSeq?pTransSeq->total:1; if( (pS && CV_NODE_IS_STRING(pS->tag) && cv_stricmp("auto",cvReadString(pS,""))==0) ||(pTransNode && CV_NODE_IS_STRING(pTransNode->tag) && cv_stricmp("auto",cvReadString(pTransNode,""))==0)) { ShiftByPos = 1; } FirstFrame = pElem->FrameBegin; LastFrame = pElem->FrameBegin+pElem->FrameNum-1; /* Calculate length of video and reallocate * transformation array: */ for(p=pElem; p; p=p->next) { int v; v = cvReadIntByName( fs, node, "BG", -1 ); if(v!=-1)p->BG = v; v = cvReadIntByName( fs, node, "Mask", -1 ); if(v!=-1)p->Mask = v; p->FrameBegin += cvReadIntByName( fs, node, "FrameBegin", 0 ); p->FrameNum = cvReadIntByName( fs, node, "FrameNum", p->FrameNum ); p->FrameNum = cvReadIntByName( fs, node, "Dur", p->FrameNum ); { int LastFrame = cvReadIntByName( fs, node, "LastFrame", p->FrameBegin+p->FrameNum-1 ); p->FrameNum = MIN(p->FrameNum,LastFrame - p->FrameBegin+1); } icvTestSeqAllocTrans(p); { /* New range estimation: */ int LF = p->FrameBegin+p->FrameNum-1; if(p==pElem || FirstFrame > p->FrameBegin)FirstFrame = p->FrameBegin; if(p==pElem || LastFrame < LF)LastFrame = LF; } /* New range estimation. */ } /* End allocate new transfrom array. */ if(ShiftByPos) { for(p=pElem;p;p=p->next) { /* Modify transformation to make autoshift: */ int i; int num = p->FrameNum; assert(num <= p->TransNum); p->TransNum = MAX(1,num); for(i=0; i<num; ++i) { CvTSTrans* pT = p->pTrans+i; //float t = (num>1)?((float)i/(num-1)):0.0f; float newx = p->pPos[i%p->PosNum].x; float newy = p->pPos[i%p->PosNum].y; pT->Shift.x = -newx*pT->Scale.x; pT->Shift.y = -newy*pT->Scale.y; if(p->pImg) { newx *= p->pImg->width-1; newy *= p->pImg->height-1; } pT->T[2] = -(pT->T[0]*newx+pT->T[1]*newy); pT->T[5] = -(pT->T[3]*newx+pT->T[4]*newy); } } /* Modify transformation old. */ } /* Next record. */ /* Initialize frame number array: */ KeyFrames[0] = FirstFrame; if(pTransSeq&&KeyFrameNum>1) { int i0,i1,i; for(i=0; i<KeyFrameNum; ++i) { CvFileNode* pTN = (CvFileNode*)cvGetSeqElem(pTransSeq,i); KeyFrames[i] = cvReadIntByName(fs,pTN,"frame",-1); } if(KeyFrames[0]<0)KeyFrames[0]=FirstFrame; if(KeyFrames[KeyFrameNum-1]<0)KeyFrames[KeyFrameNum-1]=LastFrame; for(i0=0, i1=1; i1<KeyFrameNum;) { int i; for(i1=i0+1; i1<KeyFrameNum && KeyFrames[i1]<0; i1++); assert(i1<KeyFrameNum); assert(i1>i0); for(i=i0+1; i<i1; ++i) { KeyFrames[i] = cvRound(KeyFrames[i0] + (float)(i-i0)*(float)(KeyFrames[i1] - KeyFrames[i0])/(float)(i1-i0)); } i0 = i1; i1++; } /* Next key run. */ } /* Initialize frame number array. */ if(pTransNode || pTransSeq) { /* More complex transform. */ int param; CvFileNode* pTN = pTransSeq?(CvFileNode*)cvGetSeqElem(pTransSeq,0):pTransNode; for(p=pElem; p; p=p->next) { //int trans_num = p->TransNum; for(param=0; param_name[param]; ++param) { const char* name = param_name[param]; float defv = param_defval[param]; if(KeyFrameNum==1) { /* Only one transform record: */ int i; double val; CvFileNode* node = cvGetFileNodeByName( fs, pTN,name); if(node == NULL) continue; val = cvReadReal(node,defv); for(i=0; i<p->TransNum; ++i) { icvUpdateTrans( p->pTrans+i, param, val, p->pImg?(float)(p->pImg->width-1):1.0f, p->pImg?(float)(p->pImg->height-1):1.0f); } } /* Next record. */ else { /* Several transforms: */ int i0,i1; double v0; double v1; CvFileNode* pTN = (CvFileNode*)cvGetSeqElem(pTransSeq,0); v0 = cvReadRealByName(fs, pTN,name,defv); for(i1=1,i0=0; i1<KeyFrameNum; ++i1) { int f0,f1; int i; CvFileNode* pTN = (CvFileNode*)cvGetSeqElem(pTransSeq,i1); CvFileNode* pVN = cvGetFileNodeByName(fs,pTN,name); if(pVN)v1 = cvReadReal(pVN,defv); else if(pVN == NULL && i1 == KeyFrameNum-1) v1 = defv; else continue; f0 = KeyFrames[i0]; f1 = KeyFrames[i1]; if(i1==(KeyFrameNum-1)) f1++; for(i=f0; i<f1; ++i) { double val; double t = (float)(i-f0); int li = i - p->FrameBegin; if(li<0) continue; if(li>= p->TransNum) break; if(KeyFrames[i1]>KeyFrames[i0]) t /=(float)(KeyFrames[i1]-KeyFrames[i0]); val = t*(v1-v0)+v0; icvUpdateTrans( p->pTrans+li, param, val, p->pImg?(float)(p->pImg->width-1):1.0f, p->pImg?(float)(p->pImg->height-1):1.0f); } /* Next transform. */ i0 = i1; v0 = v1; } /* Next value run. */ } /* Several transforms. */ } /* Next parameter. */ } /* Next record. */ } /* More complex transform. */ } /* Read transfroms. */ return pElem; } /* icvTestSeqReadElemOne */
//============================================================================ void AAM_CAM::Train(const file_lists& pts_files, const file_lists& img_files, double scale /* = 1.0 */, double shape_percentage /* = 0.975 */, double texture_percentage /* = 0.975 */, double appearance_percentage /* = 0.975 */) { //building shape and texture distribution model std::vector<AAM_Shape> AllShapes; for(int ii = 0; ii < pts_files.size(); ii++) { AAM_Shape Shape; bool flag = Shape.ReadAnnotations(pts_files[ii]); if(!flag) { IplImage* image = cvLoadImage(img_files[ii].c_str(), -1); Shape.ScaleXY(image->width, image->height); cvReleaseImage(&image); } AllShapes.push_back(Shape); } printf("Build point distribution model...\n"); __shape.Train(AllShapes, scale, shape_percentage); printf("Build warp information of mean shape mesh..."); __Points = cvCreateMat (1, __shape.nPoints(), CV_32FC2); __Storage = cvCreateMemStorage(0); AAM_Shape refShape = __shape.__AAMRefShape/* * scale */; //if(refShape.GetWidth() > 50) // refShape.Scale(50/refShape.GetWidth()); __paw.Train(refShape, __Points, __Storage); printf("[%d by %d, %d triangles, %d*3 pixels]\n", __paw.Width(), __paw.Height(), __paw.nTri(), __paw.nPix()); printf("Build texture distribution model...\n"); __texture.Train(pts_files, img_files, __paw, texture_percentage, true); __pq = cvCreateMat(1, __shape.nModes()+4, CV_64FC1); printf("Build combined appearance model...\n"); int nsamples = pts_files.size(); int npointsby2 = __shape.nPoints()*2; int npixels = __texture.nPixels(); int nfeatures = __shape.nModes() + __texture.nModes(); CvMat* AllAppearances = cvCreateMat(nsamples, nfeatures, CV_64FC1); CvMat* s = cvCreateMat(1, npointsby2, CV_64FC1); CvMat* t = cvCreateMat(1, npixels, CV_64FC1); __MeanS = cvCreateMat(1, npointsby2, CV_64FC1); __MeanG = cvCreateMat(1, npixels, CV_64FC1); cvCopy(__shape.GetMean(), __MeanS); cvCopy(__texture.GetMean(), __MeanG); //calculate ratio of shape to appearance CvScalar Sum1 = cvSum(__shape.__ShapesEigenValues); CvScalar Sum2 = cvSum(__texture.__TextureEigenValues); __WeightsS2T = sqrt(Sum2.val[0] / Sum1.val[0]); printf("Combine shape and texture parameters...\n"); for(int i = 0; i < nsamples; i++) { //Get Shape and Texture respectively IplImage* image = cvLoadImage(img_files[i].c_str(), -1); AAM_Shape Shape; if(!Shape.ReadAnnotations(pts_files[i])) Shape.ScaleXY(image->width, image->height); Shape.Point2Mat(s); AAM_Common::CheckShape(s, image->width, image->height); __paw.CalcWarpTexture(s, image, t); __texture.NormalizeTexture(__MeanG, t); //combine shape and texture parameters CvMat OneAppearance; cvGetRow(AllAppearances, &OneAppearance, i); ShapeTexture2Combined(s, t, &OneAppearance); cvReleaseImage(&image); } //Do PCA of appearances DoPCA(AllAppearances, appearance_percentage); int np = __AppearanceEigenVectors->rows; printf("Extracting the shape and texture part of the combined eigen vectors..\n"); // extract the shape part of the combined eigen vectors CvMat Ps; cvGetCols(__AppearanceEigenVectors, &Ps, 0, __shape.nModes()); __Qs = cvCreateMat(np, npointsby2, CV_64FC1); cvMatMul(&Ps, __shape.GetBases(), __Qs); cvConvertScale(__Qs, __Qs, 1.0/__WeightsS2T); // extract the texture part of the combined eigen vectors CvMat Pg; cvGetCols(__AppearanceEigenVectors, &Pg, __shape.nModes(), nfeatures); __Qg = cvCreateMat(np, npixels, CV_64FC1); cvMatMul(&Pg, __texture.GetBases(), __Qg); __a = cvCreateMat(1, __AppearanceEigenVectors->cols, CV_64FC1); }
static int build_boost_classifier( char* data_filename, char* filename_to_save, char* filename_to_load ) { const int class_count = 26; CvMat* data = 0; CvMat* responses = 0; CvMat* var_type = 0; CvMat* temp_sample = 0; CvMat* weak_responses = 0; int ok = read_num_class_data( data_filename, 16, &data, &responses ); int nsamples_all = 0, ntrain_samples = 0; int var_count; int i, j, k; double train_hr = 0, test_hr = 0; CvBoost boost; if( !ok ) { printf( "Could not read the database %s\n", data_filename ); return -1; } printf( "The database %s is loaded.\n", data_filename ); nsamples_all = data->rows; ntrain_samples = (int)(nsamples_all*0.5); var_count = data->cols; // Create or load Boosted Tree classifier if( filename_to_load ) { // load classifier from the specified file boost.load( filename_to_load ); ntrain_samples = 0; if( !boost.get_weak_predictors() ) { printf( "Could not read the classifier %s\n", filename_to_load ); return -1; } printf( "The classifier %s is loaded.\n", data_filename ); } else { // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // // As currently boosted tree classifier in MLL can only be trained // for 2-class problems, we transform the training database by // "unrolling" each training sample as many times as the number of // classes (26) that we have. // // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CvMat* new_data = cvCreateMat( ntrain_samples*class_count, var_count + 1, CV_32F ); CvMat* new_responses = cvCreateMat( ntrain_samples*class_count, 1, CV_32S ); // 1. unroll the database type mask printf( "Unrolling the database...\n"); for( i = 0; i < ntrain_samples; i++ ) { float* data_row = (float*)(data->data.ptr + data->step*i); for( j = 0; j < class_count; j++ ) { float* new_data_row = (float*)(new_data->data.ptr + new_data->step*(i*class_count+j)); for( k = 0; k < var_count; k++ ) new_data_row[k] = data_row[k]; new_data_row[var_count] = (float)j; new_responses->data.i[i*class_count + j] = responses->data.fl[i] == j+'A'; } } // 2. create type mask var_type = cvCreateMat( var_count + 2, 1, CV_8U ); cvSet( var_type, cvScalarAll(CV_VAR_ORDERED) ); // the last indicator variable, as well // as the new (binary) response are categorical cvSetReal1D( var_type, var_count, CV_VAR_CATEGORICAL ); cvSetReal1D( var_type, var_count+1, CV_VAR_CATEGORICAL ); // 3. train classifier printf( "Training the classifier (may take a few minutes)...\n"); boost.train( new_data, CV_ROW_SAMPLE, new_responses, 0, 0, var_type, 0, CvBoostParams(CvBoost::REAL, 100, 0.95, 5, false, 0 )); cvReleaseMat( &new_data ); cvReleaseMat( &new_responses ); printf("\n"); } temp_sample = cvCreateMat( 1, var_count + 1, CV_32F ); weak_responses = cvCreateMat( 1, boost.get_weak_predictors()->total, CV_32F ); // compute prediction error on train and test data for( i = 0; i < nsamples_all; i++ ) { int best_class = 0; double max_sum = -DBL_MAX; double r; CvMat sample; cvGetRow( data, &sample, i ); for( k = 0; k < var_count; k++ ) temp_sample->data.fl[k] = sample.data.fl[k]; for( j = 0; j < class_count; j++ ) { temp_sample->data.fl[var_count] = (float)j; boost.predict( temp_sample, 0, weak_responses ); double sum = cvSum( weak_responses ).val[0]; if( max_sum < sum ) { max_sum = sum; best_class = j + 'A'; } } r = fabs(best_class - responses->data.fl[i]) < FLT_EPSILON ? 1 : 0; if( i < ntrain_samples ) train_hr += r; else test_hr += r; } test_hr /= (double)(nsamples_all-ntrain_samples); train_hr /= (double)ntrain_samples; printf( "Recognition rate: train = %.1f%%, test = %.1f%%\n", train_hr*100., test_hr*100. ); printf( "Number of trees: %d\n", boost.get_weak_predictors()->total ); // Save classifier to file if needed if( filename_to_save ) boost.save( filename_to_save ); cvReleaseMat( &temp_sample ); cvReleaseMat( &weak_responses ); cvReleaseMat( &var_type ); cvReleaseMat( &data ); cvReleaseMat( &responses ); return 0; }
static int build_rtrees_classifier( char* data_filename, char* filename_to_save, char* filename_to_load ) { CvMat* data = 0; CvMat* responses = 0; CvMat* var_type = 0; CvMat* sample_idx = 0; int ok = read_num_class_data( data_filename, 16, &data, &responses ); int nsamples_all = 0, ntrain_samples = 0; int i = 0; double train_hr = 0, test_hr = 0; CvRTrees forest; CvMat* var_importance = 0; if( !ok ) { printf( "Could not read the database %s\n", data_filename ); return -1; } printf( "The database %s is loaded.\n", data_filename ); nsamples_all = data->rows; ntrain_samples = (int)(nsamples_all*0.8); // Create or load Random Trees classifier if( filename_to_load ) { // load classifier from the specified file forest.load( filename_to_load ); ntrain_samples = 0; if( forest.get_tree_count() == 0 ) { printf( "Could not read the classifier %s\n", filename_to_load ); return -1; } printf( "The classifier %s is loaded.\n", data_filename ); } else { // create classifier by using <data> and <responses> printf( "Training the classifier ...\n"); // 1. create type mask var_type = cvCreateMat( data->cols + 1, 1, CV_8U ); cvSet( var_type, cvScalarAll(CV_VAR_ORDERED) ); cvSetReal1D( var_type, data->cols, CV_VAR_CATEGORICAL ); // 2. create sample_idx sample_idx = cvCreateMat( 1, nsamples_all, CV_8UC1 ); { CvMat mat; cvGetCols( sample_idx, &mat, 0, ntrain_samples ); cvSet( &mat, cvRealScalar(1) ); cvGetCols( sample_idx, &mat, ntrain_samples, nsamples_all ); cvSetZero( &mat ); } // 3. train classifier forest.train( data, CV_ROW_SAMPLE, responses, 0, sample_idx, var_type, 0, CvRTParams(10,10,0,false,15,0,true,4,100,0.01f,CV_TERMCRIT_ITER)); printf( "\n"); } // compute prediction error on train and test data for( i = 0; i < nsamples_all; i++ ) { double r; CvMat sample; cvGetRow( data, &sample, i ); r = forest.predict( &sample ); r = fabs((double)r - responses->data.fl[i]) <= FLT_EPSILON ? 1 : 0; if( i < ntrain_samples ) train_hr += r; else test_hr += r; } test_hr /= (double)(nsamples_all-ntrain_samples); train_hr /= (double)ntrain_samples; printf( "Recognition rate: train = %.1f%%, test = %.1f%%\n", train_hr*100., test_hr*100. ); printf( "Number of trees: %d\n", forest.get_tree_count() ); // Print variable importance var_importance = (CvMat*)forest.get_var_importance(); if( var_importance ) { double rt_imp_sum = cvSum( var_importance ).val[0]; printf("var#\timportance (in %%):\n"); for( i = 0; i < var_importance->cols; i++ ) printf( "%-2d\t%-4.1f\n", i, 100.f*var_importance->data.fl[i]/rt_imp_sum); } //Print some proximitites printf( "Proximities between some samples corresponding to the letter 'T':\n" ); { CvMat sample1, sample2; const int pairs[][2] = {{0,103}, {0,106}, {106,103}, {-1,-1}}; for( i = 0; pairs[i][0] >= 0; i++ ) { cvGetRow( data, &sample1, pairs[i][0] ); cvGetRow( data, &sample2, pairs[i][1] ); printf( "proximity(%d,%d) = %.1f%%\n", pairs[i][0], pairs[i][1], forest.get_proximity( &sample1, &sample2 )*100. ); } } // Save Random Trees classifier to file if needed if( filename_to_save ) forest.save( filename_to_save ); cvReleaseMat( &sample_idx ); cvReleaseMat( &var_type ); cvReleaseMat( &data ); cvReleaseMat( &responses ); return 0; }
detectMotion::detectMotion(char *_videoname, int Threshold) { this->videoname= _videoname; T = Threshold; //IplImage pointers IplImage* pFrame = NULL; IplImage* pFrImg = NULL; IplImage* pBkImg = NULL; //CvMat pointers CvMat* pFrameMat = NULL; CvMat* pFrMat = NULL; CvMat* pBkMat = NULL; //CvVideoWriter pointers CvVideoWriter* vWriter = NULL; CvCapture* pCapture = NULL; int nFrmNum = 0; int Mt = 0; //Motion double TM = 0; //Total motion double AM = 0; //Avarage motion if( !(pCapture = cvCaptureFromFile(videoname))) { fprintf(stderr, "Can not open video file %s\n", videoname); return ; } //For each frame while(pFrame = cvQueryFrame( pCapture )) { nFrmNum++; //memory alloc and variable intial for first frame if(nFrmNum == 1) { //8 bit unsigned int array of size width*height pBkImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,1); pFrImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,1); // pBkMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1); pFrMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1); pFrameMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1); //save to gray image cvCvtColor(pFrame, pBkImg, CV_BGR2GRAY); cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY); //convert gray image to pFrameMat pointer cvConvert(pFrImg, pFrameMat); cvConvert(pFrImg, pFrMat); cvConvert(pFrImg, pBkMat); //double temp = cvGetCaptureProperty(pCapture,CV_CAP_PROP_FPS); //fps //init the writer pointer vWriter = cvCreateVideoWriter ("ccfgvideo.avi", cvGetCaptureProperty(pCapture, CV_CAP_PROP_FOURCC), cvGetCaptureProperty(pCapture, CV_CAP_PROP_FPS ),cvSize (pFrame->width,pFrame->height),1); cvWriteFrame (vWriter, pFrame); } else { //convert to gray image cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY); cvConvert(pFrImg, pFrameMat); //gaussian filter //cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0); //get background frame cvAbsDiff(pFrameMat, pBkMat, pFrMat); //T=60 cvThreshold(pFrMat, pFrImg, T, 255.0, CV_THRESH_BINARY); //erode and dilate to reduce hole in objects cvErode(pFrImg, pFrImg, 0, 1); cvDilate(pFrImg, pFrImg, 0, 1); //calculate Mt and reset fps base on it Mt = (cvSum(pFrImg).val[0]/255); //(pFrame->width*pFrame->height); //cvCreateVideoWriter( const char* filename, int fourcc, double fps, CvSize frame_size, int is_color=1 ); if(Mt>0) { int n=cvWriteFrame (vWriter, pFrame); //write to frame //cout<<n; if (Mt<100) { cvRunningAvg(pFrameMat, pBkMat, 0.05, 0); } else { cvRunningAvg(pFrameMat, pBkMat, 0.8, 0); } } else { cvRunningAvg(pFrameMat, pBkMat, 0.05, 0); nFrmNum-=1; } /*CvScalar Mn; CvScalar SDev; cvAvgSdv(pFrMat,&Mn,&SDev); Mn.val[0];*/ /*if ( SDev.val[0]<1) { cvRunningAvg(pFrameMat, pBkMat, 1, 0); } else { }*/ //renew background //cvRunningAvg(pFrameMat, pBkMat, 1, 0); cvConvert(pBkMat, pBkImg); //wait for key interupt if( cvWaitKey(2) >= 0 ) { cvReleaseVideoWriter(&vWriter); cvReleaseImage(&pFrImg); cvReleaseImage(&pBkImg); cvReleaseMat(&pFrameMat); cvReleaseMat(&pFrMat); cvReleaseMat(&pBkMat); cvReleaseCapture(&pCapture); std::cout<<"Frame number after deleting zero motion frames" <<nFrmNum<<std::endl; break; } // //TM+=Mt; } } //AM=TM/nFrmNum; //Release pointers cvReleaseVideoWriter(&vWriter); cvReleaseImage(&pFrImg); cvReleaseImage(&pBkImg); cvReleaseMat(&pFrameMat); cvReleaseMat(&pFrMat); cvReleaseMat(&pBkMat); //print Mt and Tm cvReleaseCapture(&pCapture); //cout<<"Total Motion = "<<TM<<endl; //cout<<"Average Motion = "<<AM<<endl; std::cout<<"Frame number after deleting zero motion frames" <<nFrmNum<<std::endl; return; }