示例#1
0
////////////////////////////////////////////////////////////////////////////////
// in this version, set data and smoothness terms using arrays
// grid neighborhood structure is assumed
//
void GridGraph_DfnSfn(int width,int height,int num_pixels,int num_labels)
{

	int *result = new int[num_pixels];   // stores result of optimization

	// first set up the array for data costs
	int *data = new int[num_pixels*num_labels];
	for ( int i = 0; i < num_pixels; i++ )
		for (int l = 0; l < num_labels; l++ )
			if (i < 25 ){
				if(  l == 0 ) data[i*num_labels+l] = 0;
				else data[i*num_labels+l] = 10;
			}
			else {
				if(  l == 5 ) data[i*num_labels+l] = 0;
				else data[i*num_labels+l] = 10;
			}


	try{
		GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);

		// set up the needed data to pass to function for the data costs
		ForDataFn toFn;
		toFn.data = data;
		toFn.numLab = num_labels;

		gc->setDataCost(&dataFn,&toFn);

		// smoothness comes from function pointer
		gc->setSmoothCost(&smoothFn);

		printf("\nBefore optimization energy is %d",gc->compute_energy());
		gc->expansion(2);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
		printf("\nAfter optimization energy is %d",gc->compute_energy());

		for ( int  i = 0; i < num_pixels; i++ )
			result[i] = gc->whatLabel(i);

		delete gc;
	}
	catch (GCException e){
		e.Report();
	}

	delete [] result;
	delete [] data;

}
示例#2
0
////////////////////////////////////////////////////////////////////////////////
// in this version, set data and smoothness terms using arrays
// grid neighborhood structure is assumed
//
void GridGraph_DArraySArray(int width,int height,int num_pixels,int num_labels)
{

	int *result = new int[num_pixels];   // stores result of optimization

	// first set up the array for data costs
	int *data = new int[num_pixels*num_labels];
	for ( int i = 0; i < num_pixels; i++ )
		for (int l = 0; l < num_labels; l++ )
			if (i < 25 ){
				if(  l == 0 ) data[i*num_labels+l] = 0;
				else data[i*num_labels+l] = 10;
			}
			else {
				if(  l == 5 ) data[i*num_labels+l] = 0;
				else data[i*num_labels+l] = 10;
			}
	// next set up the array for smooth costs
	int *smooth = new int[num_labels*num_labels];
	for ( int l1 = 0; l1 < num_labels; l1++ )
		for (int l2 = 0; l2 < num_labels; l2++ )
			smooth[l1+l2*num_labels] = (l1-l2)*(l1-l2) <= 4  ? (l1-l2)*(l1-l2):4;


	try{
		GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);
		gc->setDataCost(data);
		gc->setSmoothCost(smooth);
		printf("\nBefore optimization energy is %d",gc->compute_energy());
		gc->expansion(2);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
		printf("\nAfter optimization energy is %d",gc->compute_energy());

		for ( int  i = 0; i < num_pixels; i++ )
			result[i] = gc->whatLabel(i);

		delete gc;
	}
	catch (GCException e){
		e.Report();
	}

	delete [] result;
	delete [] smooth;
	delete [] data;

}
示例#3
0
////////////////////////////////////////////////////////////////////////////////
// smoothness and data costs are set up one by one, individually
// grid neighborhood structure is assumed
//
void GridGraph_Individually(int width,int height,int num_pixels,int num_labels)
{

	int *result = new int[num_pixels];   // stores result of optimization



	try{
		GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);

		// first set up data costs individually
		for ( int i = 0; i < num_pixels; i++ )
			for (int l = 0; l < num_labels; l++ )
				if (i < 25 ){
					if(  l == 0 ) gc->setDataCost(i,l,0);
					else gc->setDataCost(i,l,10);
				}
				else {
					if(  l == 5 ) gc->setDataCost(i,l,0);
					else gc->setDataCost(i,l,10);
				}

		// next set up smoothness costs individually
		for ( int l1 = 0; l1 < num_labels; l1++ )
			for (int l2 = 0; l2 < num_labels; l2++ ){
				int cost = (l1-l2)*(l1-l2) <= 4  ? (l1-l2)*(l1-l2):4;
				gc->setSmoothCost(l1,l2,cost); 
			}

		printf("\nBefore optimization energy is %d",gc->compute_energy());
        gc->expansion(2);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
		printf("\nAfter optimization energy is %d",gc->compute_energy());

		for ( int  i = 0; i < num_pixels; i++ )
			result[i] = gc->whatLabel(i);

		delete gc;
	}
	catch (GCException e){
		e.Report();
	}

	delete [] result;
}
void mexFunctionReal(	int		nlhs, 		/* number of expected outputs */
		 			mxArray	*plhs[],	/* mxArray output pointer array */
			 		int		nrhs, 		/* number of inputs */
				 	const mxArray	*prhs[]		/* mxArray input pointer array */)
{
	//
	// Get input
	//	
	ASSERT( nlhs == 2 && nrhs == 10);
	matrix<double> mxC1  = prhs[0];
	matrix<double> mxC2	 = prhs[1];
	matrix<float> mxKP1 = prhs[2];
	matrix<float> mxKP2 = prhs[3];
	matrix<double> mxKPSize1 = prhs[4];
	matrix<double> mxKPSize2 = prhs[5];
	matrix<double> mxIrange = prhs[6];
	matrix<double> mxJrange = prhs[7];
	matrix<int> mxShiftI = prhs[8];
	matrix<int> mxShiftJ = prhs[9];

	ASSERT( mxC1.O == 3 );
	ASSERT( mxC2.O == 3 );
	ASSERT( mxKPSize1.numel() == 2 );
	ASSERT( mxKPSize2.numel() == 2 );
	ASSERT(  mxIrange.numel() == 2 );
	ASSERT(  mxJrange.numel() == 2 );
	
	M1 = mxKPSize1[0];
	N1 = mxKPSize1[1];
	M2 = mxKPSize2[0];
	N2 = mxKPSize2[1];
	ASSERT( mxC1.M == M1 && mxC1.N == N1); // fulhack
	ASSERT( mxC2.M == M2 && mxC2.N == N2);
	ASSERT( mxShiftI.M == M1 && mxShiftI.N == N1);
	ASSERT( mxShiftJ.M == M1 && mxShiftJ.N == N1);
	ASSERT( mxKP1.numel() == kpLength*M1*N1);
	ASSERT( mxKP2.numel() == kpLength*M2*N2);
	
	//
	// Range of shifts to consider
	//
	ASSERT(mxIrange[0]>=-std::max(M1,M2)+1 && mxIrange[1]<=std::max(M1,M2)-1);
	ASSERT(mxJrange[0]>=-std::max(N1,N2)+1 && mxJrange[1]<=std::max(N1,N2)-1);
	max_shift_i = mxIrange[1];
	min_shift_i = mxIrange[0];
	max_shift_j = mxJrange[1];
	min_shift_j = mxJrange[0];
	// Compute number of labels
	const int num_labels = (max_shift_i-min_shift_i+1)*(max_shift_j-min_shift_j+1);
	
	
	mexPrintf("Shift-map %d x %d  with %d labels\n",M1,N1,num_labels);
	mexPrintf("I-range %d ... %d \n",min_shift_i,max_shift_i);
	mexPrintf("J-range %d ... %d \n",min_shift_j,max_shift_j);
	
	//
	// Create graph
	//
	GCoptimizationGridGraph gc_obj(M1,N1,num_labels);
	GCoptimizationGridGraph* gc = &gc_obj;

	// set up the needed data to pass to function for the data costs
	ForDataFn toFn;
	toFn.c1		= mxC1;
	toFn.c2		= mxC2;
	toFn.kp1 	= mxKP1;
	toFn.kp2 	= mxKP2;
	toFn.shiftI = mxShiftI;
	toFn.shiftJ = mxShiftJ;
	
	gc->setDataCost(&dataFn,&toFn);
	gc->setSmoothCost(&smoothFn, &toFn);
	
	// Initialize shift-map to 0
	mexPrintf("Starting shift-map: di=0, dj=0\n");
	int zero_label = shift2lab(0,0);
	for (int i=0;i<M1*N1;++i) {
		gc->setLabel(i,zero_label);
	}

	// Print energy informaion
	double energy = gc->compute_energy();
	double dataEnergy = gc->giveDataEnergy();
	double smoothEnergy = gc->giveSmoothEnergy();
	mexPrintf("Start energy : %16.0f\n",energy);
	mexPrintf("Start data   : %16.0f\n",dataEnergy);
	mexPrintf("Start smooth : %16.0f\n",smoothEnergy);
	mexPrintf("Stopping when old_energy*%f < new_energy.\n",cutoff);
	flush_output();
	
	double old_energy = energy/cutoff + 1;
	int iter = 1;
	startTime();
	while ( cutoff*old_energy > energy  && iter <= 100)
	{
		old_energy = energy;
		//energy = gc->expansion(1);
		for (int lab=0;  lab < num_labels;  lab++ )
		{
			gc->alpha_expansion(lab);
		}
		energy = gc->compute_energy();
		dataEnergy = gc->giveDataEnergy();
		smoothEnergy = gc->giveSmoothEnergy();
		double time_taken = endTime(); // Measure the time taken since last call to endtime
		mexPrintf("Iteration %3d:   T:%16.0f   D:%16.0f   S:%16.0f  time: %.2f sec\n",iter,energy,dataEnergy,smoothEnergy,time_taken);
		iter++;
		endTime(); //Don't measure the time taken by the output
	}
	
	mexPrintf("Final energy : %16.0f\n",gc->compute_energy());
	
    
     
     
	//
	// Create output
	//
	matrix<int> shiftI_out(M1,N1);
	matrix<int> shiftJ_out(M1,N1);
	plhs[0] = shiftI_out;
	plhs[1] = shiftJ_out;
	
	for ( int  ind = 0; ind < M1*N1; ind++ ) {
		int di,dj;
		int lab = gc->whatLabel(ind);
		lab2shift(lab,di,dj);
		shiftI_out[ind] = di + mxShiftI[ind];
		shiftJ_out[ind] = dj + mxShiftJ[ind];
	}
}
示例#5
0
void GraphCut::GridGraph_Individually(int width,int height,int num_pixels,int num_labels)
{

    int *result = new int[num_pixels];   // stores result of optimization

    try{
        GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);

        std::vector<cv::Vec3b>::iterator it;
        for ( int i = 0; i < num_pixels; i++ ){
            int currentRow = i/this->rawImage.cols;
            int currentCol = i%this->rawImage.cols;

            //////////////////////////直接使用initGuess,start
            cv::Vec3b currentValue = this->initGuess.at<cv::Vec3b>(currentRow,currentCol);
            it = std::find(this->label2Value.begin(), this->label2Value.end(), currentValue);
            int label = it - this->label2Value.begin();
            for(int labelIndex = 0; labelIndex < this->CLASS_NUMBER; labelIndex++){
                if(label == labelIndex){
                    gc->setDataCost(i,labelIndex,1);
                }else{
                    gc->setDataCost(i,labelIndex,4);
                }
            }
            //////////////////////////直接使用initGuess,end


            //////////////////////////使用GMM,start
            /*for(int labelIndex = 0; labelIndex < this->CLASS_NUMBER; labelIndex++){
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                double gmmProbability = this->GMMProbability.at<cv::Vec<double,3> >(currentRow,currentCol)[labelIndex];
                gc->setDataCost(i,labelIndex,1*-log(gmmProbability));
            }*/
            //////////////////////////使用GMM,end

        }

        // next set up smoothness costs individually
        for ( int l1 = 0; l1 < num_labels; l1++ ){
            for (int l2 = 0; l2 < num_labels; l2++ ){
                int cost = (l1-l2)*(l1-l2) <= 32  ? 2*(l1-l2)*(l1-l2):32;
                gc->setSmoothCost(l1,l2,5*cost);
            }
        }

        //printf("\nBefore optimization energy is %d",static_cast<int>(gc->compute_energy()));
        //gc->expansion(5);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
        gc->swap(5);
        //printf("\nAfter optimization energy is %d\n",static_cast<int>(gc->compute_energy()));

        for ( int  i = 0; i < num_pixels; i++ ){
            result[i] = gc->whatLabel(i);
            this->resultLabel.at<cv::Vec3b>(i/this->resultLabel.cols, i%this->resultLabel.cols) = this->label2Value.at(result[i]);
        }

        delete gc;
    }
    catch (GCException e){
        e.Report();
    }
    delete [] result;
}
示例#6
0
文件: main.cpp 项目: jobinkv/docSeg
////////////////////////////////////////////////////////////////////////////////
// smoothness and data costs are set up one by one, individually
// grid neighborhood structure is assumed
//
Mat GridGraph_Individually(int num_labels,Mat img,int lambda)
{

	int height=img.rows;//HEIGHT
	int width=img.cols;//width
	int num_pixels=height*width;

	int *result = new int[num_pixels];   // stores result of optimization
	int rw;
	int col;
	Mat  opimage =img.clone();
//image is transformed int 1 drow in row major order

	try{
		GCoptimizationGridGraph *gc = new GCoptimizationGridGraph(width,height,num_labels);

		// first set up data costs individually


		for ( int i = 0; i < num_pixels; i++ )
		{
			if((i+1)%width==0 )
			{
				rw=((i+1)/width)-1;
				col=width-1;

			}	
			else
			{
			rw=(i+1)/width;
			col=((i+1)%width)-1;
			}

			int blue=img.at<cv::Vec3b>(rw,col)[0];
			int green=img.at<cv::Vec3b>(rw,col)[1];
			int red=img.at<cv::Vec3b>(rw,col)[2];



			for (int l = 0; l < num_labels; l++ )
			{
				if(l==0)
					 gc->setDataCost(i,l,(255-blue)/*+red+green*/);
			 	if(l==1)
			 		gc->setDataCost(i,l,(255-green)/*+red+blue*/);
		 		if(l==2)
		 			gc->setDataCost(i,l,(255-red)/*+blue+green*/);

			}
		}

		// next set up smoothness costs individually
		for ( int l1 = 0; l1 < num_labels; l1++ )
			for (int l2 = 0; l2 < num_labels; l2++ )
			{

				if(l1==l2)
				//int cost = (l1-l2)*(l1-l2) <= 4  ? (l1-l2)*(l1-l2):4;
				gc->setSmoothCost(l1,l2,0);

				else

				gc->setSmoothCost(l1,l2,lambda);


			}

		//printf("\nBefore optimization energy is %d",gc->compute_energy());
		gc->expansion(2);// run expansion for 2 iterations. For swap use gc->swap(num_iterations);
		//printf("\nAfter optimization energy is %d",gc->compute_energy());


		

		for ( int  i = 0; i < num_pixels; i++ )
		{
			result[i] = gc->whatLabel(i);
			if((i+1)%width==0 )
			{
				rw=((i+1)/width)-1;
				col=width-1;
			}
			else
			{
				rw=(i+1)/width;
				col=((i+1)%width)-1;
			}
			if(result[i]==0) //sky
			{
		//cout<<"label 0 \n";
				opimage.at<cv::Vec3b>(rw,col)[0]=255;//blue
				opimage.at<cv::Vec3b>(rw,col)[1]=0;
				opimage.at<cv::Vec3b>(rw,col)[2]=0;
			}
			if(result[i]==1) // grass
			{
			opimage.at<cv::Vec3b>(rw,col)[0]=0;
			opimage.at<cv::Vec3b>(rw,col)[1]=255;
			opimage.at<cv::Vec3b>(rw,col)[2]=0;
			//cout<<"label 1 \n";
			}
			if(result[i]==2) //third object
			{
				opimage.at<cv::Vec3b>(rw,col)[0]=0;
				opimage.at<cv::Vec3b>(rw,col)[1]=0;
				opimage.at<cv::Vec3b>(rw,col)[2]=255;//red
			}
		}





		//imwrite( "outputimage.png", opimage );


		delete gc;
	}
	catch (GCException e)
	{
		e.Report();
	}
	delete [] result;
	return opimage;
}