1 import numpy as np
2 from scipy import weave
3
4
7 self.max_length = max_length
8 self.maxes = np.zeros((max_length,2),dtype=np.int)
9 self.vals = np.zeros((max_length,),dtype=np.float)
10
11 - def __call__(self, mat, threshold = None, sort_results = True):
12 '''
13 All any local maximum that are greater than threshhold up to a total of
14 max_length.
15
16 To save time arrays that hold the maxes and vals that are created
17 once and reused for each call. This means that local maximum detection
18 is not thread safe. If using this class with threads create an instance
19 for each thread.
20
21 @param mat: 2d Real Matrix input.
22 @param threshold: Mininum value of local maxima.
23 @param sort_results: set to False to save time and return an unorderd list.
24
25 @returns: maxes,vals
26 '''
27 maxes = self.maxes
28 vals = self.vals
29 r,c = mat.shape
30 max_length = self.max_length
31
32 if threshold != None:
33 count = weave.inline(
34 '''
35 int count = 0;
36
37 for( int i = 1; i < r-1 ; i++){
38 for(int j = 1; j < c-1 ; j++){
39 // Check if the current location meets the threshold
40
41 if (mat(i,j) > threshold &&
42 mat(i,j) > mat(i,j-1) &&
43 mat(i,j) > mat(i,j+1) &&
44 mat(i,j) > mat(i-1,j-1) &&
45 mat(i,j) > mat(i-1,j) &&
46 mat(i,j) > mat(i-1,j+1) &&
47 mat(i,j) > mat(i+1,j-1) &&
48 mat(i,j) > mat(i+1,j) &&
49 mat(i,j) > mat(i+1,j+1)){
50
51 // This is a local max
52 maxes(count,0) = i;
53 maxes(count,1) = j;
54 vals(count) = mat(i,j);
55 count += 1;
56
57 if(count == max_length){
58 i = r;
59 j = c;
60 }
61 }
62 }
63 }
64
65 return_val = count;
66 ''',
67 arg_names=['mat','maxes','vals','max_length','threshold','r','c'],
68 type_converters=weave.converters.blitz,
69 )
70 else:
71 count = weave.inline(
72 '''
73 int count = 0;
74
75 for( int i = 1; i < r-1 ; i++){
76 for(int j = 1; j < c-1 ; j++){
77 // Check if the current location meets the threshold
78
79 if (mat(i,j) > mat(i,j-1) &&
80 mat(i,j) > mat(i,j+1) &&
81 mat(i,j) > mat(i-1,j-1) &&
82 mat(i,j) > mat(i-1,j) &&
83 mat(i,j) > mat(i-1,j+1) &&
84 mat(i,j) > mat(i+1,j-1) &&
85 mat(i,j) > mat(i+1,j) &&
86 mat(i,j) > mat(i+1,j+1)){
87
88 // This is a local max
89 maxes(count,0) = i;
90 maxes(count,1) = j;
91 vals(count) = mat(i,j);
92 count += 1;
93
94 if(count == max_length){
95 i = r;
96 j = c;
97 }
98 }
99 }
100 }
101
102 return_val = count;
103 ''',
104 arg_names=['mat','maxes','vals','max_length','r','c'],
105 type_converters=weave.converters.blitz,
106 )
107
108 if sort_results == False:
109 return maxes[:count,:].copy(),vals[:count].copy()
110
111 order = np.argsort(vals[:count])[::-1]
112 maxes = maxes[order]
113 vals = vals[order]
114
115
116
117
118 return maxes,vals
119