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 import pyvision as pv
35 import numpy as np
36
38 '''
39 This class defines an interface to a Region Of Interest (ROI) detector.
40 '''
41 - def __init__(self,n=250,selector='bins',bin_size=50):
42 '''
43 n - is the approximate number of points requested.
44 bin_size - the width and height of each bin in pixels.
45 selector - ('all', 'bins', or 'best') stratagy for point selection.
46
47 When corner_selector is set to bins, the image is subdivided in to bins of
48 size <bin_size>X<bin_size> pixels and an equal number of points will be taken
49 from each of those bins. This insures that points are found in all parts of the
50 image not just where the corners are strongest.
51 '''
52
53 self.n = n
54 self.selector = selector
55 self.bin_size = bin_size
56 pass
57
58 - def detect(self,image,**kwargs):
59 '''
60 Returns a list of region of interest. Each element in the list is a
61 tuple of (score,centerpoint,radius). Radius of "None" is used for point
62 detectors. Higher scores are better and scores of "None" indicate no
63 score is avalible.
64 '''
65
66 A = None
67 if isinstance(image,pv.Image):
68 A = image.asMatrix2D()
69 elif isinstance(image,np.array) and len(image.shape)==2:
70 A = image
71 else:
72 raise TypeError("ERROR Unknown Type (%s) - Only arrays and pyvision images supported."%type(image))
73
74 L = self._detect(image,**kwargs)
75
76 L.sort()
77 L.reverse()
78
79 if self.selector == 'best':
80 L=L[:self.n]
81 elif self.selector == 'bins':
82 nbins = A.shape[0]/self.bin_size*A.shape[1]/self.bin_size
83 npts = self.n / nbins + 1
84
85 corners = []
86 for xmin in range(0,A.shape[0],self.bin_size):
87 xmax = xmin + self.bin_size
88 for ymin in range(0,A.shape[1],self.bin_size):
89 bin_data = []
90 ymax = ymin + self.bin_size
91 for each in L:
92
93 if xmin <= each[1] and each[1] < xmax and ymin <= each[2] and each[2] < ymax:
94 bin_data.append(each)
95 if len(bin_data) >= npts:
96 break
97 corners += bin_data
98 L = corners
99 else:
100 pass
101
102 roi = []
103 for each in L:
104 roi.append([each[0],pv.Point(each[1],each[2]),each[3]])
105
106
107 return roi
108
110 raise NotImplementedError("This method should be overridden in a sub class.")
111