Blame view

gauss_derivative_odd.cpp 2.98 KB
b71cc8bb   Tianshu Cheng   mPb using 3 channels
1
2
3
  #include <stim/image/image.h>
  #include <cmath>
  #include <stim/visualization/colormap.h>
b71cc8bb   Tianshu Cheng   mPb using 3 channels
4
5
6
  
  #define PI 3.1415926
  
40d11588   Tianshu Cheng   2D inseparable co...
7
8
9
10
  void conv2(float* img, float* mask, float* cpu_copy, unsigned int w, unsigned int h, unsigned int M);
  void array_abs(float* img, unsigned int N);
  void array_multiply(float* lhs, float rhs, unsigned int N);
  
7fab7a98   Tianshu Cheng   a neat version of...
11
  /// This function evaluates the gaussian derivative gradient given an one-channel image
b71cc8bb   Tianshu Cheng   mPb using 3 channels
12
  
7fab7a98   Tianshu Cheng   a neat version of...
13
14
15
16
  /// @param img is the one-channel image
  /// @param r is an array of radii for different scaled discs(filters)
  //  @param sigma_n is the number of standard deviations used to define the sigma
  /// @param theta is angle used for computing the gradient
b71cc8bb   Tianshu Cheng   mPb using 3 channels
17
  
7fab7a98   Tianshu Cheng   a neat version of...
18
  stim::image<float> gaussian_derivative_filter_odd(stim::image<float> image, int r, unsigned int sigma_n, float theta){
e166b01b   Tianshu Cheng   simple mask test
19
  
7fab7a98   Tianshu Cheng   a neat version of...
20
21
22
23
24
  	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
  	int winsize = 2 * r + 1;           // set the winsdow size of disc(filter)
  	float sigma  = float(r)/float(sigma_n); // calculate the sigma used in gaussian function
e166b01b   Tianshu Cheng   simple mask test
25
  
7fab7a98   Tianshu Cheng   a neat version of...
26
27
28
29
  	stim::image<float> mask_x(winsize, winsize);    // allocate space for x-axis-oriented filter
  	stim::image<float> mask_y(winsize, winsize);    // allocate space for y-axis-oriented filter
  	stim::image<float> mask_theta(winsize, winsize);// allocate space for theta-oriented filter
  	stim::image<float> derivative_theta(w, h);      // allocate space for theta-oriented gradient
b71cc8bb   Tianshu Cheng   mPb using 3 channels
30
  
7fab7a98   Tianshu Cheng   a neat version of...
31
  	float theta_r = (theta * PI)/180; //change angle unit from degree to rad
b71cc8bb   Tianshu Cheng   mPb using 3 channels
32
  
7fab7a98   Tianshu Cheng   a neat version of...
33
34
  	for (int j = 0; j < winsize; j++){
  		for (int i = 0; i< winsize; i++){
b71cc8bb   Tianshu Cheng   mPb using 3 channels
35
  
7fab7a98   Tianshu Cheng   a neat version of...
36
37
  			int x = i - r;          //range of x
  			int y = j - r;          //range of y
b71cc8bb   Tianshu Cheng   mPb using 3 channels
38
39
  
  			// create the x-oriented gaussian derivative filter mask_x
7fab7a98   Tianshu Cheng   a neat version of...
40
  			mask_x.data()[j*winsize + i] = (-1) * x * exp((-1)*(pow(x, 2))/(2*pow(sigma, 2))) * exp((-1)*(pow(y, 2))/(2*pow(sigma, 2)));
b71cc8bb   Tianshu Cheng   mPb using 3 channels
41
  			// create the y-oriented gaussian derivative filter mask_y
7fab7a98   Tianshu Cheng   a neat version of...
42
  			mask_y.data()[j*winsize + i] = (-1) * y * exp((-1)*(pow(y, 2))/(2*pow(sigma, 2))) * exp((-1)*(pow(x, 2))/(2*pow(sigma, 2)));
b71cc8bb   Tianshu Cheng   mPb using 3 channels
43
  			// create the mask_theta
7fab7a98   Tianshu Cheng   a neat version of...
44
  			mask_theta.data()[j*winsize + i] = cos(theta_r) * mask_x.data()[j*winsize + i] + sin(theta_r) * mask_y.data()[j*winsize + i] ;
b71cc8bb   Tianshu Cheng   mPb using 3 channels
45
46
47
48
  		
  		}
  	}
  
7fab7a98   Tianshu Cheng   a neat version of...
49
  	//stim::cpu2image(mask_theta.data(), "data_output/mask_0911_2.bmp", winsize, winsize, stim::cmBrewer); // (optional) show the mask result
b71cc8bb   Tianshu Cheng   mPb using 3 channels
50
  
7fab7a98   Tianshu Cheng   a neat version of...
51
52
  	// do the 2D convolution with image and mask
  	conv2(image.data(), mask_theta.data(), derivative_theta.data(), w, h, winsize);
40d11588   Tianshu Cheng   2D inseparable co...
53
  
7fab7a98   Tianshu Cheng   a neat version of...
54
  	array_abs(derivative_theta.data(), N); // get the absolute value for each pixel (why slower than the "for loop" method sometimes?)
b71cc8bb   Tianshu Cheng   mPb using 3 channels
55
  
7fab7a98   Tianshu Cheng   a neat version of...
56
57
  	float max = derivative_theta.max();						// get the maximum of gradient used for normalization
  	array_multiply(derivative_theta.data(), 1/max, N);		// normalize the gradient
b71cc8bb   Tianshu Cheng   mPb using 3 channels
58
  
b71cc8bb   Tianshu Cheng   mPb using 3 channels
59
  
7fab7a98   Tianshu Cheng   a neat version of...
60
  	//stim::cpu2image(derivative_theta.data(), "data_output/derivative_theta_0911.bmp", w, h, stim::cmBrewer); // (optional) show the gradient result
b71cc8bb   Tianshu Cheng   mPb using 3 channels
61
62
63
  
  	return derivative_theta;
  
abaf5630   David Mayerich   fixed the signed/...
64
  }