textons.cpp 2.4 KB
#include <stim/image/image.h>
//#include <cmath>
#include <stim/visualization/colormap.h>
#include <stim/image/image_contour_detection.h>
#include <sstream>

/// This function convolve the grayscale image with a set of oriented Gaussian 
/// derivative filters, and return a texton image with (theta_n*2+1) channels

/// @param image is an one-channel grayscale image
/// @param theta_n is the number of angles used for computing the gradient

stim::image<float> textons(stim::image<float> image, unsigned int theta_n){

	unsigned int w = image.width();    // get the width of picture
	unsigned int h = image.height();   // get the height of picture
	unsigned N = w * h;				   // get the number of pixels of picture

	stim::image<float> textons(w, h, 1, theta_n*2+1);    // allocate space for textons
	stim::image<float> temp(w, h);						 // allocate space for temp

	int sigma = 1;     // set sigma for odd-symmetric, even-symmetric and center-surround filter filter

	//std::ostringstream ss1, ss2;                      // (optional) set the stream to designate the test result file

	for (unsigned int i = 0; i < theta_n; i++){

		//ss1 << "data_output/textons_channel_"<< i << ".bmp";  // set the name for test result file (optional)
		//std::string sss1 = ss1.str();
		//ss2 << "data_output/textons_channel_"<< i+theta_n << ".bmp";  // set the name for test result file (optional)
		//std::string sss2 = ss2.str();
		float theta = 180 * ((float)i/theta_n);              // calculate the even-splited angle for each oriented filter

		temp = dG1_theta_conv2(image, sigma, theta);     // return dG1_theta_conv2 to temp
		//stim::cpu2image(temp.data(), sss1, w, h, stim::cmBrewer);  
		textons.set_channel(i, temp.data());                 // copy temp to ith channel of textons

		temp = dG2_d2x_theta_conv2(image, sigma, theta);  // return dG2_d2x_theta_conv2 to temp
		//stim::cpu2image(temp.data(), sss2, w, h, stim::cmBrewer);  
		textons.set_channel(i + theta_n, temp.data());       // copy temp to (i+theta_n)th channel of textons

		//ss1.str("");   //(optional) clear the space for stream
		//ss2.str("");   //(optional) clear the space for stream


	temp = laplacian_conv2(image, sigma);  // return laplacian_conv2 to temp
	//stim::cpu2image(temp.data(), "data_output/textons_channel_16.bmp", w, h, stim::cmBrewer);  
	textons.set_channel(theta_n*2, temp.data());        // copy temp to (theta_n*2)th channel of textons

	return textons;