示例#1
0
	void operator()( tbb::blocked_range<int> rng ){
		double samples_per_image = 1.0*n_samples / lab_images.count();
	
		// Collect some statistics on mean and variance of each feature
		for( int i=rng.begin(), n_features=rng.begin()*samples_per_image; i<rng.end(); i++ ){
			Image< float > feature_response = feature->evaluate( lab_images[i], names[i] );
			for( int j=0; j<feature_response.height(); j++ )
				for( int i=0; i<feature_response.width(); i++ ){
					VectorXd x( feature_size );
					for( int k=0; k<feature_size; k++ )
						x[k] = feature_response(i,j,k);
					
					count += 1;
					VectorXd delta = x - mean;
					mean += delta / count;
					covariance += delta * (x-mean).transpose();
				}
			
			for(;n_features < (i+1)*samples_per_image; n_features++){
				int x = random() % feature_response.width();
				int y = random() % feature_response.height();
				for( int i=0; i<feature_size; i++ )
					features.append( feature_response( x, y, i ) );
			}
		}
	}
示例#2
0
void computeFeatures( QVector<float> & features, VectorXd & mean, MatrixXd & covariance, const QSharedPointer<Feature> & feature, const QVector< Image< float > >& lab_images, const QVector< QString >& names, int n_samples ){
	int feature_size = feature->size();
	int n_features = 0;
	double samples_per_image = 1.0*n_samples / lab_images.count();
	
	// Do propper whitening: http://en.wikipedia.org/wiki/White_noise#Whitening_a_random_vector
	
	// Collect some statistics on mean and variance of each feature
	mean = VectorXd::Zero( feature_size );
	covariance = MatrixXd::Zero( feature_size, feature_size );
	double count = 0;
	for( int i=0; i<lab_images.count(); i++ ){
		Image< float > feature_response = feature->evaluate( lab_images[i], names[i] );
		for( int j=0; j<feature_response.height(); j++ )
			for( int i=0; i<feature_response.width(); i++ ){
				VectorXd x( feature_size );
				for( int k=0; k<feature_size; k++ )
					x[k] = feature_response(i,j,k);
				
				count += 1;
				VectorXd delta = x - mean;
				mean += delta / count;
				covariance += delta * (x-mean).transpose();
			}
		
		for(;n_features < (i+1)*samples_per_image; n_features++){
			int x = random() % feature_response.width();
			int y = random() % feature_response.height();
			for( int i=0; i<feature_size; i++ )
				features.append( feature_response( x, y, i ) );
		}
	}
	// Compute the final covariance
	covariance = covariance / count;
}
示例#3
0
Image< short > Texton::textonize(const Image< float >& lab_image, const QString & name ) const{
	Image< float > feature_response = feature_->evaluate( lab_image, name );
	// Whitening (zero mean, 1 stddev)
	for( int j=0; j<feature_response.height(); j++ )
		for( int i=0; i<feature_response.width(); i++ ){
			VectorXd x( feature_response.depth() );
			for( int k=0; k<feature_response.depth(); k++ )
				x[k] = feature_response(i,j,k);
			x = transformation_*(x - mean_);
			for( int k=0; k<feature_response.depth(); k++ )
				feature_response(i,j,k) = x[k];
		}
	return kmeans_.evaluate( feature_response );
}
// [ref] ${VXL_HOME}/contrib/mul/fhs/tools/find_matches.cxx
int fhs_find_matches_example(int argc, char *argv[])
{
	vul_arg<vcl_string> image1_path("-i1", "Input image 1");
	vul_arg<vcl_string> image2_path("-i2", "Input image 2");
	vul_arg<vcl_string> output_image1_path("-o1", "Output image 1", "output1.png");
	vul_arg<vcl_string> output_image2_path("-o2", "Output image 1", "output2.png");
	vul_arg<unsigned> level("-L", "Image pyramid level to work on", 2);
	vul_arg<unsigned> nc("-n", "Number of points to select", 10);
	vul_arg<unsigned> w("-w", "Half width of filters", 7);
	vul_arg<double> f("-f", "Relative strength of intensity vs shape", 0.1);

	vul_arg_parse(argc, argv);

	if (image1_path() == "" || image2_path() == "")
	{
		local::print_usage();
		return 0;
	}

	// ============================================
	// Attempt to load in images
	// ============================================

	vimt_image_2d_of<vxl_byte> image1, image2;
	image1.image() = vil_load(image1_path().c_str());
	if (image1.image().size() == 0)
	{
		vcl_cerr << "Unable to read in image from " << image1_path() << vcl_endl;
		return 1;
	}
	image2.image() = vil_load(image2_path().c_str());
	if (image2.image().size() == 0)
	{
		vcl_cerr <<" Unable to read in image from " << image2_path() << vcl_endl;
		return 1;
	}

	// ============================================
	// Build image pyramids and select chosen level
	// ============================================
	vimt_gaussian_pyramid_builder_2d<vxl_byte> pyr_builder;
	vimt_image_pyramid image_pyr1, image_pyr2;
	pyr_builder.build(image_pyr1, image1);
	pyr_builder.build(image_pyr2, image2);

	const vimt_image_2d_of<vxl_byte> &image1_L = static_cast<const vimt_image_2d_of<vxl_byte> &>(image_pyr1(level()));
	const vimt_image_2d_of<vxl_byte> &image2_L = static_cast<const vimt_image_2d_of<vxl_byte> &>(image_pyr2(level()));

	// ====================================================
	// Apply corner operator to image1_L and select corners
	// ====================================================

	vimt_image_2d_of<float> corner_im;
	corner_im.set_world2im(image1_L.world2im());
	vil_corners(image1_L.image(), corner_im.image());

	vcl_vector<unsigned> pi, pj;
	float threshold = 4.0f;
	vil_find_peaks_3x3(pi, pj, corner_im.image(), threshold);

	// Evaluate corner strength at each point (pi[i], pj[i])
	unsigned n = pi.size();
	vcl_vector<float> corner_str(n);
	for (unsigned i = 0 ; i < n; ++i)
		corner_str[i] = corner_im.image()(pi[i], pj[i]);

	// Sort and generate a list of image points and equivalent world points
	vcl_vector<unsigned> index;
	mbl_index_sort(corner_str, index);

	unsigned n_c = vcl_min(nc(),n);
	vcl_vector<vgl_point_2d<int> > im_pts(n_c);
	vcl_vector<vgl_point_2d<double> > w_pts(n_c);
	vimt_transform_2d im2w = image1_L.world2im().inverse();
	for (unsigned i = 0; i < n_c; ++i)
	{
		im_pts[i] = vgl_point_2d<int>(pi[index[n - 1 - i]], pj[index[n - 1 - i]]);
		w_pts[i] = im2w(pi[index[n - 1 - i]], pj[index[n - 1 - i]]);
	}


	// ========================================================
	// Extract patches around each selected point and normalise
	// ========================================================
	vcl_vector<vil_image_view<float> > patch(n_c);
	vcl_vector<vgl_point_2d<double> > patch_ref(n_c);  // Reference point
	int ni = image1.image().ni();
	int nj = image1.image().nj();
	for (unsigned i = 0; i < n_c; ++i)
	{
		// Select region around point, allowing for image edges.
		int ilo = vcl_max(0, int(im_pts[i].x() - w()));
		int ihi = vcl_min(ni - 1, int(im_pts[i].x() + w()));
		int jlo = vcl_max(0, int(im_pts[i].y() - w()));
		int jhi = vcl_min(nj - 1, int(im_pts[i].y() + w()));

		// Compute position of reference point relative to corner
		int kx = im_pts[i].x() - ilo;
		int ky = im_pts[i].y() - jlo;
		patch_ref[i] = vgl_point_2d<double>(kx, ky);
		vil_convert_cast(vil_crop(image1_L.image(), ilo, 1 + ihi - ilo, jlo, 1 + jhi - jlo), patch[i]);
		vil_math_normalise(patch[i]);
	}

	// Construct tree structure for points
	vcl_vector<vcl_pair<int, int> > pairs;
	mbl_minimum_spanning_tree(w_pts, pairs);

	assert(pairs.size() == n_c - 1);

	// Draw tree into image for display purposes
	local::draw_tree(image1.image(), w_pts, pairs);

	if (vil_save(image1.image(), output_image1_path().c_str()))
	{
		vcl_cout << "Saved output image 1 to " << output_image1_path() << vcl_endl;
	}

	// =================================================
	// Construct the arc model from the points and pairs
	// =================================================

	vcl_vector<fhs_arc> arcs(n_c - 1);
	int root_node = pairs[0].first;
	for (unsigned i = 0; i < pairs.size(); ++i)
	{
		int i1 = pairs[i].first;
		int i2 = pairs[i].second;
		vgl_vector_2d<double> dp = w_pts[i2] - w_pts[i1];
		double sd_x = vcl_max(0.1 * ni, 0.2 * dp.length());
		double sd_y = vcl_max(0.1 * nj, 0.2 * dp.length());
		arcs[i] = fhs_arc(i1, i2, dp.x(), dp.y(), sd_x * sd_x, sd_y * sd_y);
	}

	// =================================================
	// Apply filters to image2 (initially to whole image)
	// =================================================
	vcl_vector<vimt_image_2d_of<float> > feature_response(n_c);
	for (unsigned i = 0; i < n_c; ++i)
	{
		// Apply to whole image in first instance
		// Ideally would crop a region around expected position
		vimt_normalised_correlation_2d(image2_L, feature_response[i], patch[i], patch_ref[i], float());

		// Need good values to be small, not large, so apply -ve factor
		vil_math_scale_values(feature_response[i].image(), -f());
	}

	// ======================================================
	// Use fhs_searcher to locate equivalent points on image2
	// ======================================================

	fhs_searcher searcher;
	searcher.set_tree(arcs, root_node);
	searcher.search(feature_response);
	vcl_vector<vgl_point_2d<double> > pts2;
	searcher.best_points(pts2);

	// Draw tree into image for display purposes
	local::draw_tree(image2.image(), pts2, pairs);

	if (vil_save(image2.image(), output_image2_path().c_str()))
	{
		vcl_cout << "Saved output image 2 to " << output_image2_path() << vcl_endl;
	}

	return 0;
}