示例#1
0
/*! Transform the grid with given homograhy and average colors over
 * triangles.
 */
void LightCollector::averageImage(IplImage *im, CvMat *_homography)
{
	if (avgChannels != im->nChannels) {
		if (avgChannels < im->nChannels) { 
			delete[] avg;
			avg = 0;
		}
		avgChannels = im->nChannels;
	}
	if (!avg) avg = new float[avgChannels*nbTri];
	
	// apply the homography to every mesh vertex
	if (_homography)
		cvMatMul(_homography, vertices, transformed);
	else
		cvCopy(vertices, transformed);
	CvMat r1,r2,r3;
	cvGetRow(transformed, &r1, 0);
	cvGetRow(transformed, &r2, 1);
	cvGetRow(transformed, &r3, 2);
	cvDiv(&r1,&r3,&r1);
	cvDiv(&r2,&r3,&r2);
	
	nbPix=0;
	for (int t=0; t<nbTri;t++) {
		int pts[3][2];
		for (int i=0; i<3; i++) {
			assert(triangles[t*3+i] < transformed->cols);
			pts[i][0] = cvRound(CV_MAT_ELEM(*transformed, float, 0, triangles[t*3+i]));
			pts[i][1] = cvRound(CV_MAT_ELEM(*transformed, float, 1, triangles[t*3+i]));
		}
		nbPix+=stat_triangle(im, pts, avg+t*avgChannels);
	}
}
示例#2
0
bool LightCollector::genGrid(float corners[4][2], int nx, int ny)
{
	if (nx<1 || ny<1) return false;
	if (avg) delete[] avg; avg=0;
	if (vertices) cvReleaseMat(&vertices);
	if (transformed) cvReleaseMat(&transformed);

	// generate vertices
	vertices = cvCreateMat(3, (nx+1)*(ny+1), CV_32FC1);
	transformed = cvCreateMat(3, vertices->cols, CV_32FC1);
	for (int y=0; y<(ny+1); ++y)
		for (int x=0; x<(nx+1); ++x) {
			CV_MAT_ELEM(*vertices, float, 0, y*(nx+1)+x) = float(x)/float(nx);
			CV_MAT_ELEM(*vertices, float, 1, y*(nx+1)+x) = float(y)/float(ny);
			CV_MAT_ELEM(*vertices, float, 2, y*(nx+1)+x) = 1;
		}

	// generate triangles
	nbTri = nx*ny*2;
	triangles = new int[nbTri*3];
	int *tri = triangles;
	for (int y=0; y<ny; ++y)
		for (int x=0; x<nx; ++x) {
			tri[0] = y*(nx+1)+x;
			tri[1] = y*(nx+1)+x+1;
			tri[2] = (y+1)*(nx+1)+x;
			tri+=3;
			tri[0] = y*(nx+1)+x+1;
			tri[1] = (y+1)*(nx+1)+x+1;
			tri[2] = (y+1)*(nx+1)+x;
			tri+=3;
		}

	homography H;
	if (!H.estimate(0, 0, corners[0][0], corners[0][1],
				1, 0, corners[1][0], corners[1][1],
				1, 1, corners[2][0], corners[2][1],
				0, 1, corners[3][0], corners[3][1]))
		return false;

	cvMatMul(&H, vertices, transformed);
	CvMat r1,r2,r3, d1, d2;
	cvGetRow(transformed, &r1, 0);
	cvGetRow(transformed, &r2, 1);
	cvGetRow(transformed, &r3, 2);
	cvGetRow(vertices, &d1, 0);
	cvGetRow(vertices, &d2, 1);
	cvDiv(&r1,&r3,&d1);
	cvDiv(&r2,&r3,&d2);
	return true;
}
double* ObtenerMaximo(IplImage* Imagen, STFrame* FrameData, CvRect Roi) {
	// obtener matriz de distancias normalizadas al background
	if (SHOW_VALIDATION_DATA == 1)
		printf(" \n\n Busqueda del máximo umbral...");
	IplImage* IDif = 0;
	IplImage* peso = 0;
	CvSize size = cvSize(Imagen->width, Imagen->height); // get current frame size
	if (!IDif || IDif->width != size.width || IDif->height != size.height) {
		cvReleaseImage(&IDif);
		cvReleaseImage(&peso);
		IDif = cvCreateImage(cvSize(FrameData->BGModel->width,
				FrameData->BGModel->height), IPL_DEPTH_8U, 1); // imagen diferencia abs(I(pi)-u(p(i))
		peso = cvCreateImage(cvSize(FrameData->BGModel->width,
				FrameData->BGModel->height), IPL_DEPTH_32F, 1);//Imagen resultado wi ( pesos)
		cvZero(IDif);
		cvZero(peso);
	}

	// |I(p)-u(p)|/0(p)
	cvAbsDiff(Imagen, FrameData->BGModel, IDif);
	cvDiv(IDif, FrameData->IDesvf, peso);

	// Buscar máximo
	double* Maximo = 0;
	cvMinMaxLoc(peso, Maximo, 0, 0, 0, FrameData->FG);

	return Maximo;
}
示例#4
0
static void node_composit_exec_cvDiv(void *data, bNode *node, bNodeStack **in, bNodeStack **out)
{
	CvArr* dst;
	CvArr* src1;
	CvArr* src2;

	CV_FUNCNAME( "cvDiv" ); 
	//TODO: Use atach buffers
	if(out[0]->hasoutput==0) return;
	cvSetErrMode(1); //Parent mode error
	__CV_BEGIN__;
	if((in[0]->data)&&(in[1]->data)){
			
		CV_CALL(src1 = in[0]->data);
		CV_CALL(src2 = in[1]->data);
		if(!BOCV_checkAreSameType(src1, src2))
			  CV_ERROR( CV_StsBadArg,"The source inputs are differents");
			
		CV_CALL(dst=BOCV_CreateArrFrom(src2));
		if(dst)		
		{
			CV_CALL(cvDiv(src1, src2, dst,1));
		 	CV_CALL(out[0]->data= dst);
		}
	}
	__CV_END__;
}
示例#5
0
void HarrisBuffer::OpticalFlowFromSMM()
{
  // ref: Laptev et al. CVIU 2007, eq.(8)
  cvMul(cxx, cyy, tmp1);
  cvMul(cxy, cxy, tmp2);
  cvSub(tmp1,tmp2,tmp5);

  cvMul(cyy, cxt, tmp3);
  cvMul(cxy, cyt, tmp4);
  cvSub(tmp3,tmp4,tmp6);

  cvDiv(tmp6,tmp5,OFx);

  cvMul(cxx, cyt, tmp3);
  cvMul(cxy, cxt, tmp4);
  cvSub(tmp3,tmp4,tmp6);

  cvDiv(tmp6,tmp5,OFy);
}
示例#6
0
文件: bmodel.cpp 项目: grafin/Diplom
void BModel::wiener_filter_chanel(IplImage *channel, IplImage *kernel, const double sigma)
{
    IplImage *fKernel  = cvCreateImage(cvGetSize(kernel), IPL_DEPTH_64F, 2);
    IplImage *fChannel = cvCreateImage(cvGetSize(channel), IPL_DEPTH_64F, 2);
    IplImage *answ = cvCreateImage(cvGetSize(channel), IPL_DEPTH_64F, 2);
    IplImage *reFKernel = cvCreateImage(cvGetSize(kernel), IPL_DEPTH_64F, 1);
    IplImage *imFKernel = cvCreateImage(cvGetSize(kernel), IPL_DEPTH_64F, 1);
    IplImage *reFChannel = cvCreateImage(cvGetSize(kernel), IPL_DEPTH_64F, 1);
    IplImage *imFChannel = cvCreateImage(cvGetSize(kernel), IPL_DEPTH_64F, 1);
    IplImage *reAnsw = cvCreateImage(cvGetSize(channel), IPL_DEPTH_64F, 1);
    IplImage *imAnsw = cvCreateImage(cvGetSize(channel), IPL_DEPTH_64F, 1);

    cvDFT(kernel, fKernel, CV_DXT_FORWARD, channel->height);
    cvDFT(channel, fChannel, CV_DXT_FORWARD, channel->height);

    cvMulSpectrums(fChannel, fKernel, answ, CV_DXT_MUL_CONJ);
    cvSplit(answ, reAnsw, imAnsw, 0, 0 );
    cvSplit(fKernel, reFKernel, imFKernel, 0, 0 );

    cvPow(reFKernel, reFKernel, 2);
    cvPow(imFKernel, imFKernel, 2);
    cvAdd(reFKernel, imFKernel, reFKernel, 0);
    cvAddS(reFKernel, cvScalarAll(sigma), reFKernel, 0);

    cvDiv(reAnsw, reFKernel, reAnsw, 1);
    cvDiv(imAnsw, reFKernel, imAnsw, 1);

    cvMerge(reAnsw, imAnsw, NULL, NULL, answ);

    cvDFT(answ, answ, CV_DXT_INV_SCALE, channel->height);
    cvCopy(answ, channel);

    cvReleaseImage(&fKernel);
    cvReleaseImage(&fChannel);
    cvReleaseImage(&answ);
    cvReleaseImage(&reFKernel);
    cvReleaseImage(&imFKernel);
    cvReleaseImage(&reFChannel);
    cvReleaseImage(&imFChannel);
    cvReleaseImage(&reAnsw);
    cvReleaseImage(&imAnsw);
}
示例#7
0
void LightCollector::drawGrid(IplImage *im, CvMat *_homography)
{
	// apply the homography to every mesh vertex
	cvMatMul(_homography, vertices, transformed);
	CvMat r1,r2,r3;
	cvGetRow(transformed, &r1, 0);
	cvGetRow(transformed, &r2, 1);
	cvGetRow(transformed, &r3, 2);
	cvDiv(&r1,&r3,&r1);
	cvDiv(&r2,&r3,&r2);
	
	for (int t=0; t<nbTri;t++) {
		int pts[3][2];
		for (int i=0; i<3; i++) {
			pts[i][0] = cvRound(CV_MAT_ELEM(*transformed, float, 0, triangles[t*3+i]));
			pts[i][1] = cvRound(CV_MAT_ELEM(*transformed, float, 1, triangles[t*3+i]));
		}
		cvLine(im, cvPoint(pts[0][0], pts[0][1]), cvPoint(pts[1][0], pts[1][1]), cvScalarAll(255), 1,4,0);
		cvLine(im, cvPoint(pts[1][0], pts[1][1]), cvPoint(pts[2][0], pts[2][1]), cvScalarAll(255), 1,4,0);
		cvLine(im, cvPoint(pts[2][0], pts[2][1]), cvPoint(pts[0][0], pts[0][1]), cvScalarAll(255), 1,4,0);
	}
}
示例#8
0
//! assuming row vectors (a row is a sample)
void cvSoftmax(CvMat * src, CvMat * dst){
  CV_FUNCNAME("cvSoftmax");
  __BEGIN__;
  CV_ASSERT(cvCountNAN(src)<1);
  cvExp(src,dst);
  CV_ASSERT(cvCountNAN(dst)<1);
  const int dtype = CV_MAT_TYPE(src->type);
  CvMat * sum = cvCreateMat(src->rows,1,dtype);
  CvMat * sum_repeat = cvCreateMat(src->rows,src->cols,dtype);
  cvReduce(dst,sum,-1,CV_REDUCE_SUM);
  CV_ASSERT(cvCountNAN(sum)<1);
  cvRepeat(sum,sum_repeat);
  cvDiv(dst,sum_repeat,dst);
  cvReleaseMat(&sum);
  cvReleaseMat(&sum_repeat);
  __END__;
}
示例#9
0
static void node_composit_exec_cvDiv(void *data, bNode *node, bNodeStack **in, bNodeStack **out) {
    CvArr* dst;
    CvArr* src1;
    CvArr* src2;
    CompBuf *dst_buf;

    if (out[0]->hasoutput == 0) return;
    if ((in[0]->data) && in[1]->data) {
        //Get inuts
        src1 = BOCV_IplImage_attach(in[0]->data);
        src2 = BOCV_IplImage_attach(in[1]->data);

        //Create output
        dst_buf = dupalloc_compbuf(in[0]->data);
        dst = BOCV_IplImage_attach(dst_buf);


        //Check for inputs    
        if (!BOCV_checkAreSameType(src1, src2)) {
            node->error = 1;
            return;
        }
        if (!BOCV_checkSameNChannels(src1, src2)) {
            node->error = 1;
            return;
        }

        cvDiv(src1, src2, dst, in[2]->vec[0]);

        //Output
        out[0]->data = dst_buf;

        //Release memory
        BOCV_IplImage_detach(src1);
        BOCV_IplImage_detach(src2);
        BOCV_IplImage_detach(dst);
    }
}
示例#10
0
void FacePredict::CalcMeanTextureParams(const CvMat* GroupTextures, int group)
{
	int nSamples = GroupTextures->rows;

	CvMat mParams;
	cvGetRow(__TextureParamGroups, &mParams, group);  //resize the mParams

	CvMat* lamda = cvCreateMat(1, __nTextureModes, CV_64FC1);
	CvMat* oneTexture = cvCreateMat(1, GroupTextures->cols, CV_64FC1);

	for (int i = 0; i < nSamples; i++) {
		cvGetRow(GroupTextures, oneTexture, i);
		__texture.CalcParams(oneTexture, lamda);
		cvAdd(&mParams, lamda, &mParams);
	}
	CvMat * size = cvCreateMat(1, __nTextureModes, CV_64FC1);
	for (int i = 0; i < __nTextureModes; i++) 
		cvmSet(size, 0, i, nSamples);
	cvDiv(&mParams, size, &mParams);

	cvReleaseMat(&lamda);
	cvReleaseMat(&size);
	cvReleaseMat(&oneTexture);
}
示例#11
0
void FacePredict::CalcMeanShapeParams(const std::vector<AAM_Shape> &GroupShapes, int group)
{
	int nSamples = GroupShapes.size();
	
	CvMat mParams;
	cvGetRow(__ShapeParamGroups, &mParams, group);

	CvMat* p = cvCreateMat(1, __nShapeModes, CV_64FC1);
	CvMat* pq = cvCreateMat(1, 4+__nShapeModes, CV_64FC1);
	for (int i = 0; i < nSamples; i++) {
		__shape.CalcParams(GroupShapes[i], pq);
		cvGetCols(pq, p, 4, 4+__nShapeModes);
		cvAdd(&mParams, p, &mParams);
	}

	CvMat * size = cvCreateMat(1, __nShapeModes, CV_64FC1);
	for (int i = 0; i < __nShapeModes; i++) 
		cvmSet(size, 0, i, nSamples);
	cvDiv(&mParams, size, &mParams);

	cvReleaseMat(&p);
	cvReleaseMat(&pq);
	cvReleaseMat(&size);
}
示例#12
0
文件: mlem.cpp 项目: glo/ee384b
/* log_weight_div_det[k] = -2*log(weights_k) + log(det(Sigma_k)))

   covs[k] = cov_rotate_mats[k] * cov_eigen_values[k] * (cov_rotate_mats[k])'
   cov_rotate_mats[k] are orthogonal matrices of eigenvectors and
   cov_eigen_values[k] are diagonal matrices (represented by 1D vectors) of eigen values.

   The <alpha_ik> is the probability of the vector x_i to belong to the k-th cluster:
   <alpha_ik> ~ weights_k * exp{ -0.5[ln(det(Sigma_k)) + (x_i - mu_k)' Sigma_k^(-1) (x_i - mu_k)] }
   We calculate these probabilities here by the equivalent formulae:
   Denote
   S_ik = -0.5(log(det(Sigma_k)) + (x_i - mu_k)' Sigma_k^(-1) (x_i - mu_k)) + log(weights_k),
   M_i = max_k S_ik = S_qi, so that the q-th class is the one where maximum reaches. Then
   alpha_ik = exp{ S_ik - M_i } / ( 1 + sum_j!=q exp{ S_ji - M_i })
*/
double CvEM::run_em( const CvVectors& train_data )
{
    CvMat* centered_sample = 0;
    CvMat* covs_item = 0;
    CvMat* log_det = 0;
    CvMat* log_weights = 0;
    CvMat* cov_eigen_values = 0;
    CvMat* samples = 0;
    CvMat* sum_probs = 0;
    log_likelihood = -DBL_MAX;

    CV_FUNCNAME( "CvEM::run_em" );
    __BEGIN__;

    int nsamples = train_data.count, dims = train_data.dims, nclusters = params.nclusters;
    double min_variation = FLT_EPSILON;
    double min_det_value = MAX( DBL_MIN, pow( min_variation, dims ));
    double likelihood_bias = -CV_LOG2PI * (double)nsamples * (double)dims / 2., _log_likelihood = -DBL_MAX;
    int start_step = params.start_step;

    int i, j, k, n;
    int is_general = 0, is_diagonal = 0, is_spherical = 0;
    double prev_log_likelihood = -DBL_MAX / 1000., det, d;
    CvMat whdr, iwhdr, diag, *w, *iw;
    double* w_data;
    double* sp_data;

    if( nclusters == 1 )
    {
        double log_weight;
        CV_CALL( cvSet( probs, cvScalar(1.)) );

        if( params.cov_mat_type == COV_MAT_SPHERICAL )
        {
            d = cvTrace(*covs).val[0]/dims;
            d = MAX( d, FLT_EPSILON );
            inv_eigen_values->data.db[0] = 1./d;
            log_weight = pow( d, dims*0.5 );
        }
        else
        {
            w_data = inv_eigen_values->data.db;

            if( params.cov_mat_type == COV_MAT_GENERIC )
                cvSVD( *covs, inv_eigen_values, *cov_rotate_mats, 0, CV_SVD_U_T );
            else
                cvTranspose( cvGetDiag(*covs, &diag), inv_eigen_values );

            cvMaxS( inv_eigen_values, FLT_EPSILON, inv_eigen_values );
            for( j = 0, det = 1.; j < dims; j++ )
                det *= w_data[j];
            log_weight = sqrt(det);
            cvDiv( 0, inv_eigen_values, inv_eigen_values );
        }

        log_weight_div_det->data.db[0] = -2*log(weights->data.db[0]/log_weight);
        log_likelihood = DBL_MAX/1000.;
        EXIT;
    }

    if( params.cov_mat_type == COV_MAT_GENERIC )
        is_general  = 1;
    else if( params.cov_mat_type == COV_MAT_DIAGONAL )
        is_diagonal = 1;
    else if( params.cov_mat_type == COV_MAT_SPHERICAL )
        is_spherical  = 1;
    /* In the case of <cov_mat_type> == COV_MAT_DIAGONAL, the k-th row of cov_eigen_values
    contains the diagonal elements (variations). In the case of
    <cov_mat_type> == COV_MAT_SPHERICAL - the 0-ths elements of the vectors cov_eigen_values[k]
    are to be equal to the mean of the variations over all the dimensions. */

    CV_CALL( log_det = cvCreateMat( 1, nclusters, CV_64FC1 ));
    CV_CALL( log_weights = cvCreateMat( 1, nclusters, CV_64FC1 ));
    CV_CALL( covs_item = cvCreateMat( dims, dims, CV_64FC1 ));
    CV_CALL( centered_sample = cvCreateMat( 1, dims, CV_64FC1 ));
    CV_CALL( cov_eigen_values = cvCreateMat( inv_eigen_values->rows, inv_eigen_values->cols, CV_64FC1 ));
    CV_CALL( samples = cvCreateMat( nsamples, dims, CV_64FC1 ));
    CV_CALL( sum_probs = cvCreateMat( 1, nclusters, CV_64FC1 ));
    sp_data = sum_probs->data.db;

    // copy the training data into double-precision matrix
    for( i = 0; i < nsamples; i++ )
    {
        const float* src = train_data.data.fl[i];
        double* dst = (double*)(samples->data.ptr + samples->step*i);

        for( j = 0; j < dims; j++ )
            dst[j] = src[j];
    }

    if( start_step != START_M_STEP )
    {
        for( k = 0; k < nclusters; k++ )
        {
            if( is_general || is_diagonal )
            {
                w = cvGetRow( cov_eigen_values, &whdr, k );
                if( is_general )
                    cvSVD( covs[k], w, cov_rotate_mats[k], 0, CV_SVD_U_T );
                else
                    cvTranspose( cvGetDiag( covs[k], &diag ), w );
                w_data = w->data.db;
                for( j = 0, det = 1.; j < dims; j++ )
                    det *= w_data[j];
                if( det < min_det_value )
                {
                    if( start_step == START_AUTO_STEP )
                        det = min_det_value;
                    else
                        EXIT;
                }
                log_det->data.db[k] = det;
            }
            else
            {
                d = cvTrace(covs[k]).val[0]/(double)dims;
                if( d < min_variation )
                {
                    if( start_step == START_AUTO_STEP )
                        d = min_variation;
                    else
                        EXIT;
                }
                cov_eigen_values->data.db[k] = d;
                log_det->data.db[k] = d;
            }
        }

        cvLog( log_det, log_det );
        if( is_spherical )
            cvScale( log_det, log_det, dims );
    }

    for( n = 0; n < params.term_crit.max_iter; n++ )
    {
        if( n > 0 || start_step != START_M_STEP )
        {
            // e-step: compute probs_ik from means_k, covs_k and weights_k.
            CV_CALL(cvLog( weights, log_weights ));

            // S_ik = -0.5[log(det(Sigma_k)) + (x_i - mu_k)' Sigma_k^(-1) (x_i - mu_k)] + log(weights_k)
            for( k = 0; k < nclusters; k++ )
            {
                CvMat* u = cov_rotate_mats[k];
                const double* mean = (double*)(means->data.ptr + means->step*k);
                w = cvGetRow( cov_eigen_values, &whdr, k );
                iw = cvGetRow( inv_eigen_values, &iwhdr, k );
                cvDiv( 0, w, iw );

                w_data = (double*)(inv_eigen_values->data.ptr + inv_eigen_values->step*k);

                for( i = 0; i < nsamples; i++ )
                {
                    double *csample = centered_sample->data.db, p = log_det->data.db[k];
                    const double* sample = (double*)(samples->data.ptr + samples->step*i);
                    double* pp = (double*)(probs->data.ptr + probs->step*i);
                    for( j = 0; j < dims; j++ )
                        csample[j] = sample[j] - mean[j];
                    if( is_general )
                        cvGEMM( centered_sample, u, 1, 0, 0, centered_sample, CV_GEMM_B_T );
                    for( j = 0; j < dims; j++ )
                        p += csample[j]*csample[j]*w_data[is_spherical ? 0 : j];
                    pp[k] = -0.5*p + log_weights->data.db[k];

                    // S_ik <- S_ik - max_j S_ij
                    if( k == nclusters - 1 )
                    {
                        double max_val = 0;
                        for( j = 0; j < nclusters; j++ )
                            max_val = MAX( max_val, pp[j] );
                        for( j = 0; j < nclusters; j++ )
                            pp[j] -= max_val;
                    }
                }
            }

            CV_CALL(cvExp( probs, probs )); // exp( S_ik )
            cvZero( sum_probs );

            // alpha_ik = exp( S_ik ) / sum_j exp( S_ij ),
            // log_likelihood = sum_i log (sum_j exp(S_ij))
            for( i = 0, _log_likelihood = likelihood_bias; i < nsamples; i++ )
            {
                double* pp = (double*)(probs->data.ptr + probs->step*i), sum = 0;
                for( j = 0; j < nclusters; j++ )
                    sum += pp[j];
                sum = 1./MAX( sum, DBL_EPSILON );
                for( j = 0; j < nclusters; j++ )
                {
                    double p = pp[j] *= sum;
                    sp_data[j] += p;
                }
                _log_likelihood -= log( sum );
            }

            // check termination criteria
            if( fabs( (_log_likelihood - prev_log_likelihood) / prev_log_likelihood ) < params.term_crit.epsilon )
                break;
            prev_log_likelihood = _log_likelihood;
        }

        // m-step: update means_k, covs_k and weights_k from probs_ik
        cvGEMM( probs, samples, 1, 0, 0, means, CV_GEMM_A_T );

        for( k = 0; k < nclusters; k++ )
        {
            double sum = sp_data[k], inv_sum = 1./sum;
            CvMat* cov = covs[k], _mean, _sample;

            w = cvGetRow( cov_eigen_values, &whdr, k );
            w_data = w->data.db;
            cvGetRow( means, &_mean, k );
            cvGetRow( samples, &_sample, k );

            // update weights_k
            weights->data.db[k] = sum;

            // update means_k
            cvScale( &_mean, &_mean, inv_sum );

            // compute covs_k
            cvZero( cov );
            cvZero( w );

            for( i = 0; i < nsamples; i++ )
            {
                double p = probs->data.db[i*nclusters + k]*inv_sum;
                _sample.data.db = (double*)(samples->data.ptr + samples->step*i);

                if( is_general )
                {
                    cvMulTransposed( &_sample, covs_item, 1, &_mean );
                    cvScaleAdd( covs_item, cvRealScalar(p), cov, cov );
                }
                else
                    for( j = 0; j < dims; j++ )
                    {
                        double val = _sample.data.db[j] - _mean.data.db[j];
                        w_data[is_spherical ? 0 : j] += p*val*val;
                    }
            }

            if( is_spherical )
            {
                d = w_data[0]/(double)dims;
                d = MAX( d, min_variation );
                w->data.db[0] = d;
                log_det->data.db[k] = d;
            }
            else
            {
                if( is_general )
                    cvSVD( cov, w, cov_rotate_mats[k], 0, CV_SVD_U_T );
                cvMaxS( w, min_variation, w );
                for( j = 0, det = 1.; j < dims; j++ )
                    det *= w_data[j];
                log_det->data.db[k] = det;
            }
        }

        cvConvertScale( weights, weights, 1./(double)nsamples, 0 );
        cvMaxS( weights, DBL_MIN, weights );

        cvLog( log_det, log_det );
        if( is_spherical )
            cvScale( log_det, log_det, dims );
    } // end of iteration process

    //log_weight_div_det[k] = -2*log(weights_k/det(Sigma_k))^0.5) = -2*log(weights_k) + log(det(Sigma_k)))
    if( log_weight_div_det )
    {
        cvScale( log_weights, log_weight_div_det, -2 );
        cvAdd( log_weight_div_det, log_det, log_weight_div_det );
    }

    /* Now finalize all the covariation matrices:
    1) if <cov_mat_type> == COV_MAT_DIAGONAL we used array of <w> as diagonals.
       Now w[k] should be copied back to the diagonals of covs[k];
    2) if <cov_mat_type> == COV_MAT_SPHERICAL we used the 0-th element of w[k]
       as an average variation in each cluster. The value of the 0-th element of w[k]
       should be copied to the all of the diagonal elements of covs[k]. */
    if( is_spherical )
    {
        for( k = 0; k < nclusters; k++ )
            cvSetIdentity( covs[k], cvScalar(cov_eigen_values->data.db[k]));
    }
    else if( is_diagonal )
    {
        for( k = 0; k < nclusters; k++ )
            cvTranspose( cvGetRow( cov_eigen_values, &whdr, k ),
                         cvGetDiag( covs[k], &diag ));
    }
    cvDiv( 0, cov_eigen_values, inv_eigen_values );

    log_likelihood = _log_likelihood;

    __END__;

    cvReleaseMat( &log_det );
    cvReleaseMat( &log_weights );
    cvReleaseMat( &covs_item );
    cvReleaseMat( &centered_sample );
    cvReleaseMat( &cov_eigen_values );
    cvReleaseMat( &samples );
    cvReleaseMat( &sum_probs );

    return log_likelihood;
}
示例#13
0
int main(int argc, char ** argv)
{
int height,width,step,channels;
uchar* data;
uchar* data1;
int i,j,k;
float s;

CvMat *dft_A;
CvMat *dft_B;
CvMat *dft_C;
IplImage* im;
IplImage* im1;

IplImage* image_ReB;
IplImage* image_ImB;

IplImage* image_ReC;
IplImage* image_ImC;
IplImage* complex_ImC;

IplImage* image_ReDen;
IplImage* image_ImDen;

FILE *fp;
fp = fopen("test.txt","w+");

int dft_M,dft_N;
int dft_M1,dft_N1;
CvMat* cvShowDFT();
void cvShowInvDFT();

im1 = cvLoadImage( "kutty-1.jpg",1 );
cvNamedWindow("original-color", 0);
cvShowImage("original-color", im1);
im = cvLoadImage( "kutty-1.jpg", CV_LOAD_IMAGE_GRAYSCALE );
if( !im )
return -1;

cvNamedWindow("original-gray", 0);
cvShowImage("original-gray", im);
// Create blur kernel (non-blind)
//float vals[]={.000625,.000625,.000625,.003125,.003125,.003125,.000625,.000625,.000625};
//float vals[]={-0.167,0.333,0.167,-0.167,.333,.167,-0.167,.333,.167};

float vals[]={.055,.055,.055,.222,.222,.222,.055,.055,.055};
CvMat kernel = cvMat(3, // number of rows
3, // number of columns
CV_32FC1, // matrix data type
vals);
IplImage* k_image_hdr;
IplImage* k_image;

k_image_hdr = cvCreateImageHeader(cvSize(3,3),IPL_DEPTH_64F,2);
k_image = cvCreateImage(cvSize(3,3),IPL_DEPTH_64F,1);
k_image = cvGetImage(&kernel,k_image_hdr);

/*IplImage* k_image;
k_image = cvLoadImage( "kernel4.bmp",0 );*/
cvNamedWindow("blur kernel", 0);

height = k_image->height;
width = k_image->width;
step = k_image->widthStep;

channels = k_image->nChannels;
//data1 = (float *)(k_image->imageData);
data1 = (uchar *)(k_image->imageData);

cvShowImage("blur kernel", k_image);

dft_M = cvGetOptimalDFTSize( im->height - 1 );
dft_N = cvGetOptimalDFTSize( im->width - 1 );

//dft_M1 = cvGetOptimalDFTSize( im->height+99 - 1 );
//dft_N1 = cvGetOptimalDFTSize( im->width+99 - 1 );

dft_M1 = cvGetOptimalDFTSize( im->height+3 - 1 );
dft_N1 = cvGetOptimalDFTSize( im->width+3 - 1 );

// Perform DFT of original image
dft_A = cvShowDFT(im, dft_M1, dft_N1,"original");
//Perform inverse (check & comment out) - Commented as it overwrites dft_A
//cvShowInvDFT(im,dft_A,dft_M1,dft_N1,fp, "original");

// Perform DFT of kernel
dft_B = cvShowDFT(k_image,dft_M1,dft_N1,"kernel");
//Perform inverse of kernel (check & comment out) - commented as it overwrites dft_B
//cvShowInvDFT(k_image,dft_B,dft_M1,dft_N1,fp, "kernel");

// Multiply numerator with complex conjugate
dft_C = cvCreateMat( dft_M1, dft_N1, CV_64FC2 );

printf("%d %d %d %d\n",dft_M,dft_N,dft_M1,dft_N1);

// Multiply DFT(blurred image) * complex conjugate of blur kernel
cvMulSpectrums(dft_A,dft_B,dft_C,CV_DXT_MUL_CONJ);

// Split Fourier in real and imaginary parts
image_ReC = cvCreateImage( cvSize(dft_N1, dft_M1), IPL_DEPTH_64F, 1);
image_ImC = cvCreateImage( cvSize(dft_N1, dft_M1), IPL_DEPTH_64F, 1);
complex_ImC = cvCreateImage( cvSize(dft_N1, dft_M1), IPL_DEPTH_64F, 2);

printf("%d %d %d %d\n",dft_M,dft_N,dft_M1,dft_N1);

//cvSplit( dft_C, image_ReC, image_ImC, 0, 0 );
cvSplit( dft_C, image_ReC, image_ImC, 0, 0 );

// Compute A^2 + B^2 of denominator or blur kernel
image_ReB = cvCreateImage( cvSize(dft_N1, dft_M1), IPL_DEPTH_64F, 1);
image_ImB = cvCreateImage( cvSize(dft_N1, dft_M1), IPL_DEPTH_64F, 1);

// Split Real and imaginary parts
cvSplit( dft_B, image_ReB, image_ImB, 0, 0 );
cvPow( image_ReB, image_ReB, 2.0);
cvPow( image_ImB, image_ImB, 2.0);
cvAdd(image_ReB, image_ImB, image_ReB,0);

//Divide Numerator/A^2 + B^2
cvDiv(image_ReC, image_ReB, image_ReC, 1.0);
cvDiv(image_ImC, image_ReB, image_ImC, 1.0);

// Merge Real and complex parts
cvMerge(image_ReC, image_ImC, NULL, NULL, complex_ImC);

// Perform Inverse
cvShowInvDFT(im, complex_ImC,dft_M1,dft_N1,fp,"deblur");
cvWaitKey(-1);
return 0;
}
示例#14
0
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
CvMat *tgso (CvMat &tmap, int ntex, double sigma, double theta, CvMat &tsim, int useChi2) {


	CvMat *roundTmap=cvCreateMat(tmap.rows,tmap.cols,CV_32FC1);
	CvMat *comp=cvCreateMat(tmap.rows,tmap.cols,CV_32FC1);

	for (int i=0;i<tmap.rows;i++)
		for (int j=0;j<tmap.cols;j++)
			cvSetReal2D(roundTmap,i,j,cvRound(cvGetReal2D(&tmap,i,j)));

	cvSub(&tmap,roundTmap,comp);
	if (cvCountNonZero(comp)) {
		printf("texton labels not integral");
		cvReleaseMat(&roundTmap);
		cvReleaseMat(&comp);
		exit(1);
	}

	double min,max;
	cvMinMaxLoc(&tmap,&min,&max);
	if (min<1 && max>ntex) {
		char *msg=new char[50];
		printf(msg,"texton labels out of range [1,%d]",ntex);
		cvReleaseMat(&roundTmap);
		cvReleaseMat(&comp);
		exit(1);
	}

	cvReleaseMat(&roundTmap);
	cvReleaseMat(&comp);


	double wr=floor(sigma); //sigma=radius (Leo) 

	CvMat *x=cvCreateMat(1,wr-(-wr)+1, CV_64FC1);
	CvMat *y=cvCreateMat(wr-(-wr)+1,1, CV_64FC1);

	CvMat *u=cvCreateMat(wr-(-wr)+1,wr-(-wr)+1, CV_64FC1);
	CvMat *v=cvCreateMat(wr-(-wr)+1,wr-(-wr)+1, CV_64FC1);
	CvMat *gamma=cvCreateMat(u->rows,v->rows, CV_64FC1);

	// Set x,y directions 
	for (int j=-wr;j<=wr;j++) {
		cvSetReal2D(x,0,(j+wr),j);
		cvSetReal2D(y,(j+wr),0,j);
	}

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

	// Compute the gamma matrix from the grid
	for (int i=0;i<u->rows;i++) 
		for (int j=0;j<u->cols;j++)
			cvSetReal2D(gamma,i,j,atan2(cvGetReal2D(v,i,j),cvGetReal2D(u,i,j)));

	cvReleaseMat(&x);
	cvReleaseMat(&y);

	CvMat *sum=cvCreateMat(u->rows,u->cols, CV_64FC1);
	cvMul(u,u,u);
	cvMul(v,v,v);
	cvAdd(u,v,sum);
	CvMat *mask=cvCreateMat(u->rows,u->cols, CV_8UC1);
	cvCmpS(sum,sigma*sigma,mask,CV_CMP_LE);
	cvConvertScale(mask,mask,1.0/255);
	cvSetReal2D(mask,wr,wr,0);
	int count=cvCountNonZero(mask);

	cvReleaseMat(&u);
	cvReleaseMat(&v);
	cvReleaseMat(&sum);

	CvMat *sub=cvCreateMat(mask->rows,mask->cols, CV_64FC1);
	CvMat *side=cvCreateMat(mask->rows,mask->cols, CV_8UC1);

	cvSubS(gamma,cvScalar(theta),sub);
	cvReleaseMat(&gamma);

	for (int i=0;i<mask->rows;i++){
		for (int j=0;j<mask->cols;j++) {
			double n=cvmGet(sub,i,j);
			double n_mod = n-floor(n/(2*M_PI))*2*M_PI;
			cvSetReal2D(side,i,j, 1 + int(n_mod < M_PI));
		}
	}

	cvMul(side,mask,side);
	cvReleaseMat(&sub);
	cvReleaseMat(&mask);

	CvMat *lmask=cvCreateMat(side->rows,side->cols, CV_8UC1);
	CvMat *rmask=cvCreateMat(side->rows,side->cols, CV_8UC1);
	cvCmpS(side,1,lmask,CV_CMP_EQ);
	cvCmpS(side,2,rmask,CV_CMP_EQ);
	int count1=cvCountNonZero(lmask), count2=cvCountNonZero(rmask);
	if (count1 != count2) {
		printf("Bug: imbalance\n");
	}

	CvMat *rlmask=cvCreateMat(side->rows,side->cols, CV_32FC1);
	CvMat *rrmask=cvCreateMat(side->rows,side->cols, CV_32FC1);
	cvConvertScale(lmask,rlmask,1.0/(255*count)*2);
	cvConvertScale(rmask,rrmask,1.0/(255*count)*2);


	cvReleaseMat(&lmask);
	cvReleaseMat(&rmask);
	cvReleaseMat(&side);

	int h=tmap.rows;
	int w=tmap.cols;


	CvMat *d       = cvCreateMat(h*w,ntex,CV_32FC1);
	CvMat *coltemp = cvCreateMat(h*w,1,CV_32FC1);
	CvMat *tgL     = cvCreateMat(h,w, CV_32FC1);
	CvMat *tgR     = cvCreateMat(h,w, CV_32FC1);
	CvMat *temp    = cvCreateMat(h,w,CV_8UC1);
	CvMat *im      = cvCreateMat(h,w, CV_32FC1);
	CvMat *sub2    = cvCreateMat(h,w,CV_32FC1);
	CvMat *sub2t   = cvCreateMat(w,h,CV_32FC1);
	CvMat *prod    = cvCreateMat(h*w,ntex,CV_32FC1);
	CvMat reshapehdr,*reshape;

	CvMat* tgL_pad = cvCreateMat(h+rlmask->rows-1,w+rlmask->cols-1,CV_32FC1);
	CvMat* tgR_pad = cvCreateMat(h+rlmask->rows-1,w+rlmask->cols-1,CV_32FC1);
	CvMat* im_pad  = cvCreateMat(h+rlmask->rows-1,w+rlmask->cols-1,CV_32FC1);

	CvMat *tg=cvCreateMat(h,w,CV_32FC1);
	cvZero(tg);
	
	if (useChi2 == 1){
		CvMat* temp_add1 = cvCreateMat(h,w,CV_32FC1);
		for (int i=0;i<ntex;i++) {
			cvCmpS(&tmap,i+1,temp,CV_CMP_EQ); 
			cvConvertScale(temp,im,1.0/255);

			cvCopyMakeBorder(tgL,tgL_pad,cvPoint((rlmask->cols-1)/2,(rlmask->rows-1)/2),IPL_BORDER_CONSTANT);
			cvCopyMakeBorder(tgR,tgR_pad,cvPoint((rlmask->cols-1)/2,(rlmask->rows-1)/2),IPL_BORDER_CONSTANT);
			cvCopyMakeBorder(im,im_pad,cvPoint((rlmask->cols-1)/2,(rlmask->rows-1)/2),IPL_BORDER_CONSTANT);

			cvFilter2D(im_pad,tgL_pad,rlmask,cvPoint((rlmask->cols-1)/2,(rlmask->rows-1)/2));
			cvFilter2D(im_pad,tgR_pad,rrmask,cvPoint((rlmask->cols-1)/2,(rlmask->rows-1)/2));

			cvGetSubRect(tgL_pad,tgL,cvRect((rlmask->cols-1)/2,(rlmask->rows-1)/2,tgL->cols,tgL->rows));
			cvGetSubRect(tgR_pad,tgR,cvRect((rlmask->cols-1)/2,(rlmask->rows-1)/2,tgR->cols,tgR->rows));

			cvSub(tgL,tgR,sub2);
			cvPow(sub2,sub2,2.0);
			cvAdd(tgL,tgR,temp_add1);
			cvAddS(temp_add1,cvScalar(0.0000000001),temp_add1);
			cvDiv(sub2,temp_add1,sub2);
			cvAdd(tg,sub2,tg);
		}
		cvScale(tg,tg,0.5);

		cvReleaseMat(&temp_add1);

	}
	else{// if not chi^2
		for (int i=0;i<ntex;i++) {
			cvCmpS(&tmap,i+1,temp,CV_CMP_EQ); 
			cvConvertScale(temp,im,1.0/255);

			cvCopyMakeBorder(tgL,tgL_pad,cvPoint((rlmask->cols-1)/2,(rlmask->rows-1)/2),IPL_BORDER_CONSTANT);
			cvCopyMakeBorder(tgR,tgR_pad,cvPoint((rlmask->cols-1)/2,(rlmask->rows-1)/2),IPL_BORDER_CONSTANT);
			cvCopyMakeBorder(im,im_pad,cvPoint((rlmask->cols-1)/2,(rlmask->rows-1)/2),IPL_BORDER_CONSTANT);

			cvFilter2D(im_pad,tgL_pad,rlmask,cvPoint((rlmask->cols-1)/2,(rlmask->rows-1)/2));
			cvFilter2D(im_pad,tgR_pad,rrmask,cvPoint((rlmask->cols-1)/2,(rlmask->rows-1)/2));

			cvGetSubRect(tgL_pad,tgL,cvRect((rlmask->cols-1)/2,(rlmask->rows-1)/2,tgL->cols,tgL->rows));
			cvGetSubRect(tgR_pad,tgR,cvRect((rlmask->cols-1)/2,(rlmask->rows-1)/2,tgR->cols,tgR->rows));

			cvSub(tgL,tgR,sub2);
			cvAbs(sub2,sub2);
			cvTranspose(sub2,sub2t);
			reshape=cvReshape(sub2t,&reshapehdr,0,h*w);
			cvGetCol(d,coltemp,i);
			cvCopy(reshape,coltemp);
		}

		cvMatMul(d,&tsim,prod);
		cvMul(prod,d,prod);


		CvMat *sumcols=cvCreateMat(h*w,1,CV_32FC1);
		cvSetZero(sumcols);
		for (int i=0;i<prod->cols;i++) {
			cvGetCol(prod,coltemp,i);
			cvAdd(sumcols,coltemp,sumcols);
		}

		reshape=cvReshape(sumcols,&reshapehdr,0,w);
		cvTranspose(reshape,tg);

		cvReleaseMat(&sumcols);
	}


	//Smooth the gradient now!!
	tg=fitparab(*tg,sigma,sigma/4,theta);
	cvMaxS(tg,0,tg); 

	
	cvReleaseMat(&im_pad);
	cvReleaseMat(&tgL_pad);
	cvReleaseMat(&tgR_pad);
	cvReleaseMat(&rlmask);
	cvReleaseMat(&rrmask);
	cvReleaseMat(&im);
	cvReleaseMat(&tgL);
	cvReleaseMat(&tgR);
	cvReleaseMat(&temp);
	cvReleaseMat(&coltemp);
	cvReleaseMat(&sub2);
	cvReleaseMat(&sub2t);
	cvReleaseMat(&d);
	cvReleaseMat(&prod);

	return tg;

}
示例#15
0
gint compose_skin_matrix(IplImage* rgbin, IplImage* gray_out)
{
/*
  int skin_under_seed = 0;

  static IplImage* imageHSV = cvCreateImage( cvSize(rgbin->width, rgbin->height), IPL_DEPTH_8U, 3);
  cvCvtColor(rgbin, imageHSV, CV_RGB2HSV);

  static IplImage* planeH = cvCreateImage( cvGetSize(imageHSV), 8, 1);	// Hue component.
  ///IplImage* planeH2= cvCreateImage( cvGetSize(imageHSV), 8, 1);	// Hue component, 2nd threshold
  static IplImage* planeS = cvCreateImage( cvGetSize(imageHSV), 8, 1);	// Saturation component.
  static IplImage* planeV = cvCreateImage( cvGetSize(imageHSV), 8, 1);	// Brightness component.
  cvCvtPixToPlane(imageHSV, planeH, planeS, planeV, 0);	// Extract the 3 color components.

  // Detect which pixels in each of the H, S and V channels are probably skin pixels.
  // Assume that skin has a Hue between 0 to 18 (out of 180), and Saturation above 50, and Brightness above 80.
  ///cvThreshold(planeH , planeH2, 10, UCHAR_MAX, CV_THRESH_BINARY);         //(hue > 10)
  cvThreshold(planeH , planeH , 20, UCHAR_MAX, CV_THRESH_BINARY_INV);     //(hue < 20)
  cvThreshold(planeS , planeS , 48, UCHAR_MAX, CV_THRESH_BINARY);         //(sat > 48)
  cvThreshold(planeV , planeV , 80, UCHAR_MAX, CV_THRESH_BINARY);         //(val > 80)

  // erode the HUE to get rid of noise.
  cvErode(planeH, planeH, NULL, 1);

  // Combine all 3 thresholded color components, so that an output pixel will only
  // be white (255) if the H, S and V pixels were also white.

  // gray_out = (hue > 10) ^ (hue < 20) ^ (sat > 48) ^ (val > 80), where   ^ mean pixels-wise AND
  cvAnd(planeH  , planeS , gray_out);	
  //cvAnd(gray_out, planeH2, gray_out);	
  cvAnd(gray_out, planeV , gray_out);	

  return(skin_under_seed);
*/

  static IplImage* planeR  = cvCreateImage( cvGetSize(rgbin), 8, 1);	// R component.
  static IplImage* planeG  = cvCreateImage( cvGetSize(rgbin), 8, 1);	// G component.
  static IplImage* planeB  = cvCreateImage( cvGetSize(rgbin), 8, 1);	// B component.

  static IplImage* planeAll = cvCreateImage( cvGetSize(rgbin), IPL_DEPTH_32F, 1);	// (R+G+B) component.
  static IplImage* planeR2  = cvCreateImage( cvGetSize(rgbin), IPL_DEPTH_32F, 1);	// R component, 32bits
  static IplImage* planeRp  = cvCreateImage( cvGetSize(rgbin), IPL_DEPTH_32F, 1);	// R' and >0.4
  static IplImage* planeGp  = cvCreateImage( cvGetSize(rgbin), IPL_DEPTH_32F, 1);	// G' and > 0.28

  static IplImage* planeRp2 = cvCreateImage( cvGetSize(rgbin), IPL_DEPTH_32F, 1);	// R' <0.6
  static IplImage* planeGp2 = cvCreateImage( cvGetSize(rgbin), IPL_DEPTH_32F, 1);	// G' <0.4

  cvCvtPixToPlane(rgbin, planeR, planeG, planeB, 0);	// Extract the 3 color components.
  cvAdd( planeR, planeG,   planeAll, NULL);
  cvAdd( planeB, planeAll, planeAll, NULL);  // All = R + G + B
  cvDiv( planeR, planeAll, planeRp, 1.0);    // R' = R / ( R + G + B)
  cvDiv( planeG, planeAll, planeGp, 1.0);    // G' = G / ( R + G + B)

  cvConvertScale( planeR, planeR2, 1.0, 0.0);
  cvCopy(planeGp, planeGp2, NULL);
  cvCopy(planeRp, planeRp2, NULL);

  cvThreshold(planeR2 , planeR2,   60, UCHAR_MAX, CV_THRESH_BINARY);     //(R > 60)
  cvThreshold(planeRp , planeRp, 0.40, UCHAR_MAX, CV_THRESH_BINARY);     //(R'> 0.4)
  cvThreshold(planeRp2, planeRp2, 0.6, UCHAR_MAX, CV_THRESH_BINARY_INV); //(R'< 0.6)
  cvThreshold(planeGp , planeGp, 0.28, UCHAR_MAX, CV_THRESH_BINARY);     //(G'> 0.28)
  cvThreshold(planeGp2, planeGp2, 0.4, UCHAR_MAX, CV_THRESH_BINARY_INV); //(G'< 0.4)

  // R’ = R / (R+G+B), G’ = G / (R + G + B)
  //  Skin pixel if:
  // R > 60 AND R’ > 0.4 AND R’ < 0.6 AND G’ > 0.28 and G’ < 0.4  
  static IplImage* imageSkinPixels = cvCreateImage( cvGetSize(rgbin), IPL_DEPTH_32F, 1); 
  cvAnd( planeR2 ,         planeRp , imageSkinPixels);	
  cvAnd( planeRp , imageSkinPixels , imageSkinPixels);	
  cvAnd( planeRp2, imageSkinPixels , imageSkinPixels);	
  cvAnd( planeGp , imageSkinPixels , imageSkinPixels);	
  cvAnd( planeGp2, imageSkinPixels , imageSkinPixels);	
  
  cvConvertScale( imageSkinPixels, gray_out, 1.0, 0.0);
  return(0);
}
示例#16
0
int main(int argc, char ** argv)
{
    int height,width,step,channels,depth;
    uchar* data1;

    CvMat *dft_A;
    CvMat *dft_B;

    CvMat *dft_C;
    IplImage* im;
    IplImage* im1;

    IplImage* image_ReB;
    IplImage* image_ImB;

    IplImage* image_ReC;
    IplImage* image_ImC;
    IplImage* complex_ImC;
    CvScalar val;

    IplImage* k_image_hdr;
    int i,j,k;

    FILE *fp;
    fp = fopen("test.txt","w+");

    int dft_M,dft_N;
    int dft_M1,dft_N1;

    CvMat* cvShowDFT1(IplImage*, int, int,char*);
    void cvShowInvDFT1(IplImage*, CvMat*, int, int,char*);

    im1 = cvLoadImage( "../homer.jpg",1 );

    cvNamedWindow("Original-color", 0);
    cvShowImage("Original-color", im1);

    im = cvLoadImage( "../homer.jpg", CV_LOAD_IMAGE_GRAYSCALE );

    if( !im )
        return -1;

    cvNamedWindow("Original-gray", 0);
    cvShowImage("Original-gray", im);

    // Create a random noise matrix
    fp = fopen("test.txt","w+");
    int val_noise[357*383];
    for(i=0; i <im->height;i++){
        for(j=0;j<im->width;j++){
            fprintf(fp, "%d ",(383*i+j));
            val_noise[383*i+j] = rand() % 128;
        }
        fprintf(fp, "/n");
    }

    CvMat noise = cvMat(im->height,im->width, CV_8UC1,val_noise);

    // Add the random noise matric to the image
    cvAdd(im,&noise,im, 0);

    cvNamedWindow("Original + Noise", 0);
    cvShowImage("Original + Noise", im);

    cvSmooth( im, im, CV_GAUSSIAN, 7, 7, 0.5, 0.5 );
    cvNamedWindow("Gaussian Smooth", 0);
    cvShowImage("Gaussian Smooth", im);

    // Create a blur kernel
    IplImage* k_image;
    float r = rad;
    float radius=((int)(r)*2+1)/2.0;

    int rowLength=(int)(2*radius);
    printf("rowlength %d/n",rowLength);
    float kernels[rowLength*rowLength];
    printf("rowl: %i",rowLength);
    int norm=0; //Normalization factor
    int x,y;
    CvMat kernel;
    for(x = 0; x < rowLength; x++)
        for (y = 0; y < rowLength; y++)
            if (sqrt((x - (int)(radius) ) * (x - (int)(radius) ) + (y - (int)(radius))* (y - (int)(radius))) <= (int)(radius))
                norm++;
    // Populate matrix
    for (y = 0; y < rowLength; y++) //populate array with values
    {
        for (x = 0; x < rowLength; x++) {
            if (sqrt((x - (int)(radius) ) * (x - (int)(radius) ) + (y - (int)(radius))
                     * (y - (int)(radius))) <= (int)(radius)) {
                //kernels[y * rowLength + x] = 255;
                kernels[y * rowLength + x] =1.0/norm;
                printf("%f ",1.0/norm);
            }
            else{
                kernels[y * rowLength + x] =0;
            }
        }
    }

    kernel= cvMat(rowLength, // number of rows
                  rowLength, // number of columns
                  CV_32FC1, // matrix data type
                  &kernels);
    k_image_hdr = cvCreateImageHeader( cvSize(rowLength,rowLength), IPL_DEPTH_32F,1);
    k_image = cvGetImage(&kernel,k_image_hdr);

    height = k_image->height;
    width = k_image->width;
    step = k_image->widthStep/sizeof(float);
    depth = k_image->depth;

    channels = k_image->nChannels;
    //data1 = (float *)(k_image->imageData);
    data1 = (uchar *)(k_image->imageData);
    cvNamedWindow("blur kernel", 0);
    cvShowImage("blur kernel", k_image);

    dft_M = cvGetOptimalDFTSize( im->height - 1 );
    dft_N = cvGetOptimalDFTSize( im->width - 1 );

    //dft_M1 = cvGetOptimalDFTSize( im->height+99 - 1 );
    //dft_N1 = cvGetOptimalDFTSize( im->width+99 - 1 );

    dft_M1 = cvGetOptimalDFTSize( im->height+3 - 1 );
    dft_N1 = cvGetOptimalDFTSize( im->width+3 - 1 );

    printf("dft_N1=%d,dft_M1=%d/n",dft_N1,dft_M1);

    // Perform DFT of original image
    dft_A = cvShowDFT1(im, dft_M1, dft_N1,"original");
    //Perform inverse (check)
    //cvShowInvDFT1(im,dft_A,dft_M1,dft_N1, "original"); - Commented as it overwrites the DFT

    // Perform DFT of kernel
    dft_B = cvShowDFT1(k_image,dft_M1,dft_N1,"kernel");
    //Perform inverse of kernel (check)
    //cvShowInvDFT1(k_image,dft_B,dft_M1,dft_N1, "kernel");- Commented as it overwrites the DFT

    // Multiply numerator with complex conjugate
    dft_C = cvCreateMat( dft_M1, dft_N1, CV_64FC2 );

    printf("%d %d %d %d/n",dft_M,dft_N,dft_M1,dft_N1);

    // Multiply DFT(blurred image) * complex conjugate of blur kernel
    cvMulSpectrums(dft_A,dft_B,dft_C,CV_DXT_MUL_CONJ);
    //cvShowInvDFT1(im,dft_C,dft_M1,dft_N1,"blur1");

    // Split Fourier in real and imaginary parts
    image_ReC = cvCreateImage( cvSize(dft_N1, dft_M1), IPL_DEPTH_64F, 1);
    image_ImC = cvCreateImage( cvSize(dft_N1, dft_M1), IPL_DEPTH_64F, 1);
    complex_ImC = cvCreateImage( cvSize(dft_N1, dft_M1), IPL_DEPTH_64F, 2);

    printf("%d %d %d %d/n",dft_M,dft_N,dft_M1,dft_N1);

    //cvSplit( dft_C, image_ReC, image_ImC, 0, 0 );
    cvSplit( dft_C, image_ReC, image_ImC, 0, 0 );

    // Compute A^2 + B^2 of denominator or blur kernel
    image_ReB = cvCreateImage( cvSize(dft_N1, dft_M1), IPL_DEPTH_64F, 1);
    image_ImB = cvCreateImage( cvSize(dft_N1, dft_M1), IPL_DEPTH_64F, 1);

    // Split Real and imaginary parts
    cvSplit( dft_B, image_ReB, image_ImB, 0, 0 );
    cvPow( image_ReB, image_ReB, 2.0);
    cvPow( image_ImB, image_ImB, 2.0);
    cvAdd(image_ReB, image_ImB, image_ReB,0);

    val = cvScalarAll(kappa);
    cvAddS(image_ReB,val,image_ReB,0);

    //Divide Numerator/A^2 + B^2
    cvDiv(image_ReC, image_ReB, image_ReC, 1.0);
    cvDiv(image_ImC, image_ReB, image_ImC, 1.0);

    // Merge Real and complex parts
    cvMerge(image_ReC, image_ImC, NULL, NULL, complex_ImC);

    // Perform Inverse
    cvShowInvDFT1(im, (CvMat *)complex_ImC,dft_M1,dft_N1,"O/p Wiener k=1 rad=2");

    cvWaitKey(-1);
    return 0;
}
示例#17
0
gint gstskin_find_skin_center_of_mass2(struct _GstSkin *skin, gint display)
{
  int skin_under_seed = 0;

  IplImage* imageRGB = cvCreateImageHeader( cvSize(skin->width, skin->height), IPL_DEPTH_8U, 3);
  imageRGB->imageData = skin->cvRGB->imageData;


  IplImage* planeR  = cvCreateImage( cvGetSize(imageRGB), 8, 1);	// R component.
  IplImage* planeG  = cvCreateImage( cvGetSize(imageRGB), 8, 1);	// G component.
  IplImage* planeB  = cvCreateImage( cvGetSize(imageRGB), 8, 1);	// B component.

  IplImage* planeAll = cvCreateImage( cvGetSize(imageRGB), IPL_DEPTH_32F, 1);	// (R+G+B) component.
  IplImage* planeR2  = cvCreateImage( cvGetSize(imageRGB), IPL_DEPTH_32F, 1);	// R component, 32bits
  IplImage* planeRp  = cvCreateImage( cvGetSize(imageRGB), IPL_DEPTH_32F, 1);	// R' and >0.4
  IplImage* planeGp  = cvCreateImage( cvGetSize(imageRGB), IPL_DEPTH_32F, 1);	// G' and > 0.28

  IplImage* planeRp2 = cvCreateImage( cvGetSize(imageRGB), IPL_DEPTH_32F, 1);	// R' <0.6
  IplImage* planeGp2 = cvCreateImage( cvGetSize(imageRGB), IPL_DEPTH_32F, 1);	// G' <0.4

  cvCvtPixToPlane(imageRGB, planeR, planeG, planeB, 0);	// Extract the 3 color components.
  cvAdd( planeR, planeG,   planeAll, NULL);
  cvAdd( planeB, planeAll, planeAll, NULL);  // All = R + G + B
  cvDiv( planeR, planeAll, planeRp, 1.0);    // R' = R / ( R + G + B)
  cvDiv( planeG, planeAll, planeGp, 1.0);    // G' = G / ( R + G + B)

  cvConvertScale( planeR, planeR2, 1.0, 0.0);
  cvCopy(planeGp, planeGp2, NULL);
  cvCopy(planeRp, planeRp2, NULL);

  cvThreshold(planeR2 , planeR2,   60, UCHAR_MAX, CV_THRESH_BINARY);     //(R > 60)
  cvThreshold(planeRp , planeRp, 0.42, UCHAR_MAX, CV_THRESH_BINARY);     //(R'> 0.4)
  cvThreshold(planeRp2, planeRp2, 0.6, UCHAR_MAX, CV_THRESH_BINARY_INV); //(R'< 0.6)
  cvThreshold(planeGp , planeGp, 0.28, UCHAR_MAX, CV_THRESH_BINARY);     //(G'> 0.28)
  cvThreshold(planeGp2, planeGp2, 0.4, UCHAR_MAX, CV_THRESH_BINARY_INV); //(G'< 0.4)

  // Combine all 3 thresholded color components, so that an output pixel will only
  // be white (255) if the H, S and V pixels were also white.
  IplImage* imageSkinPixels = cvCreateImage( cvGetSize(imageRGB), IPL_DEPTH_32F, 1);        // Greyscale output image.

  cvAnd( planeR2 ,         planeRp , imageSkinPixels);	
  cvAnd( planeRp , imageSkinPixels , imageSkinPixels);	
  cvAnd( planeRp2, imageSkinPixels , imageSkinPixels);	
  cvAnd( planeGp , imageSkinPixels , imageSkinPixels);	
  cvAnd( planeGp2, imageSkinPixels , imageSkinPixels);	

  IplImage* draft = cvCreateImage( cvGetSize(imageRGB), IPL_DEPTH_8U , 1);        // Greyscale output image.
  if(display){
    if( skin->showH )
      cvConvertScale( planeRp, draft, 1.0, 0.0);
    else if( skin->showS )
      cvConvertScale( planeG, draft, 1.0, 0.0);
    else if( skin->showV )
      cvConvertScale( planeB, draft, 1.0, 0.0);
    else
      cvConvertScale( imageSkinPixels, draft, 1.0, 0.0);
    cvCvtColor(draft, imageRGB, CV_GRAY2RGB);
  }


  cvReleaseImage( &imageSkinPixels );
  cvReleaseImage( &planeR );
  cvReleaseImage( &planeG );
  cvReleaseImage( &planeB );
  cvReleaseImage( &planeAll );
  cvReleaseImage( &planeR2 );
  cvReleaseImage( &planeRp );
  cvReleaseImage( &planeGp );
  cvReleaseImage( &planeRp2 );
  cvReleaseImage( &planeGp2 );
  cvReleaseImage( &draft );

  return(skin_under_seed);
}
示例#18
0
static GstFlowReturn
gst_skin_detect_transform (GstOpencvVideoFilter * base, GstBuffer * buf,
    IplImage * img, GstBuffer * outbuf, IplImage * outimg)
{
  GstSkinDetect *filter = GST_SKIN_DETECT (base);



  filter->cvRGB->imageData = (char *) img->imageData;
  filter->cvSkin->imageData = (char *) outimg->imageData;

  /* SKIN COLOUR BLOB DETECTION */
  if (HSV == filter->method) {
    cvCvtColor (filter->cvRGB, filter->cvHSV, CV_RGB2HSV);
    cvCvtPixToPlane (filter->cvHSV, filter->cvH, filter->cvS, filter->cvV, 0);  /*  Extract the 3 color components. */

    /*  Detect which pixels in each of the H, S and V channels are probably skin pixels. 
       Assume that skin has a Hue between 0 to 18 (out of 180), and Saturation above 50, and Brightness above 80. */
    cvThreshold (filter->cvH, filter->cvH2, 10, UCHAR_MAX, CV_THRESH_BINARY);   /* (hue > 10) */
    cvThreshold (filter->cvH, filter->cvH, 20, UCHAR_MAX, CV_THRESH_BINARY_INV);        /* (hue < 20) */
    cvThreshold (filter->cvS, filter->cvS, 48, UCHAR_MAX, CV_THRESH_BINARY);    /* (sat > 48) */
    cvThreshold (filter->cvV, filter->cvV, 80, UCHAR_MAX, CV_THRESH_BINARY);    /* (val > 80) */

    /*  erode the HUE to get rid of noise. */
    cvErode (filter->cvH, filter->cvH, NULL, 1);

    /*  Combine all 3 thresholded color components, so that an output pixel will only 
       be white (255) if the H, S and V pixels were also white.
       imageSkin = (hue > 10) ^ (hue < 20) ^ (sat > 48) ^ (val > 80), where   ^ mean pixels-wise AND */
    cvAnd (filter->cvH, filter->cvS, filter->cvSkinPixels1, NULL);
    cvAnd (filter->cvSkinPixels1, filter->cvH2, filter->cvSkinPixels1, NULL);
    cvAnd (filter->cvSkinPixels1, filter->cvV, filter->cvSkinPixels1, NULL);

    cvCvtColor (filter->cvSkinPixels1, filter->cvRGB, CV_GRAY2RGB);
  } else if (RGB == filter->method) {
    cvCvtPixToPlane (filter->cvRGB, filter->cvR, filter->cvG, filter->cvB, 0);  /*  Extract the 3 color components. */
    cvAdd (filter->cvR, filter->cvG, filter->cvAll, NULL);
    cvAdd (filter->cvB, filter->cvAll, filter->cvAll, NULL);    /*  All = R + G + B */
    cvDiv (filter->cvR, filter->cvAll, filter->cvRp, 1.0);      /*  R' = R / ( R + G + B) */
    cvDiv (filter->cvG, filter->cvAll, filter->cvGp, 1.0);      /*  G' = G / ( R + G + B) */

    cvConvertScale (filter->cvR, filter->cvR2, 1.0, 0.0);
    cvCopy (filter->cvGp, filter->cvGp2, NULL);
    cvCopy (filter->cvRp, filter->cvRp2, NULL);

    cvThreshold (filter->cvR2, filter->cvR2, 60, UCHAR_MAX, CV_THRESH_BINARY);  /* (R > 60) */
    cvThreshold (filter->cvRp, filter->cvRp, 0.42, UCHAR_MAX, CV_THRESH_BINARY);        /* (R'> 0.4) */
    cvThreshold (filter->cvRp2, filter->cvRp2, 0.6, UCHAR_MAX, CV_THRESH_BINARY_INV);   /* (R'< 0.6) */
    cvThreshold (filter->cvGp, filter->cvGp, 0.28, UCHAR_MAX, CV_THRESH_BINARY);        /* (G'> 0.28) */
    cvThreshold (filter->cvGp2, filter->cvGp2, 0.4, UCHAR_MAX, CV_THRESH_BINARY_INV);   /* (G'< 0.4) */

    /*  Combine all 3 thresholded color components, so that an output pixel will only
       be white (255) if the H, S and V pixels were also white. */

    cvAnd (filter->cvR2, filter->cvRp, filter->cvSkinPixels2, NULL);
    cvAnd (filter->cvRp, filter->cvSkinPixels2, filter->cvSkinPixels2, NULL);
    cvAnd (filter->cvRp2, filter->cvSkinPixels2, filter->cvSkinPixels2, NULL);
    cvAnd (filter->cvGp, filter->cvSkinPixels2, filter->cvSkinPixels2, NULL);
    cvAnd (filter->cvGp2, filter->cvSkinPixels2, filter->cvSkinPixels2, NULL);

    cvConvertScale (filter->cvSkinPixels2, filter->cvdraft, 1.0, 0.0);
    cvCvtColor (filter->cvdraft, filter->cvRGB, CV_GRAY2RGB);
  }

  /* After this we have a RGB Black and white image with the skin, in 
     filter->cvRGB. We can postprocess by applying 1 erode-dilate and 1
     dilate-erode, or alternatively 1 opening-closing all together, with
     the goal of removing small (spurious) skin spots and creating large
     connected areas */
  if (filter->postprocess) {
    cvSplit (filter->cvRGB, filter->cvChA, NULL, NULL, NULL);

    cvErode (filter->cvChA, filter->cvChA,
        cvCreateStructuringElementEx (3, 3, 1, 1, CV_SHAPE_RECT, NULL), 1);
    cvDilate (filter->cvChA, filter->cvChA,
        cvCreateStructuringElementEx (3, 3, 1, 1, CV_SHAPE_RECT, NULL), 2);
    cvErode (filter->cvChA, filter->cvChA,
        cvCreateStructuringElementEx (3, 3, 1, 1, CV_SHAPE_RECT, NULL), 1);

    cvCvtColor (filter->cvChA, filter->cvRGB, CV_GRAY2RGB);
  }

  cvCopy (filter->cvRGB, filter->cvSkin, NULL);

  return GST_FLOW_OK;
}
示例#19
0
void prefilt(const IplImage* img,IplImage* prefedImg,int pad,IplImage* filter2D)
{
	int imagesize=img->height;
	int padImgSize=imagesize+2*pad;
	
	IplImage* temp1=cvCreateImage(cvSize(imagesize,imagesize),IPL_DEPTH_32F,3);
	
	/*put 8 bits image into 32 bits float image*/
	cvConvertScale(img,temp1,1,1);

	int i;
	//use ptr to get access to data blocks/pixels,each block/pixel has three float numbers
	IplImage* temp2=cvCreateImage(cvSize(imagesize,imagesize),IPL_DEPTH_32F,3);
	float* ptr=NULL;

	cvLog(temp1,temp2);
	cvReleaseImage(&temp1);

	/*pad the image to reduce boundary artifacts*/
	IplImage* temp5=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,3);
	IplImage* pad1=cvCreateImage(cvSize(imagesize,imagesize),IPL_DEPTH_32F,1);
	IplImage* pad2=cvCreateImage(cvSize(imagesize,imagesize),IPL_DEPTH_32F,1);
	IplImage* pad3=cvCreateImage(cvSize(imagesize,imagesize),IPL_DEPTH_32F,1);
	cvSplit(temp2,pad1,pad2,pad3,NULL);
	IplImage* C1=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,1);
	IplImage* C2=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,1);
	IplImage* C3=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,1);
	padarraySym(pad1,C1,pad);/* pad each channel */
	padarraySym(pad2,C2,pad);
	padarraySym(pad3,C3,pad);
	cvMerge(C1,C2,C3,NULL,temp5);

	cvReleaseImage(&pad1);
	cvReleaseImage(&pad2);
	cvReleaseImage(&pad3);
	cvReleaseImage(&C1);
	cvReleaseImage(&C2);
	cvReleaseImage(&C3);



	/* FFT */
	/* split 3-channel image to single channel images */
	IplImage* splitImgs[3];
	IplImage* dftOutput[3];
	IplImage* splitImgsC2[3];
	for(i=0;i<3;i++)
	{
		splitImgs[i]=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,1);
		splitImgsC2[i]=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,2);
		dftOutput[i]=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,2);
		cvSetZero(splitImgs[i]);
		cvSetZero(splitImgsC2[i]);
		cvSetZero(dftOutput[i]);
	}
	cvSplit(temp5,splitImgs[0],splitImgs[1],splitImgs[2],NULL);
	IplImage* mergSrc=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,1);
	cvSetZero(mergSrc);
	/* merge to double channel images for input of FFT */
	for(i=0;i<3;i++)
	{
		cvMerge(splitImgs[i],mergSrc,NULL,NULL,splitImgsC2[i]);
		cvReleaseImage(&splitImgs[i]);
	}
	/* FFT */
	for(i=0;i<3;i++)
	{
		cvDFT(splitImgsC2[i],dftOutput[i],CV_DXT_FORWARD);
		cvReleaseImage(&splitImgsC2[i]);
	}

	/* apply prefilt to three channels */
	IplImage* output_temp[3];
	IplImage* outputReal[3];
	IplImage* outputImaginary[3];
	IplImage* ifftOutput[3];
	float temp=0;
	int j=0;
	float* ptr1=NULL;
	for(i=0;i<3;i++)
	{
		output_temp[i]=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,2);
		cvSetZero(output_temp[i]);
		cvMul(filter2D,dftOutput[i],output_temp[i]);
		cvReleaseImage(&dftOutput[i]);

		outputReal[i]=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,1);
		cvSetZero(outputReal[i]);
		outputImaginary[i]=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,1);
		cvSetZero(outputImaginary[i]);
		ifftOutput[i]=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,2);
		cvSetZero(ifftOutput[i]);
		cvDFT(output_temp[i],ifftOutput[i],CV_DXT_INVERSE_SCALE);
		cvSplit(ifftOutput[i],outputReal[i],outputImaginary[i],NULL,NULL);
		cvReleaseImage(&outputImaginary[i]);
		cvReleaseImage(&ifftOutput[i]);
	}

	/* merge real parts to a 3-channel image */
	IplImage* temp3=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,3);
	cvMerge(outputReal[0],outputReal[1],outputReal[2],NULL,temp3);
	cvReleaseImage(&outputReal[0]);
	cvReleaseImage(&outputReal[1]);
	cvReleaseImage(&outputReal[2]);

	IplImage* prefiltOutput=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,3);
	cvSub(temp5,temp3,prefiltOutput);
	cvReleaseImage(&temp3);
	cvReleaseImage(&temp5);



	/* local contrast normalization */
	/* mean of values in different channels of each pixel */
	IplImage* mean=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,1);
	IplImage* mean2D=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,2);
	ptr=(float*) mean->imageData;
	ptr1=(float*) prefiltOutput->imageData;
	for(i=0;i<(padImgSize)*(padImgSize)*3;i++)
	{
		if((i+1)%3==0)
		{
			temp=(*(ptr1+i)+*(ptr1+i-1)+*(ptr1+i-2))/3;
			*(ptr+(i+1)/3-1)=temp*temp;
		}

	}


	/* FFT */
	IplImage* fftRes=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,2);
	cvMerge(mean,mergSrc,NULL,NULL,mean2D);
	cvReleaseImage(&mergSrc);
	cvDFT(mean2D,fftRes,CV_DXT_FORWARD);
	cvReleaseImage(&mean);
	IplImage* temp4=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,2);
	cvMul(filter2D,fftRes,temp4);

	/* Inverse FFT */
	IplImage* ifftRes=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,2);
	IplImage* lccstTemp=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,1);
	ptr1=(float*) lccstTemp->imageData;
	cvFFT(temp4,ifftRes,CV_DXT_INVERSE_SCALE);
	/* caculate the abs of complex number matrix */
	ptr=(float*)ifftRes->imageData;
	float realV=0;
	float imgV=0;
	for(i=0;i<(padImgSize)*(padImgSize);i++)
	{
		realV=*(ptr+2*i);
		imgV=*(ptr+2*i+1);
		*(ptr1+i)=sqrt(sqrt(realV*realV+fabs(imgV*imgV)))+0.2;
	}
	IplImage* lccst=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,3);

	cvMerge(lccstTemp,lccstTemp,lccstTemp,NULL,lccst);
	IplImage* outputTemp=cvCreateImage(cvSize(padImgSize,padImgSize),IPL_DEPTH_32F,3);
	cvDiv(prefiltOutput,lccst,outputTemp);

	cvReleaseImage(&mean2D);
	cvReleaseImage(&fftRes);
	cvReleaseImage(&ifftRes);
	cvReleaseImage(&lccstTemp);
	cvReleaseImage(&lccst);
	cvReleaseImage(&prefiltOutput);
	cvReleaseImage(&temp2);
	cvReleaseImage(&temp4);
	cvReleaseImage(&output_temp[0]);
	cvReleaseImage(&output_temp[1]);
	cvReleaseImage(&output_temp[2]);


	IplImage* temp6 = cvGetSubImage(outputTemp,cvRect(pad,pad,imagesize,imagesize));
	cvCopy(temp6,prefedImg);
	cvReleaseImage(&outputTemp);
	cvReleaseImage(&temp6);
}
示例#20
0
void icvConvertPointsHomogenious( const CvMat* src, CvMat* dst )
{
  CvMat* temp = 0;
  CvMat* denom = 0;

  CV_FUNCNAME( "cvConvertPointsHomogenious" );

  __BEGIN__;

  int i, s_count, s_dims, d_count, d_dims;
  CvMat _src, _dst, _ones;
  CvMat* ones = 0;

  if( !CV_IS_MAT(src) )
    CV_ERROR( !src ? CV_StsNullPtr : CV_StsBadArg,
              "The input parameter is not a valid matrix" );

  if( !CV_IS_MAT(dst) )
    CV_ERROR( !dst ? CV_StsNullPtr : CV_StsBadArg,
              "The output parameter is not a valid matrix" );

  if( src == dst || src->data.ptr == dst->data.ptr )
  {
    if( src != dst && (!CV_ARE_TYPES_EQ(src, dst) ||
                       !CV_ARE_SIZES_EQ(src,dst)) )
      CV_ERROR( CV_StsBadArg, "Invalid inplace operation" );
    EXIT;
  }

  if( src->rows > src->cols )
  {
    if( !((src->cols > 1) ^ (CV_MAT_CN(src->type) > 1)) )
      CV_ERROR( CV_StsBadSize,
                "Either the number of channels or columns or "
                "rows must be =1" );

    s_dims = CV_MAT_CN(src->type)*src->cols;
    s_count = src->rows;
  }
  else
  {
    if( !((src->rows > 1) ^ (CV_MAT_CN(src->type) > 1)) )
      CV_ERROR( CV_StsBadSize,
                "Either the number of channels or columns or "
                "rows must be =1" );

    s_dims = CV_MAT_CN(src->type)*src->rows;
    s_count = src->cols;
  }

  if( src->rows == 1 || src->cols == 1 )
    src = cvReshape( src, &_src, 1, s_count );

  if( dst->rows > dst->cols )
  {
    if( !((dst->cols > 1) ^ (CV_MAT_CN(dst->type) > 1)) )
      CV_ERROR( CV_StsBadSize,
                "Either the number of channels or columns or "
                "rows in the input matrix must be =1" );

    d_dims = CV_MAT_CN(dst->type)*dst->cols;
    d_count = dst->rows;
  }
  else
  {
    if( !((dst->rows > 1) ^ (CV_MAT_CN(dst->type) > 1)) )
      CV_ERROR( CV_StsBadSize,
                "Either the number of channels or columns or "
                "rows in the output matrix must be =1" );

    d_dims = CV_MAT_CN(dst->type)*dst->rows;
    d_count = dst->cols;
  }

  if( dst->rows == 1 || dst->cols == 1 )
    dst = cvReshape( dst, &_dst, 1, d_count );

  if( s_count != d_count )
    CV_ERROR( CV_StsUnmatchedSizes, "Both matrices must have the "
                "same number of points" );

  if( CV_MAT_DEPTH(src->type) < CV_32F || CV_MAT_DEPTH(dst->type) < CV_32F )
    CV_ERROR( CV_StsUnsupportedFormat,
              "Both matrices must be floating-point "
                "(single or double precision)" );

  if( s_dims < 2 || s_dims > 4 || d_dims < 2 || d_dims > 4 )
    CV_ERROR( CV_StsOutOfRange,
              "Both input and output point dimensionality "
                "must be 2, 3 or 4" );

  if( s_dims < d_dims - 1 || s_dims > d_dims + 1 )
    CV_ERROR( CV_StsUnmatchedSizes,
              "The dimensionalities of input and output "
                "point sets differ too much" );

  if( s_dims == d_dims - 1 )
  {
    if( d_count == dst->rows )
    {
      ones = cvGetSubRect( dst, &_ones, cvRect( s_dims, 0, 1, d_count ));
      dst = cvGetSubRect( dst, &_dst, cvRect( 0, 0, s_dims, d_count ));
    }
    else
    {
      ones = cvGetSubRect( dst, &_ones, cvRect( 0, s_dims, d_count, 1 ));
      dst = cvGetSubRect( dst, &_dst, cvRect( 0, 0, d_count, s_dims ));
    }
  }

  if( s_dims <= d_dims )
  {
    if( src->rows == dst->rows && src->cols == dst->cols )
    {
      if( CV_ARE_TYPES_EQ( src, dst ) )
        cvCopy( src, dst );
      else
        cvConvert( src, dst );
    }
    else
    {
      if( !CV_ARE_TYPES_EQ( src, dst ))
      {
        CV_CALL( temp = cvCreateMat( src->rows, src->cols, dst->type ));
        cvConvert( src, temp );
        src = temp;
      }
      cvTranspose( src, dst );
    }

    if( ones )
      cvSet( ones, cvRealScalar(1.) );
  }
  else
  {
    int s_plane_stride, s_stride, d_plane_stride, d_stride, elem_size;

    if( !CV_ARE_TYPES_EQ( src, dst ))
    {
      CV_CALL( temp = cvCreateMat( src->rows, src->cols, dst->type ));
      cvConvert( src, temp );
      src = temp;
    }

    elem_size = CV_ELEM_SIZE(src->type);

    if( s_count == src->cols )
      s_plane_stride = src->step / elem_size, s_stride = 1;
    else
      s_stride = src->step / elem_size, s_plane_stride = 1;

    if( d_count == dst->cols )
      d_plane_stride = dst->step / elem_size, d_stride = 1;
    else
      d_stride = dst->step / elem_size, d_plane_stride = 1;

    CV_CALL( denom = cvCreateMat( 1, d_count, dst->type ));

    if( CV_MAT_DEPTH(dst->type) == CV_32F )
    {
      const float* xs = src->data.fl;
      const float* ys = xs + s_plane_stride;
      const float* zs = 0;
      const float* ws = xs + (s_dims - 1)*s_plane_stride;

      float* iw = denom->data.fl;

      float* xd = dst->data.fl;
      float* yd = xd + d_plane_stride;
      float* zd = 0;

      if( d_dims == 3 )
      {
        zs = ys + s_plane_stride;
        zd = yd + d_plane_stride;
      }

      for( i = 0; i < d_count; i++, ws += s_stride )
      {
        float t = *ws;
        iw[i] = t ? t : 1.f;
      }

      cvDiv( 0, denom, denom );

      if( d_dims == 3 )
        for( i = 0; i < d_count; i++ )
        {
          float w = iw[i];
          float x = *xs * w, y = *ys * w, z = *zs * w;
          xs += s_stride; ys += s_stride; zs += s_stride;
          *xd = x; *yd = y; *zd = z;
          xd += d_stride; yd += d_stride; zd += d_stride;
        }
      else
        for( i = 0; i < d_count; i++ )
        {
          float w = iw[i];
          float x = *xs * w, y = *ys * w;
          xs += s_stride; ys += s_stride;
          *xd = x; *yd = y;
          xd += d_stride; yd += d_stride;
        }
    }
    else
    {
      const double* xs = src->data.db;
      const double* ys = xs + s_plane_stride;
      const double* zs = 0;
      const double* ws = xs + (s_dims - 1)*s_plane_stride;

      double* iw = denom->data.db;

      double* xd = dst->data.db;
      double* yd = xd + d_plane_stride;
      double* zd = 0;

      if( d_dims == 3 )
      {
        zs = ys + s_plane_stride;
        zd = yd + d_plane_stride;
      }

      for( i = 0; i < d_count; i++, ws += s_stride )
      {
        double t = *ws;
        iw[i] = t ? t : 1.;
      }

      cvDiv( 0, denom, denom );

      if( d_dims == 3 )
        for( i = 0; i < d_count; i++ )
        {
          double w = iw[i];
          double x = *xs * w, y = *ys * w, z = *zs * w;
          xs += s_stride; ys += s_stride; zs += s_stride;
          *xd = x; *yd = y; *zd = z;
          xd += d_stride; yd += d_stride; zd += d_stride;
        }
      else
        for( i = 0; i < d_count; i++ )
        {
          double w = iw[i];
          double x = *xs * w, y = *ys * w;
          xs += s_stride; ys += s_stride;
          *xd = x; *yd = y;
          xd += d_stride; yd += d_stride;
        }
    }
  }

  __END__;

  cvReleaseMat( &denom );
  cvReleaseMat( &temp );
}
示例#21
0
CvScalar calcSSIM :: compare(IplImage *source1, IplImage *source2, Colorspace space)
{
  IplImage *src1, *src2;
  src1 = colorspaceConversion(source1, space);
  src2 = colorspaceConversion(source2, space);
  int x = source1->width, y = source1->height;
  // default settings
  const double C1 = (K1 * L) * (K1 * L); 
  const double C2 = (K2 * L) * (K2 * L);

  int nChan = src1->nChannels;
  int d = IPL_DEPTH_32F;
  CvSize size = cvSize(x, y);
  
  //creating FLOAT type images of src1 and src2 
  IplImage *img1 = cvCreateImage(size, d, nChan);
  IplImage *img2 = cvCreateImage(size, d, nChan);

  //Image squares
  IplImage *img1_sq = cvCreateImage(size, d, nChan);
  IplImage *img2_sq = cvCreateImage(size, d, nChan);
  IplImage *img1_img2 = cvCreateImage(size, d, nChan);
  
  cvConvert(src1, img1);
  cvConvert(src2, img2);
  
  //Squaring the images thus created
  cvPow(img1, img1_sq, 2);
  cvPow(img2, img2_sq, 2);
  cvMul(img1, img2, img1_img2, 1);

  IplImage *mu1 = cvCreateImage(size, d, nChan);
  IplImage *mu2 = cvCreateImage(size, d, nChan);
  IplImage *mu1_sq = cvCreateImage(size, d, nChan);
  IplImage *mu2_sq = cvCreateImage(size, d, nChan);
  IplImage *mu1_mu2 = cvCreateImage(size, d, nChan);

  IplImage *sigma1_sq = cvCreateImage(size, d, nChan);
  IplImage *sigma2_sq = cvCreateImage(size, d, nChan);
  IplImage *sigma12 = cvCreateImage(size, d, nChan);

  //PRELIMINARY COMPUTING
  //gaussian smoothing is performed
  cvSmooth(img1, mu1, CV_GAUSSIAN, gaussian_window, gaussian_window, gaussian_sigma);
  cvSmooth(img2, mu2, CV_GAUSSIAN, gaussian_window, gaussian_window, gaussian_sigma);

  //gettting mu, mu_sq, mu1_mu2
  cvPow(mu1, mu1_sq, 2);
  cvPow(mu2, mu2_sq, 2);
  cvMul(mu1, mu2, mu1_mu2, 1);

  //calculating sigma1, sigma2, sigma12
  cvSmooth(img1_sq, sigma1_sq, CV_GAUSSIAN, gaussian_window, gaussian_window, gaussian_sigma);
  cvSub(sigma1_sq, mu1_sq, sigma1_sq);

  cvSmooth(img2_sq, sigma2_sq, CV_GAUSSIAN, gaussian_window, gaussian_window, gaussian_sigma);
  cvSub(sigma2_sq, mu2_sq, sigma2_sq);

  cvSmooth(img1_img2, sigma12, CV_GAUSSIAN, gaussian_window, gaussian_window, gaussian_sigma);
  cvSub(sigma12, mu1_mu2, sigma12);
  
  //releasing some junk buffers
  cvReleaseImage(&img1);
  cvReleaseImage(&img2);
  cvReleaseImage(&img1_sq);
  cvReleaseImage(&img2_sq);
  cvReleaseImage(&img1_img2);
  cvReleaseImage(&mu1);
  cvReleaseImage(&mu2);
  
  // creating buffers for numerator and denominator 
  IplImage *numerator1 = cvCreateImage(size, d, nChan);
  IplImage *numerator2 = cvCreateImage(size, d, nChan);
  IplImage *numerator = cvCreateImage(size, d, nChan);
  IplImage *denominator1 = cvCreateImage(size, d, nChan);
  IplImage *denominator2 = cvCreateImage(size, d, nChan);
  IplImage *denominator = cvCreateImage(size, d, nChan);

  // FORMULA to calculate SSIM
  // (2*mu1_mu2 + C1)
  cvScale(mu1_mu2, numerator1, 2);
  cvAddS(numerator1, cvScalarAll(C1), numerator1);
  // (2*sigma12 + C2) 
  cvScale(sigma12, numerator2, 2);
  cvAddS(numerator2, cvScalarAll(C2), numerator2);
  // ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
  cvMul(numerator1, numerator2, numerator, 1);

  // (mu1_sq + mu2_sq + C1)
  cvAdd(mu1_sq, mu2_sq, denominator1);
  cvAddS(denominator1, cvScalarAll(C1), denominator1);
  // (sigma1_sq + sigma2_sq + C2) >>>
  cvAdd(sigma1_sq, sigma2_sq, denominator2);
  cvAddS(denominator2, cvScalarAll(C2),denominator2);
  // ((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
  cvMul(denominator1, denominator2, denominator, 1);

  //Release some junk buffers 
  cvReleaseImage(&numerator1);
  cvReleaseImage(&denominator1);
  cvReleaseImage(&mu1_sq);
  cvReleaseImage(&mu2_sq);
  cvReleaseImage(&mu1_mu2);
  cvReleaseImage(&sigma1_sq);
  cvReleaseImage(&sigma2_sq);
  cvReleaseImage(&sigma12);

  //ssim map and cs_map
  ssim_map = cvCreateImage(size, d, nChan);
  cs_map = cvCreateImage(size, d, nChan);
  // SSIM_INDEX map 
  // ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
  cvDiv(numerator, denominator, ssim_map, 1);
  // Contrast Structure CS_index map
  // (2*sigma12 + C2)./(sigma1_sq + sigma2_sq + C2)
  cvDiv(numerator2, denominator2, cs_map, 1);

  // average is taken for both SSIM_map and CS_map 
  mssim_value = cvAvg(ssim_map);
  mean_cs_value = cvAvg(cs_map);

  //Release images
  cvReleaseImage(&numerator);
  cvReleaseImage(&denominator);
  cvReleaseImage(&numerator2);
  cvReleaseImage(&denominator2);
  cvReleaseImage(&src1);
  cvReleaseImage(&src2);

  return mssim_value;
}
示例#22
0
//function definitions
void ComputeBrisqueFeature(IplImage *orig, vector<double>& featurevector)
{
    IplImage *orig_bw_int = cvCreateImage(cvGetSize(orig), orig->depth, 1); 
    cvCvtColor(orig, orig_bw_int, CV_RGB2GRAY);
    IplImage *orig_bw = cvCreateImage(cvGetSize(orig_bw_int), IPL_DEPTH_64F, 1);
    cvConvertScale(orig_bw_int, orig_bw, 1.0/255);
    cvReleaseImage(&orig_bw_int);
    
    //orig_bw now contains the grayscale image normalized to the range 0,1
    
    int scalenum = 2;
    for (int itr_scale = 1; itr_scale<=scalenum; itr_scale++)
	{
		IplImage *imdist_scaled = cvCreateImage(cvSize(orig_bw->width/pow((double)2,itr_scale-1), orig_bw->height/pow((double)2,itr_scale-1)), IPL_DEPTH_64F, 1);
		cvResize(orig_bw, imdist_scaled,CV_INTER_CUBIC); 
		
		//compute mu and mu squared
		IplImage* mu = cvCreateImage(cvGetSize(imdist_scaled), IPL_DEPTH_64F, 1);
		cvSmooth( imdist_scaled, mu, CV_GAUSSIAN, 7, 7, 1.16666 );
		IplImage* mu_sq = cvCreateImage(cvGetSize(imdist_scaled), IPL_DEPTH_64F, 1);
		cvMul(mu, mu, mu_sq);

		//compute sigma
		IplImage* sigma = cvCreateImage(cvGetSize(imdist_scaled), IPL_DEPTH_64F, 1);
		cvMul(imdist_scaled, imdist_scaled, sigma);
		cvSmooth(sigma, sigma, CV_GAUSSIAN, 7, 7, 1.16666 );
		cvSub(sigma, mu_sq, sigma);
		cvPow(sigma, sigma, 0.5);

		//compute structdis = (x-mu)/sigma
		cvAddS(sigma, cvScalar(1.0/255), sigma);
		IplImage* structdis = cvCreateImage(cvGetSize(imdist_scaled), IPL_DEPTH_64F, 1);
		cvSub(imdist_scaled, mu, structdis);
		cvDiv(structdis, sigma, structdis);

		//Compute AGGD fit
                double lsigma_best, rsigma_best, gamma_best;
                AGGDfit(structdis, lsigma_best, rsigma_best, gamma_best);
		featurevector.push_back(gamma_best);
		featurevector.push_back((lsigma_best*lsigma_best + rsigma_best*rsigma_best)/2);
		
		//Compute paired product images
		int shifts[4][2]={{0,1},{1,0},{1,1},{-1,1}};
		for(int itr_shift=1; itr_shift<=4; itr_shift++)
		{
			int* reqshift = shifts[itr_shift-1];

                        IplImage* shifted_structdis = cvCreateImage(cvGetSize(imdist_scaled), IPL_DEPTH_64F, 1);
			BwImage OrigArr(structdis);
			BwImage ShiftArr(shifted_structdis);
			for(int i=0; i<structdis->height; i++)
			{
				for(int j=0; j<structdis->width; j++)
				{
					if(i+reqshift[0]>=0 && i+reqshift[0]<structdis->height && j+reqshift[1]>=0 && j+reqshift[1]<structdis->width)
					{
						ShiftArr[i][j]=OrigArr[i+reqshift[0]][j+reqshift[1]];
					}
					else
					{
						ShiftArr[i][j]=0;
					}
				}
			}
		
			//computing correlation
			cvMul(structdis, shifted_structdis, shifted_structdis);
			AGGDfit(shifted_structdis, lsigma_best, rsigma_best, gamma_best);
		
			double constant = sqrt(tgamma(1/gamma_best))/sqrt(tgamma(3/gamma_best));
			double meanparam = (rsigma_best-lsigma_best)*(tgamma(2/gamma_best)/tgamma(1/gamma_best))*constant;
			
			featurevector.push_back(gamma_best);
			featurevector.push_back(meanparam);
			featurevector.push_back(pow(lsigma_best,2));
			featurevector.push_back(pow(rsigma_best,2));

                        cvReleaseImage(&shifted_structdis);	
		}

                cvReleaseImage(&mu);
		cvReleaseImage(&mu_sq);
		cvReleaseImage(&sigma);
		cvReleaseImage(&structdis);
		cvReleaseImage(&imdist_scaled);
	}
	
}
示例#23
0
文件: SSIM.cpp 项目: Anjan369/ssim
/*
 * Parameters : complete path to the two image to be compared
 * The file format must be supported by your OpenCV build
 */
int main(int argc, char** argv)
{
	if(argc!=3)
		return -1;
	
	// default settings
	double C1 = 6.5025, C2 = 58.5225;

	IplImage
		*img1=NULL, *img2=NULL, *img1_img2=NULL,
		*img1_temp=NULL, *img2_temp=NULL,
		*img1_sq=NULL, *img2_sq=NULL,
		*mu1=NULL, *mu2=NULL,
		*mu1_sq=NULL, *mu2_sq=NULL, *mu1_mu2=NULL,
		*sigma1_sq=NULL, *sigma2_sq=NULL, *sigma12=NULL,
		*ssim_map=NULL, *temp1=NULL, *temp2=NULL, *temp3=NULL;
	

	/***************************** INITS **********************************/
	img1_temp = cvLoadImage(argv[1]);
	img2_temp = cvLoadImage(argv[2]);

	if(img1_temp==NULL || img2_temp==NULL)
		return -1;

	int x=img1_temp->width, y=img1_temp->height;
	int nChan=img1_temp->nChannels, d=IPL_DEPTH_32F;
	CvSize size = cvSize(x, y);

	img1 = cvCreateImage( size, d, nChan);
	img2 = cvCreateImage( size, d, nChan);

	cvConvert(img1_temp, img1);
	cvConvert(img2_temp, img2);
	cvReleaseImage(&img1_temp);
	cvReleaseImage(&img2_temp);

	
	img1_sq = cvCreateImage( size, d, nChan);
	img2_sq = cvCreateImage( size, d, nChan);
	img1_img2 = cvCreateImage( size, d, nChan);
	
	cvPow( img1, img1_sq, 2 );
	cvPow( img2, img2_sq, 2 );
	cvMul( img1, img2, img1_img2, 1 );

	mu1 = cvCreateImage( size, d, nChan);
	mu2 = cvCreateImage( size, d, nChan);

	mu1_sq = cvCreateImage( size, d, nChan);
	mu2_sq = cvCreateImage( size, d, nChan);
	mu1_mu2 = cvCreateImage( size, d, nChan);
	

	sigma1_sq = cvCreateImage( size, d, nChan);
	sigma2_sq = cvCreateImage( size, d, nChan);
	sigma12 = cvCreateImage( size, d, nChan);

	temp1 = cvCreateImage( size, d, nChan);
	temp2 = cvCreateImage( size, d, nChan);
	temp3 = cvCreateImage( size, d, nChan);

	ssim_map = cvCreateImage( size, d, nChan);
	/*************************** END INITS **********************************/


	//////////////////////////////////////////////////////////////////////////
	// PRELIMINARY COMPUTING
	cvSmooth( img1, mu1, CV_GAUSSIAN, 11, 11, 1.5 );
	cvSmooth( img2, mu2, CV_GAUSSIAN, 11, 11, 1.5 );
	
	cvPow( mu1, mu1_sq, 2 );
	cvPow( mu2, mu2_sq, 2 );
	cvMul( mu1, mu2, mu1_mu2, 1 );


	cvSmooth( img1_sq, sigma1_sq, CV_GAUSSIAN, 11, 11, 1.5 );
	cvAddWeighted( sigma1_sq, 1, mu1_sq, -1, 0, sigma1_sq );
	
	cvSmooth( img2_sq, sigma2_sq, CV_GAUSSIAN, 11, 11, 1.5 );
	cvAddWeighted( sigma2_sq, 1, mu2_sq, -1, 0, sigma2_sq );

	cvSmooth( img1_img2, sigma12, CV_GAUSSIAN, 11, 11, 1.5 );
	cvAddWeighted( sigma12, 1, mu1_mu2, -1, 0, sigma12 );
	

	//////////////////////////////////////////////////////////////////////////
	// FORMULA

	// (2*mu1_mu2 + C1)
	cvScale( mu1_mu2, temp1, 2 );
	cvAddS( temp1, cvScalarAll(C1), temp1 );

	// (2*sigma12 + C2)
	cvScale( sigma12, temp2, 2 );
	cvAddS( temp2, cvScalarAll(C2), temp2 );

	// ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
	cvMul( temp1, temp2, temp3, 1 );

	// (mu1_sq + mu2_sq + C1)
	cvAdd( mu1_sq, mu2_sq, temp1 );
	cvAddS( temp1, cvScalarAll(C1), temp1 );

	// (sigma1_sq + sigma2_sq + C2)
	cvAdd( sigma1_sq, sigma2_sq, temp2 );
	cvAddS( temp2, cvScalarAll(C2), temp2 );

	// ((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
	cvMul( temp1, temp2, temp1, 1 );

	// ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
	cvDiv( temp3, temp1, ssim_map, 1 );


	CvScalar index_scalar = cvAvg( ssim_map );
	
	// through observation, there is approximately 
	// 1% error max with the original matlab program

	cout << "(R, G & B SSIM index)" << endl ;
	cout << index_scalar.val[2] * 100 << "%" << endl ;
	cout << index_scalar.val[1] * 100 << "%" << endl ;
	cout << index_scalar.val[0] * 100 << "%" << endl ;

	// if you use this code within a program
	// don't forget to release the IplImages
	return 0;
}
示例#24
0
CV_IMPL void cvDrlse_edge(CvArr * srcphi, 
                          CvArr * srcgrad,
                          CvArr * dstarr,
                          double lambda, 
                          double mu, 
                          double alfa, 
                          double epsilon, 
                          int timestep,
                          int iter)
{   
    CV_FUNCNAME( "cvDrlse_edge" );
    
    __BEGIN__;
    
    CvMat sstub1, sstub2, *phi, *grad;
    CvMat dstub, *dst;
    CvMat *gradx=0, *grady=0, *phi_0=0, *phix=0, *phiy=0;
    CvMat *s=0, *Nx=0, *Ny=0, *curvature=0, *distRegTerm=0;
    CvMat *diracPhi=0, *areaTerm=0, *edgeTerm=0;
    CvMat *temp1=0, *temp2=0, *temp3=0, *ones=0;
    CvSize size;
    int i;
    
    CV_CALL( phi    = cvGetMat(srcphi, &sstub1 ));
    CV_CALL( grad   = cvGetMat(srcgrad,   &sstub2 ));
    CV_CALL( dst    = cvGetMat(dstarr,  &dstub));
    
    if( CV_MAT_TYPE(phi->type) != CV_32FC1)
        CV_ERROR( CV_StsUnsupportedFormat, "Only-32bit, 1-channel input images are supported" );
    
    if( CV_MAT_TYPE(grad->type) != CV_32FC1)
        CV_ERROR( CV_StsUnsupportedFormat, "Only-32bit, 1-channel input images are supported" );
    
    if( !CV_ARE_SIZES_EQ( phi, grad ))
        CV_ERROR( CV_StsUnmatchedSizes, "The input images must have the same size" );
    size = cvGetMatSize( phi );
    
    //Initialization 
    gradx = cvCreateMat(size.height, size.width, CV_32FC1 );
    grady = cvCreateMat(size.height, size.width, CV_32FC1 );
    phi_0 = cvCreateMat(size.height, size.width, CV_32FC1 );
    phix  = cvCreateMat(size.height, size.width, CV_32FC1 );
    phiy  = cvCreateMat(size.height, size.width, CV_32FC1 );
    Nx    = cvCreateMat(size.height, size.width, CV_32FC1 );
    Ny    = cvCreateMat(size.height, size.width, CV_32FC1 );
    s     = cvCreateMat(size.height, size.width, CV_32FC1 );
    curvature= cvCreateMat(size.height, size.width, CV_32FC1 );
    distRegTerm= cvCreateMat(size.height, size.width, CV_32FC1 );
    diracPhi  = cvCreateMat(size.height, size.width, CV_32FC1 );
    areaTerm  = cvCreateMat(size.height, size.width, CV_32FC1 );
    edgeTerm  = cvCreateMat(size.height, size.width, CV_32FC1 );
    temp1  = cvCreateMat(size.height, size.width, CV_32FC1 );
    temp2  = cvCreateMat(size.height, size.width, CV_32FC1 );
    temp3  = cvCreateMat(size.height, size.width, CV_32FC1 );
    ones  = cvCreateMat(size.height, size.width, CV_32FC1 );
    cvSetZero(gradx);
    cvSetZero(grady);
    cvSetZero(phix);
    cvSetZero(phiy);
    cvSetZero(Nx);
    cvSetZero(Ny);
    cvSetZero(s);
    cvSetZero(curvature);
    cvSetZero(distRegTerm);
    cvSetZero(diracPhi);
    cvSetZero(areaTerm);
    cvSetZero(edgeTerm);
    cvSetZero(temp1);
    cvSetZero(temp2);
    cvSetZero(temp3);
    cvSet(ones, cvScalar(1.0f));
    
    //--------------BEGIN----------------------
    cvSobel(grad, gradx, 1, 0, 1);
    cvSobel(grad, grady, 0, 1, 1);
    cvMul(gradx, ones, gradx, 0.25f);
    cvMul(grady, ones, grady, 0.25f);
    cvCopy(phi, dst);
    
    for(i=0; i<iter; i++){
        cvNeumannBoundCond(dst, dst);
        cvSobel(dst, phix, 1, 0, 1);
        cvSobel(dst, phiy, 0, 1, 1);
        cvCalS(dst,s);
        cvDiv(phix, s, Nx, 0.25f);
        cvDiv(phiy, s, Ny, 0.25f);
        cvCurvature(Nx, Ny, curvature);
        cvDistReg(dst, distRegTerm);
        cvDirac(dst, diracPhi, epsilon);        //Compute driacPhi;
        cvMul(diracPhi, grad, areaTerm);        //Compute areaTerm
        
        cvMul(gradx, Nx, gradx);                //------------------//
        cvMul(grady, Ny, grady);                // Computing        //
        cvAdd(gradx, grady, temp1);             //                  //
        cvMul(diracPhi, temp1, temp2);          // edgeTerm         //
        cvMul(areaTerm, curvature, temp3);      //                  //
        cvAdd(temp2, temp3, edgeTerm);          //------------------//
        
        cvMul(distRegTerm, ones, distRegTerm, mu);              //  distRegTerm = mu     * distRegTerm
        cvMul(edgeTerm,    ones, edgeTerm,    lambda);          //  edgeTerm    = lambda * edgeTerm
        cvMul(areaTerm,    ones, areaTerm,    alfa);            //  areaTerm    = alfa   * areaTerm
        cvAdd(distRegTerm, edgeTerm, temp1);
        cvAdd(temp1, areaTerm, temp2);                          //  (distRegTerm + edgeTerm + areaTerm)
        cvMul(temp2, ones, temp2, double(timestep));            //  timestep * (distRegTerm + edgeTerm + areaTerm)
        cvAdd(dst, temp2, dst);                                 //  phi = phi + timestep * (distRegTerm + edgeTerm + areaTerm)
    }
    //----------------END------------------------
    
    // Clean up
    cvReleaseMat(&ones);
    cvReleaseMat(&phi_0);
    cvReleaseMat(&gradx);
    cvReleaseMat(&grady);
    cvReleaseMat(&phix);
    cvReleaseMat(&phiy);
    cvReleaseMat(&Nx);
    cvReleaseMat(&Ny);
    cvReleaseMat(&s);
    cvReleaseMat(&curvature);
    cvReleaseMat(&distRegTerm);
    cvReleaseMat(&diracPhi);
    cvReleaseMat(&areaTerm);
    cvReleaseMat(&edgeTerm);
    cvReleaseMat(&temp1);
    cvReleaseMat(&temp2);
    cvReleaseMat(&temp3);
    
    __END__;
    
}
示例#25
0
CvPoint*
cvDRLSE(const CvArr * image,
        const CvArr * mask,
	int *length,
        double lambda,
        double alfa,
        double epsilon,
        int timestep,
        int ITER_ext,
        int ITER_int,
        int flag)
{
    IplImage sstub1, sstub2, *msk, *img, *marker, *levelset;
    CvMat *ones, *Ix, *Iy, *phi, *f, *g;
    CvSize size;
    int comp_count =0, iStep;
    float* fPtr;
    CvMemStorage* storage = cvCreateMemStorage(0);
    CvSeq* contours = 0;
    CvPoint pt= cvPoint(0,0), *point=NULL;
    double mu = 0.2f/double(timestep);
    char c;

    CV_FUNCNAME( "cvDRLSE" );
    
    __BEGIN__;
    
    CV_CALL( msk = cvGetImage(mask,    &sstub1 ));
    CV_CALL( img = cvGetImage(image,   &sstub2 ));    
    cvSmooth(img, img, CV_GAUSSIAN, 5, 5, 1.5f);
    size = cvGetSize(img);
    levelset = cvCreateImage(size, IPL_DEPTH_8U, 1);
    ones = cvCreateMat(size.height, size.width, CV_32FC1);
    Ix = cvCreateMat(size.height, size.width, CV_32FC1);
    Iy = cvCreateMat(size.height, size.width, CV_32FC1);
    phi = cvCreateMat(size.height, size.width, CV_32FC1);
    f = cvCreateMat(size.height, size.width, CV_32FC1);
    g = cvCreateMat(size.height, size.width, CV_32FC1);
    marker = cvCreateImage(size, IPL_DEPTH_32S, 1);
    cvSet(ones, cvScalar(1.0f));
    
    cvSobel(img, Ix, 1, 0, 1);
    cvSobel(img, Iy, 0, 1, 1);
    cvMul(Ix, Ix, Ix, 0.25f*0.25f);
    cvMul(Iy, Iy, Iy, 0.25f*0.25f);
    cvAdd(Ix, Iy, f);
    cvAdd(f, ones, f);
    cvDiv(NULL, f, g, 1.0f);
    
    cvFindContours( msk, storage, &contours, sizeof(CvContour),CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );
    cvZero(marker);
    for( ; contours != 0; contours = contours->h_next, comp_count++ )
    {
        cvDrawContours( marker, contours, cvScalarAll(255),cvScalarAll(255), -1, -1, 8);
    }
   
    iStep = phi->step/sizeof(fPtr[0]);
    fPtr = phi->data.fl;
    
    for (int j=0; j<size.height; j++)
        for (int i=0; i<size.width; i++) {
            int idx = CV_IMAGE_ELEM( marker, int, j, i );
            if (idx >0 )
                if (flag == CV_LSE_SHR)
                    fPtr[i+iStep*j]=-2.0f;
                else
                    fPtr[i+iStep*j]=2.0f;
            else
                if (flag == CV_LSE_SHR)
                    fPtr[i+iStep*j]=2.0f;
                else
                    fPtr[i+iStep*j]=-2.0f;
        }
    
    for (int i=0; i<ITER_ext; i++) {
        cvDrlse_edge(phi, g, phi, lambda, mu, alfa, epsilon, timestep, ITER_int);
	loadBar(i+1, ITER_ext, 50);
    }
    cvDrlse_edge(phi, g, phi, lambda, mu, 0.0f, epsilon, timestep, ITER_int);
    cvZero(msk);
    if (flag == CV_LSE_SHR)
        cvThreshold(phi, msk, 0.0f, 255, CV_THRESH_BINARY_INV);
    else
        cvThreshold(phi, msk, 0.0f, 255, CV_THRESH_BINARY);

    cvFindContours(msk, storage, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);

    if(!contours) return 0;
    *length = contours->total;
    //if(*length<10) return 0;
    point = new CvPoint[*length];
    CvSeqReader reader;
    
    cvStartReadSeq(contours, &reader);
    for (int i = 0; i < *length; i++){
        CV_READ_SEQ_ELEM(pt, reader);
        point[i]=pt;
    }
  
    //clean up
    cvReleaseMemStorage(&storage);
    cvReleaseImage(&marker);
    cvReleaseMat(&Ix);
    cvReleaseMat(&Iy);
    cvReleaseMat(&f);
    cvReleaseMat(&g);
    cvReleaseMat(&phi);
    cvReleaseMat(&ones);
    
    return point;
    __END__;
}