Blame view

func_mPb.cpp 3.31 KB
b71cc8bb   Tianshu Cheng   mPb using 3 channels
1
2
  #include <stim/image/image.h>
  #include <cmath>
b71cc8bb   Tianshu Cheng   mPb using 3 channels
3
4
5
6
  #include <stim/visualization/colormap.h>
  #include <stim/image/image_contour_detection.h>
  #include <sstream>
  
7fab7a98   Tianshu Cheng   a neat version of...
7
  /// This function evaluates the multicale Pb given an multi-channel image
b71cc8bb   Tianshu Cheng   mPb using 3 channels
8
  
7fab7a98   Tianshu Cheng   a neat version of...
9
10
11
12
13
14
  /// @param img is the multi-channel image
  /// @param theta_n is the number of angles used for computing the gradient
  /// @param r is an array of radii for different scaled discs(filters)
  /// @param alpha is an array of weights for different scaled discs(filters)
  /// @param s is the number of scales
  stim::image<float> func_mPb(stim::image<float> img, unsigned int theta_n, int* r, float* alpha, int s){
b71cc8bb   Tianshu Cheng   mPb using 3 channels
15
  
7fab7a98   Tianshu Cheng   a neat version of...
16
17
  	std::clock_t start;      // (optional) set the timer to calculate the total time 
  	start = std::clock();    // (optional) set timer start point
b71cc8bb   Tianshu Cheng   mPb using 3 channels
18
  
7fab7a98   Tianshu Cheng   a neat version of...
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  
  	std::ostringstream ss;                              // (optional) set the stream to designate the test result file
  	
  	unsigned int w = img.width();						// get the width of picture
  	unsigned int h = img.height();						// get the height of picture
  	unsigned int N = w * h;								// get the number of pixels
  
  	stim::image<float> mPb_theta(w,h);                  // allocate space for theta-dependent multicale Pb (mPb_theta)
  	stim::image<float> mPb(w,h);                        // allocate space for multicale Pb (mPb)
  
  	unsigned size = mPb.size();							// get the size of mPb
  	memset ( mPb.data(), 0, size * sizeof(float));      // initialize all the pixels of mPb to 0
  
  	float* ptr;											   // set a pointer 
  	ptr = (float*) malloc(size * sizeof(float) * theta_n); // this pointer points to a continuous space allocated to store all the mPb_theta
b71cc8bb   Tianshu Cheng   mPb using 3 channels
34
  
40d11588   Tianshu Cheng   2D inseparable co...
35
  	for (unsigned int n = 0; n < theta_n; n++){
b71cc8bb   Tianshu Cheng   mPb using 3 channels
36
  	
7fab7a98   Tianshu Cheng   a neat version of...
37
38
39
  		ss << "data_output/mPb_theta"<< n << "_0911.bmp";    // (optional) set the name for test result file
  		std::string sss = ss.str();							// (optional) 
  		float theta = 180 * ((float)n/theta_n);              // calculate the even-splited angle for each mPb_theta
b71cc8bb   Tianshu Cheng   mPb using 3 channels
40
  
7fab7a98   Tianshu Cheng   a neat version of...
41
  		mPb_theta = func_mPb_theta(img, theta, r, alpha, s); // calculate the mPb_theta
b71cc8bb   Tianshu Cheng   mPb using 3 channels
42
  
7fab7a98   Tianshu Cheng   a neat version of...
43
44
  		float* ptr_n = &ptr[ n * w * h * 1 ];       // set a pointer which points to the space for each mPb_theta
  		mPb_theta.data_noninterleaved(ptr_n);       // set this pointer to point to the each mPb_theta
b71cc8bb   Tianshu Cheng   mPb using 3 channels
45
  
7fab7a98   Tianshu Cheng   a neat version of...
46
47
  		double duration1 = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;             // (optional) calculate the time for generating each mPb_theta
  		std::cout<<"mPb_theta_"<< theta <<" complished   time:"<< duration1 <<"s"<<'\n';   // (optional) show this time 
b71cc8bb   Tianshu Cheng   mPb using 3 channels
48
  
b71cc8bb   Tianshu Cheng   mPb using 3 channels
49
  
7fab7a98   Tianshu Cheng   a neat version of...
50
51
52
53
  		unsigned long idx = n * w * h * 1;  //index for the nth mPb_theta
  
  		
  		stim::cpu2image(mPb_theta.data(), sss, w, h, stim::cmBrewer);	 // (optional) output the nth mPb_theta
b71cc8bb   Tianshu Cheng   mPb using 3 channels
54
55
56
  
  		for(unsigned long i = 0; i < N; i++){
  		
7fab7a98   Tianshu Cheng   a neat version of...
57
  			float pixel = ptr[i+idx];           //get the ith pixel in nth mPb_theta
b71cc8bb   Tianshu Cheng   mPb using 3 channels
58
  
7fab7a98   Tianshu Cheng   a neat version of...
59
  			if(pixel > mPb.data()[i]){          //get the maximum value among all mPb_theta for ith pixel
b71cc8bb   Tianshu Cheng   mPb using 3 channels
60
61
62
63
64
65
66
67
  				mPb.data()[i] = pixel;
  			}
  			
  			else{
  			}
  		}
  
  		
7fab7a98   Tianshu Cheng   a neat version of...
68
  		ss.str("");                  //(optional) clear the space for stream
b71cc8bb   Tianshu Cheng   mPb using 3 channels
69
  
b71cc8bb   Tianshu Cheng   mPb using 3 channels
70
71
  	}              
  
7fab7a98   Tianshu Cheng   a neat version of...
72
  	stim::cpu2image(mPb.data(), "data_output/mPb_500_0911_neat.bmp", w, h, stim::cmBrewer);  // output the mPb
b71cc8bb   Tianshu Cheng   mPb using 3 channels
73
  
7fab7a98   Tianshu Cheng   a neat version of...
74
75
  	double duration2 = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; // (optional) calculate the total time 
  	std::cout<<"total time:"<< duration2 <<"s"<<'\n';                      // (optional) show the total time 
b71cc8bb   Tianshu Cheng   mPb using 3 channels
76
77
78
  
  	return mPb; 
  
abaf5630   David Mayerich   fixed the signed/...
79
  }