Esempio n. 1
0
void SetupWeights (int N, const Parameter &param, const blitz::Array<double, 1> &r, blitz::Array<double, 1> &weight)
{
	Array<double, 1> x(N);
	weight.resize(N);

	// Basic Clenshaw Curtis points and  weigths 
	x = cos(M_PI * (tensor::i +1) / (N+1));

	for (int k=0; k<N; k++)
	{
		weight(k) = 1.0;
		for (int i=1; i<(N+1)/2; i++)
		{ 
			weight(k) -= 2.0/(4*i*i-1) *cos(2.0*M_PI*i*(k+1)/(N+1));
		}
		if (N%2 == 1)
		{	
			weight(k) -= 1/(N*N+2*N) *cos((N+1)*x(N/2));
		}
		weight(k) = 2.0/(N+1) * weight(k);
	}   

	// Scaling the weights according to transformations
	if (param.Range == TransformRangeRadial)
	{
		if (param.Type == TransformTypeAlgebraic) 
		{
			weight *= param.Scaling * 2.0 / ((1+x)*(1+x));
		}
		else if (param.Type == TransformTypeTrigonometric)
		{ 
			double pi_4 = M_PI/4.0;
			weight *= param.Scaling * pi_4 / sqr(cos(pi_4*(1+x)));
		}
		else if (param.Type == TransformTypeLogarithmic) 
		{
			weight *= param.Scaling * 6.0 / (1.0 - x);
		}
		else
		{
			cout << "Invalid parameter type " << param.Type << endl;
			throw std::runtime_error("Invalid parameter type");
		}
	}	
	else if (param.Range == TransformRangeCartesian)
	{
		if (param.Type == TransformTypeAlgebraic) 
		{
			weight *= param.Scaling * 1.0 / pow(1 - x*x, 1.5);
		}
		/* Not implemented for Cartesian range
		else if (param.Type == TransformTypeTrigonometric)
		{ 
			double pi_4 = M_PI/4.0;
			weight *= 
		}
		else if (param.Type == TransformTypeLogarithmic) 
		{
			weight *= 
		}
		else
		{
			cout << "Invalid parameter type " << param.Type << endl;
			throw std::runtime_error("Invalid parameter type");
		}
		*/
	}

}
Esempio n. 2
0
int main( int argc, char **argv)
{
std::string inpf, trif, outf, backf;
adobe::dictionary_t params;

bool use_lab = false;

	try
	{
	po::options_description desc("Allowed options");
	po::variables_map vm;
	po::positional_options_description p;

		p.add( "input" , 1);
		p.add( "trimap", 1);

		desc.add_options()	("help", "produce help message")
							// files
							("input", po::value<std::string>(), "input file")
							("trimap",po::value<std::string>(), "trimap file")
							("output,o", po::value<std::string>(), "output file")
							("background,bg", po::value<std::string>(), "clean background")
							("save_bg", "save estimated background")

							// colors
							("rgb", "use rgb colorspace")

							// sampling
							("kwin", po::value<int>()->default_value( 8), "known window radius")
							("uwin", po::value<int>()->default_value( 8), "unknown window radius")
							("mins", po::value<int>()->default_value( 16), "min samples")
							
							// clusters
							("clusters", po::value<int>()->default_value( 5), "max number of clusters")
							("ctheresh", po::value<double>()->default_value( 0.001), "cluster thereshold")
	
							// optimize
							("cvar", po::value<double>()->default_value( 0.04), "camera variance")

							// gui
							("window,w", "show results in a window")
							; // end

		po::store( po::command_line_parser( argc, argv).options( desc).positional( p).run(), vm);
		po::notify( vm);

		if( vm.count( "help") || !vm.count("input") || !vm.count("trimap"))
			usage();
		
		if( (!vm.count("output")) && (!vm.count("window")))
			usage();
			
		inpf = vm["input"].as<std::string>();
		trif = vm["trimap"].as<std::string>();
		
		if( vm.count("output"))
			outf = vm["output"].as<std::string>();

		use_lab = !vm.count("rgb");
		
		params[adobe::name_t("use_lab")] = adobe::any_regular_t( use_lab);
		
		// load images
		image.reference( read_image( inpf.c_str()));
		trimap.reference( read_trimap( trif.c_str()));

		if( (trimap.rows() != image.rows()) || (trimap.cols() != image.cols()))
		{
			std::cout << "Image & trimap dimensions don't match\n";
			exit( boost::exit_failure);
		}

		if( use_lab)
			convert2lab( image);
		
		if( vm.count( "background"))
		{
			backf = vm["background"].as<std::string>();
			params[adobe::name_t("use_back")] = adobe::any_regular_t( true);

			bg.reference( read_image( backf.c_str()));
			
			if( use_lab)
				convert2lab( bg);
		}
		else
		{
			bg.resize( image.rows(), image.cols());
			params[adobe::name_t("use_back")] = adobe::any_regular_t( false);
		}

		// sampling
		params[ adobe::name_t( "kwin_size")] = adobe::any_regular_t( vm["kwin"].as<int>());
		params[ adobe::name_t( "uwin_size")] = adobe::any_regular_t( vm["uwin"].as<int>());
		params[ adobe::name_t( "min_samples")] = adobe::any_regular_t( vm["mins"].as<int>());

		// cluster
		params[ adobe::name_t( "maxk")] = adobe::any_regular_t( vm["clusters"].as<int>());
		params[ adobe::name_t( "ctheresh")] = adobe::any_regular_t( vm["ctheresh"].as<double>());

		// optimize
		params[ adobe::name_t( "cvar")] = adobe::any_regular_t( vm["cvar"].as<double>());

		fg.resize( image.rows(), image.cols());
		alpha.resize( image.rows(), image.cols());
		
		bmatte bm( image, trimap, fg, bg, alpha, params);
		bm();

		if( use_lab)
		{
			convert2rgb( image);
			convert2rgb( fg);
			convert2rgb( bg);
		}

		if( vm.count( "output"))
		{
			save_image( outf.c_str(), fg, &alpha);
		
			if( vm.count( "save_bg"))
			{
			std::string bgf = "bg_" + outf;

				save_image( bgf.c_str(), bg);
			}
		}

		if( vm.count( "window"))
		{
			comp.resize( image.rows(), image.cols());
		
			for( int j=0;j<image.rows();++j)
			{
				for( int i=0;i<image.cols();++i)
				{
					float t = alpha( j, i);
					comp( j, i) = (fg( j, i) * t) + ( Imath::Color3f( .5f, .5f, .5f) * (1.0f - t));
				}
			}

			glutInit(&argc, argv);
			glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
	
			glutInitWindowPosition( 50, 50);

			glutInitWindowSize( image.cols(), image.rows());
			glutCreateWindow( "BMatte");

			glutKeyboardFunc( key);
			glutMouseFunc( mouse);

			glutDisplayFunc( display);

			gl_init();
			glutMainLoop();
		}
	}
    catch( std::exception& e)
	{
        std::cerr << "error: " << e.what() << "\n";
        return boost::exit_failure;
    }

    catch( ...) { std::cerr << "Exception of unknown type!\n";}
	return boost::exit_success;
}