Package pyvision :: Package point :: Module DetectorROI
[hide private]
[frames] | no frames]

Source Code for Module pyvision.point.DetectorROI

  1  # PyVision License 
  2  # 
  3  # Copyright (c) 2006-2008 David S. Bolme 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  #  
 10  # 1. Redistributions of source code must retain the above copyright 
 11  # notice, this list of conditions and the following disclaimer. 
 12  #  
 13  # 2. Redistributions in binary form must reproduce the above copyright 
 14  # notice, this list of conditions and the following disclaimer in the 
 15  # documentation and/or other materials provided with the distribution. 
 16  #  
 17  # 3. Neither name of copyright holders nor the names of its contributors 
 18  # may be used to endorse or promote products derived from this software 
 19  # without specific prior written permission. 
 20  #  
 21  #  
 22  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 23  # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 24  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 25  # A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR 
 26  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 27  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 28  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 29  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 30  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 31  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 32  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 33   
 34  import pyvision as pv 
 35  import numpy as np 
 36   
37 -class DetectorROI:
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 # TODO: Call subclass 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 #print each 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: # TODO: assume all 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 #L = concatenate((L.transpose,ones((1,L.shape[0])))) 107 return roi
108
109 - def _detect(self):
110 raise NotImplementedError("This method should be overridden in a sub class.")
111