RMatrixXf evalBoundary( const RMatrixXf & d, const std::vector<RMatrixXb> & bnd, int nthres, double max_r, const RMatrixXb & mask ) {
	RMatrixXf r( nthres, 5 );
	for( int i=0; i<nthres; i++ ) {
		float t = 1.0 * i / nthres;
		r(i,0) = t;
		RMatrixXb tmp = d.array() > t;
		if ( t > 0 )
			thinningGuoHall( tmp );
		r.block(i,1,1,4) = evalBoundaryBinary( tmp, bnd, max_r, mask ).cast<float>().transpose();
	}
	return r;
}
VectorXf project1D( const RMatrixXf & Y, int * rep_label=NULL ) {
// 	const int MAX_SAMPLE = 20000;
	const bool fast = true, very_fast = true;
	// Remove the DC (Y : N x M)
	RMatrixXf dY = Y.rowwise() - Y.colwise().mean();
// 	RMatrixXf sY = dY;
// 	if( 0 < MAX_SAMPLE && MAX_SAMPLE < dY.rows() ) {
// 		VectorXi samples = randomChoose( dY.rows(), MAX_SAMPLE );
// 		std::sort( samples.data(), samples.data()+samples.size() );
// 		sY = RMatrixXf( samples.size(), dY.cols() );
// 		for( int i=0; i<samples.size(); i++ )
// 			sY.row(i) = dY.row( samples[i] );
// 	}
	
	// ... and use (pc > 0)
	VectorXf lbl = VectorXf::Zero( Y.rows() );
	
	// Find the largest PC of (dY.T * dY) and project onto it
	if( very_fast ) {
		// Find the largest PC using poweriterations
		VectorXf U = VectorXf::Random( dY.cols() );
		U = U.array() / U.norm()+std::numeric_limits<float>::min();
		for( int it=0; it<20; it++ ){
			// Normalize
			VectorXf s = dY.transpose()*(dY*U);
			s.array() /= s.norm()+std::numeric_limits<float>::min();
			if ( (U-s).norm() < 1e-6 )
				break;
			U = s;
		}
		// Project onto the PC
		lbl = dY*U;
	}
	else if(fast) {
		// Compute the eigen values of the covariance (and project onto the largest eigenvector)
		MatrixXf cov = dY.transpose()*dY;
		SelfAdjointEigenSolver<MatrixXf> eigensolver(0.5*(cov+cov.transpose()));
		MatrixXf ev = eigensolver.eigenvectors();
		lbl = dY * ev.col( ev.cols()-1 );
	}
	else {
		// Use the SVD
		JacobiSVD<RMatrixXf> svd = dY.jacobiSvd(ComputeThinU | ComputeThinV );
		// Project onto the largest PC
		lbl = svd.matrixU().col(0) * svd.singularValues()[0];
	}
	// Find the representative label
	if( rep_label )
		dY.array().square().rowwise().sum().minCoeff( rep_label );
	
	return (lbl.array() < 0).cast<float>();
}
RMatrixXf SeedFeature::computeObjFeatures( const ImageOverSegmentation & ios ) {
	Image rgb_im = ios.image();
	const RMatrixXs & s = ios.s();
	const Edges & g = ios.edges();
	const int Ns = ios.Ns();
	
	RMatrixXf r = RMatrixXf::Zero( Ns, N_OBJ_F );
	if( N_OBJ_F<=1 ) return r;
	VectorXf area  = bin( s, 1, [&](int x, int y){ return 1.f; } );
	VectorXf norm = (area.array()+1e-10).cwiseInverse();
	
	r.col(0).setOnes();
	int o = 1;
	if (N_OBJ_COL>=6) {
		Image lab_im;
		rgb2lab( lab_im, rgb_im );
		r.middleCols(o,6) = norm.asDiagonal() * bin( s, 6, [&](int x, int y){ return makeArray<6>( lab_im(y,x,0), lab_im(y,x,1), lab_im(y,x,2), lab_im(y,x,0)*lab_im(y,x,0), lab_im(y,x,1)*lab_im(y,x,1), lab_im(y,x,2)*lab_im(y,x,2) ); } );
		RMatrixXf col = r.middleCols(o,3);
		if( N_OBJ_COL >= 9)
			r.middleCols(o+6,3) = col.array().square();
		o += N_OBJ_COL;
		
		// Add color difference features
		if( N_OBJ_COL_DIFF ) {
			RMatrixXf bcol = RMatrixXf::Ones( col.rows(), col.cols()+1 );
			bcol.leftCols(3) = col;
			for( int it=0; it*3+2<N_OBJ_COL_DIFF; it++ ) {
				// Apply a box filter on the graph
				RMatrixXf tmp = bcol;
				for( const auto & e: g ) {
					tmp.row(e.a) += bcol.row(e.b);
					tmp.row(e.b) += bcol.row(e.a);
				}
				bcol = tmp.col(3).cwiseInverse().asDiagonal()*tmp;
				r.middleCols(o,3) = (bcol.leftCols(3)-col).array().abs();
				o += 3;
			}
		}
	}
	if( N_OBJ_POS >= 2 ) {
		RMatrixXf xy = norm.asDiagonal() * bin( s, 2, [&](int x, int y){ return makeArray<2>( 1.0*x/(s.cols()-1)-0.5, 1.0*y/(s.rows()-1)-0.5 ); } );
		r.middleCols(o,2) = xy;
		o+=2;
		if( N_OBJ_POS >=4 ) {
			r.middleCols(o,2) = xy.array().square();
			o+=2;
		}
	}
	if( N_OBJ_EDGE ) {
		RMatrixXf edge_map = DirectedSobel().detect( rgb_im );
		for( int j=0; j<s.rows(); j++ )
			for( int i=0; i<s.cols(); i++ ) {
				const int id = s(j,i);
				int bin = edge_map(j,i)*N_OBJ_EDGE;
				if ( bin < 0 ) bin = 0;
				if ( bin >= N_OBJ_EDGE ) bin = N_OBJ_EDGE-1;
				r(id,o+bin) += norm[id];
			}
		o += N_OBJ_EDGE;
	}
	const int N_BASIC = o-1;
	// Add in context features
	for( int i=0; i<N_OBJ_CONTEXT; i++ ) {
		const int o0 = o - N_BASIC;
		// Box filter the edges
		RMatrixXf f = RMatrixXf::Ones( Ns, N_BASIC+1 ), bf = RMatrixXf::Zero( Ns, N_BASIC+1 );
		f.rightCols( N_BASIC ) = r.middleCols(o0,N_BASIC);
		for( Edge e: g ) {
			bf.row(e.a) += f.row(e.b);
			bf.row(e.b) += f.row(e.a);
		}
		r.middleCols(o,N_BASIC) = bf.col(0).array().max(1e-10f).inverse().matrix().asDiagonal() * bf.rightCols(N_BASIC);
		o += N_BASIC;
	}
	return r;
}