func_mPb.cpp
3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stim/image/image.h>
#include <cmath>
#include <stim/visualization/colormap.h>
#include <stim/image/image_contour_detection.h>
#include <sstream>
/// This function evaluates the multicale Pb given an multi-channel image
/// @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){
std::clock_t start; // (optional) set the timer to calculate the total time
start = std::clock(); // (optional) set timer start point
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
for (unsigned int n = 0; n < theta_n; n++){
//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
mPb_theta = func_mPb_theta(img, theta, r, alpha, s); // calculate the mPb_theta
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
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
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
for(unsigned long i = 0; i < N; i++){
float pixel = ptr[i+idx]; //get the ith pixel in nth mPb_theta
if(pixel > mPb.data()[i]){ //get the maximum value among all mPb_theta for ith pixel
mPb.data()[i] = pixel;
}
else{
}
}
//ss.str(""); //(optional) clear the space for stream
}
stim::cpu2image(mPb.data(), "data_output/mPb_500_0914_neat.bmp", w, h, stim::cmBrewer); // output the mPb
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
return mPb;
}