Exemplo n.º 1
0
void BackgroundDiff(IplImage *I, IplImage*Imask) {
	cvConvertScale(I, Iscratch, 1, 0);
	cvSplit(Iscratch, Igray1, Igray2, Igray3, 0);
	cvInRange(Igray1, Ilow1, Ihi1, Imask);

	cvInRange(Igray2, Ilow2, Ihi2, Imaskt);
	cvOr(Imask, Imaskt, Imask);

	cvInRange(Igray3, Ilow3, Ihi3, Imaskt);
	cvOr(Imask, Imaskt, Imask);
	cvSubRS(Imask, 255, Imask);
}
Exemplo n.º 2
0
// Shade a grayscale image using the "winter" colormap (similar to Matlab's). 
void colorizeWinter(IplImage* src, IplImage*& dst, IplImage* mask){

	// Create an increasing linear-ramp in the green channel.
	cvMerge(NULL, src, NULL, NULL, dst);

	// Create a decreasing linear-ramp in the blue channel.
	IplImage* blue = cvCloneImage(src);
	cvSubRS(src, cvScalar(255.0), blue, mask);
	cvMerge(blue, NULL, NULL, NULL, dst);
	
	// Release allocated resources.
	cvReleaseImage(&blue);
}
Exemplo n.º 3
0
	// Create a binary: 0,255 mask where 255 means forground pixel
	// I		Input image, 3 channel, 8u
	// Imask	mask image to be created, 1 channel 8u
	// num		camera number.
	//
void ofxBackground::backgroundDiff(IplImage *I,IplImage *Imask)  //Mask should be grayscale
{
	cvCvtScale(I,Iscratch,1,0); //To float;
								//Channel 1
	cvCvtPixToPlane( Iscratch, Igray1,Igray2,Igray3, 0 );
	cvInRange(Igray1,Ilow1,Ihi1,Imask);
		//Channel 2
	cvInRange(Igray2,Ilow2,Ihi2,Imaskt);
	cvOr(Imask,Imaskt,Imask);
		//Channel 3
	cvInRange(Igray3,Ilow3,Ihi3,Imaskt);
	cvOr(Imask,Imaskt,Imask);
		//Finally, invert the results
	cvSubRS( Imask, cvScalar(255), Imask);
}
Exemplo n.º 4
0
// Create a binary: 0,255 mask where 255 means forehground pixel
// I      Input image, 3 channel, 8u
// Imask      Mask image to be created, 1 channel 8u
//
void backgroundDiff(IplImage *I,IplImage *Imask)
{
   cvCvtScale(I,Iscratch,1,0); //To float;
   cvSplit( Iscratch, Igray1,Igray2,Igray3, 0 );
   //Channel 1
   cvInRange(Igray1,Ilow1,Ihi1,Imask);
   //Channel 2
   cvInRange(Igray2,Ilow2,Ihi2,Imaskt);
   cvOr(Imask,Imaskt,Imask);
   //Channel 3
   cvInRange(Igray3,Ilow3,Ihi3,Imaskt);
   cvOr(Imask,Imaskt,Imask)
   //Finally, invert the results
   cvSubRS( Imask, 255, Imask);
}
Exemplo n.º 5
0
/*
 * call-seq:
 *   sub(val[,mask])
 *
 * Return new CvScalar if <i>val</i> is CvScalar or compatible object.
 *   self[I] - val[I]
 * Or return new CvMat if <i>val</i> is CvMat or subclass.
 */
VALUE
rb_sub(int argc, VALUE *argv, VALUE self)
{
  VALUE val, mask;
  rb_scan_args(argc, argv, "11", &val, &mask);
  if(rb_obj_is_kind_of(val, cCvMat::rb_class())){
    VALUE dest = cCvMat::new_object(cvGetSize(CVARR(val)), cvGetElemType(CVARR(val)));
    cvSubRS(CVARR(val), *CVSCALAR(self), CVARR(dest), MASK(mask));
    return dest;
  }else{
    CvScalar *src = CVSCALAR(self), scl = VALUE_TO_CVSCALAR(val);
    return new_object(cvScalar(src->val[0] - scl.val[0],
                               src->val[1] - scl.val[1],
                               src->val[2] - scl.val[2],
                               src->val[3] - scl.val[3]));
  }
}
// Create a binary: 0,255 mask where 255 means forground pixel.
//
// Parameters:
//   I:     input image, 3 channel, 8u
//   Imask: mask image to be created, 1 channel 8u
//   num:   camera number
//
void backgroundDiff(IplImage *I, IplImage *Imask, int num)  // Mask should be grayscale
{
	cvCvtScale(I, Iscratch, 1, 0);  // To float.

	// Channel 1
	cvCvtPixToPlane( Iscratch, Igray1, Igray2, Igray3, 0 ); // TODO: book uses cvSplit: check!
	cvInRange( Igray1, Ilow1[num], Ihi1[num], Imask);

	// Channel 2
	cvInRange( Igray2, Ilow2[num], Ihi2[num], Imaskt );
	cvOr( Imask, Imaskt, Imask );

	// Channel 3
	cvInRange( Igray3, Ilow3[num], Ihi3[num], Imaskt );
	cvOr( Imask, Imaskt, Imask );

	// Finally, invert the results.
	cvSubRS( Imask, cvScalar(255), Imask);
}
Exemplo n.º 7
0
//------------------------------------------------------------------------------
// Color Similarity Matrix Calculation
//------------------------------------------------------------------------------
CvMat *colorsim(int nbins, double sigma) {

	CvMat *xc=cvCreateMat(1,nbins, CV_32FC1);
	CvMat *yr=cvCreateMat(nbins,1, CV_32FC1);

	CvMat *x=cvCreateMat(nbins,nbins, CV_32FC1);
	CvMat *y=cvCreateMat(nbins,nbins, CV_32FC1);
	CvMat *m=cvCreateMat(x->rows,x->rows, CV_32FC1);


	// Set x,y directions 
	for (int j=0;j<nbins;j++) {
		cvSetReal2D(xc,0,j,(j+1-0.5)/nbins);
		cvSetReal2D(yr,j,0,(j+1-0.5)/nbins);
	}

	// Set u,v, meshgrids
	for (int i=0;i<x->rows;i++) {
		cvRepeat(xc,x);
		cvRepeat(yr,y);
	}

	CvMat *sub = cvCreateMat(x->rows,y->cols,CV_32FC1);
	cvSub(x,y,sub);
	cvAbs(sub,sub);
	cvMul(sub,sub,sub);
	cvConvertScale(sub,sub,-1.0/(2*sigma*sigma));
	cvExp(sub,sub);
	cvSubRS(sub,cvScalar(1.0),m);

	cvReleaseMat(&xc);
	cvReleaseMat(&yr);
	cvReleaseMat(&x);
	cvReleaseMat(&y);
	cvReleaseMat(&sub);

	return m;
}
Exemplo n.º 8
0
void cv_SubRS(CvArr* src, CvScalar* value, CvArr* dst, const CvArr* mask) {
  cvSubRS(src, *value, dst, mask);
}
Exemplo n.º 9
0
void cvSubRS_glue(const CvArr* src, CV_SCALAR_DECL(value), CvArr* dest, 
		  const CvArr* mask)
{
     cvSubRS(src, CV_SCALAR(value), dest, mask);
}
Exemplo n.º 10
0
static int icvL1QCNewton( CvAAtOpsData& AAtData, CvMat* B, CvMat* X, CvMat* U, double epsilon, double tau, CvTermCriteria nt_term_crit, CvTermCriteria cg_term_crit )
{
	const double alpha = .01;
	const double beta = .5;

	CvMat* R = cvCreateMat( B->rows, B->cols, CV_MAT_TYPE(B->type) );
	AAtData.AOps( X, AAtData.AR, AAtData.userdata );
	cvSub( AAtData.AR, B, R );
	CvMat* fu1 = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) );
	CvMat* fu2 = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) );
	CvMat* lfu1 = cvCreateMat( fu1->rows, fu1->cols, CV_MAT_TYPE(fu1->type) );
	CvMat* lfu2 = cvCreateMat( fu2->rows, fu2->cols, CV_MAT_TYPE(fu2->type) );
	cvSub( U, X, lfu1 );
	cvAdd( X, U, lfu2 );
	cvSubRS( lfu1, cvScalar(0), fu1 );
	cvSubRS( lfu2, cvScalar(0), fu2 );
	double epsilon2 = epsilon * epsilon;
	double tau_inv = 1. / tau;
	double fe = .5 * (cvDotProduct( R, R ) - epsilon2);
	double fe_inv = 1. / fe;
	cvLog( lfu1, lfu1 );
	cvLog( lfu2, lfu2 );
	CvScalar sumU = cvSum( U );
	CvScalar sumfu1 = cvSum( lfu1 );
	CvScalar sumfu2 = cvSum( lfu2 );
	double f = sumU.val[0] - tau_inv * (sumfu1.val[0] + sumfu2.val[0] + log(-fe));

	CvMat* atr = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) );
	CvMat* ntgx = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) );
	CvMat* ntgu = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) );
	CvMat* sig1211 = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) );
	CvMat* sigx = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) );
	CvMat* w1 = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) );
	CvMat* du = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) );
	CvMat* pX = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) );
	CvMat* pU = cvCreateMat( U->rows, U->cols, CV_MAT_TYPE(U->type) );
	CvMat* pR = cvCreateMat( R->rows, R->cols, CV_MAT_TYPE(R->type) );
	CvMat* pfu1 = cvCreateMat( fu1->rows, fu1->cols, CV_MAT_TYPE(fu1->type) );
	CvMat* pfu2 = cvCreateMat( fu2->rows, fu2->cols, CV_MAT_TYPE(fu2->type) );
	CvMat* Adx = cvCreateMat( B->rows, B->cols, CV_MAT_TYPE(B->type) );
	CvMat* dx = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) );
	CvMat* tX = cvCreateMat( X->rows, X->cols, CV_MAT_TYPE(X->type) );

	int result = nt_term_crit.max_iter;

	CvH11OpsData H11OpsData;
	H11OpsData.AOps = AAtData.AOps;
	H11OpsData.AtOps = AAtData.AtOps;
	H11OpsData.AR = AAtData.AR;
	H11OpsData.AtR = AAtData.AtR;
	H11OpsData.userdata = AAtData.userdata;
	H11OpsData.tX = tX;
	H11OpsData.atr = atr;
	H11OpsData.sigx = sigx;

	int t, i;

	for ( t = 0; t < nt_term_crit.max_iter; ++t )
	{
		AAtData.AtOps( R, atr, AAtData.userdata );
		double* atrp = atr->data.db;
		double* fu1p = fu1->data.db;
		double* fu2p = fu2->data.db;
		double* ntgxp = ntgx->data.db;
		double* ntgup = ntgu->data.db;
		double* sig1211p = sig1211->data.db;
		double* sigxp = sigx->data.db;
		double* w1p = w1->data.db;
		double* dup = du->data.db;
		for ( i = 0; i < X->rows; ++i, ++atrp, ++fu1p, ++fu2p, ++ntgxp, ++ntgup, ++sig1211p, ++sigxp, ++w1p, ++dup )
		{
			double fu1_inv = 1. / (*fu1p);
			double fu2_inv = 1. / (*fu2p);
			double ntgxv = fu1_inv - fu2_inv + fe_inv * (*atrp);
			double ntguv = -tau - fu1_inv - fu2_inv;
			double sig11 = fu1_inv * fu1_inv + fu2_inv * fu2_inv;
			double sig12 = -fu1_inv * fu1_inv + fu2_inv * fu2_inv;
			*sig1211p = sig12 / sig11;
			*sigxp = sig11 - sig12 * (*sig1211p);
			*w1p = ntgxv - (*sig1211p) * ntguv;
			*ntgxp = -tau_inv * ntgxv;
			*ntgup = -tau_inv * ntguv;
			*dup = ntguv / sig11;
		}
		H11OpsData.fe_inv = fe_inv;
		H11OpsData.fe_inv_2 = fe_inv * fe_inv;
		if ( cvCGSolve( icvH11Ops, &H11OpsData, w1, dx, cg_term_crit ) > .5 )
		{
			result = t;
			goto __clean_up__;
		}
		AAtData.AOps( dx, Adx, AAtData.userdata );
		dup = du->data.db;
		sig1211p = sig1211->data.db;
		double* dxp = dx->data.db;
		for ( i = 0; i < X->rows; ++i, ++dup, ++sig1211p, ++dxp )
			*dup -= (*sig1211p) * (*dxp);

		/* minimum step size that stays in the interior */
		double aqe = cvDotProduct( Adx, Adx );
		double bqe = 2. * cvDotProduct( R, Adx );
		double cqe = cvDotProduct( R, R ) - epsilon2;
		double smax = MIN( 1, -bqe + sqrt( bqe * bqe - 4 * aqe * cqe ) / (2 * aqe) );
		dup = du->data.db;
		dxp = dx->data.db;
		fu1p = fu1->data.db;
		fu2p = fu2->data.db;
		for ( i = 0; i < X->rows; ++i, ++dup, ++dxp, ++fu1p, ++fu2p )
		{
			if ( (*dxp) - (*dup) > 0 )
				smax = MIN( smax, -(*fu1p) / ((*dxp) - (*dup)) );
			if ( (*dxp) + (*dup) < 0 )
				smax = MIN( smax, (*fu2p) / ((*dxp) + (*dup)) );
		}
		smax *= .99;

		/* backtracking line search */
		bool suffdec = 0;
		int backiter = 0;
		double fep = fe;
		double fp = f;
		double lambda2;
		while (!suffdec)
		{
			cvAddWeighted( X, 1, dx, smax, 0, pX );
			cvAddWeighted( U, 1, du, smax, 0, pU );
			cvAddWeighted( R, 1, Adx, smax, 0, pR );
			cvSub( pU, pX, lfu1 );
			cvAdd( pX, pU, lfu2 );
			cvSubRS( lfu1, cvScalar(0), pfu1 );
			cvSubRS( lfu2, cvScalar(0), pfu2 );
			fep = .5 * (cvDotProduct( pR, pR ) - epsilon2);
			cvLog( lfu1, lfu1 );
			cvLog( lfu2, lfu2 );
			CvScalar sumpU = cvSum( pU );
			CvScalar sumpfu1 = cvSum( pfu1 );
			CvScalar sumpfu2 = cvSum( pfu2 );
			fp = sumpU.val[0] - tau_inv * (sumpfu1.val[0] + sumpfu2.val[0] + log(-fep));
			lambda2 = cvDotProduct( ntgx, dx ) + cvDotProduct( ntgu, du );
			double flin = f + alpha * smax * lambda2;
			suffdec = (fp <= flin);
			smax = beta * smax;
			++backiter;
			if ( backiter > 32 )
			{
				result = t;
				goto __clean_up__;
			}
		}

		/* set up for next iteration */
		cvCopy( pX, X );
		cvCopy( pU, U );
		cvCopy( pR, R );
		cvCopy( pfu1, fu1 );
		cvCopy( pfu2, fu2 );
		fe = fep;
		fe_inv = 1. / fe;
		f = fp;
		lambda2 = -lambda2 * .5;
		if ( lambda2 < nt_term_crit.epsilon )
		{
			result = t + 1;
			break;
		}
	}

__clean_up__:

	cvReleaseMat( &pfu2 );
	cvReleaseMat( &pfu1 );
	cvReleaseMat( &pR );
	cvReleaseMat( &pU );
	cvReleaseMat( &pX );
	cvReleaseMat( &tX );
	cvReleaseMat( &dx );
	cvReleaseMat( &Adx );
	cvReleaseMat( &du );
	cvReleaseMat( &w1 );
	cvReleaseMat( &sigx );
	cvReleaseMat( &sig1211 );
	cvReleaseMat( &ntgu );
	cvReleaseMat( &ntgx );
	cvReleaseMat( &lfu2 );
	cvReleaseMat( &lfu1 );
	cvReleaseMat( &fu2 );
	cvReleaseMat( &fu1 );
	cvReleaseMat( &R );
	return result;
}