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

Source Code for Module pyvision.point.DetectorCorner

  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  # TODO: document module. 
 35   
 36  import unittest 
 37  import os.path   
 38   
 39  from numpy import array,ones,zeros,nonzero 
 40  from scipy.ndimage import convolve 
 41  from scipy.ndimage import gaussian_filter,maximum_filter 
 42  # TODO: At some point it would be nice to have the options of prewitt or sobel as filters. 
 43  # from scipy.ndimage import prewitt,sobel 
 44  #from numpy.linalg import det 
 45   
 46  import pyvision as pv 
 47  from pyvision.point.DetectorROI import DetectorROI 
 48  #from pyvision.types.img import Image 
 49  #from pyvision.types.Point import Point 
 50   
 51   
 52  conv2 = convolve 
 53   
54 -class DetectorCorner(DetectorROI):
55 - def __init__(self,mask = [[-1,0,1]], radius=9, sigma=0.7, k=0.04, **kwargs):
56 ''' 57 Corner Detector 58 59 mask - first dirivitive filter 60 radius - radius of the max filter 61 sigma - sigma of the smoothing gaussian. 62 k - not sure what this parameter means. 63 64 Passed to superclass: 65 n - is the approximate number of points requested. 66 bin_size - the width and height of each bin in pixels. 67 corner_selector ('all', 'bins', or 'best') - stratagy for point selection. 68 69 When corner_selector is set to bins, the image is subdivided in to bins of 70 size <bin_size>X<bin_size> pixels and an equal number of points will be taken 71 from each of those bins. This insures that points are found in all parts of the 72 image not just where the corners are strongest. 73 74 This code is based on a function originally written for matlab. 75 76 Original matlab code by: 77 Jingyu Yan and Marc Pollefeys 78 Department of Computer Science 79 University of North Carolina at Chapel Hill 80 81 Converted to Python by: 82 David Bolme 83 Department of Computer Science 84 Colorado State Univerisity 85 ''' 86 DetectorROI.__init__(self,**kwargs) 87 88 self.mask = mask 89 self.radius = radius 90 self.sigma = sigma 91 self.k = k
92
93 - def _detect(self,image):
94 # Asssumes a two dimensional array 95 A = None 96 if isinstance(image,pv.Image): 97 A = image.asMatrix2D() 98 elif isinstance(image,array) and len(image.shape)==2: 99 A = image 100 else: 101 raise TypeError("ERROR Unknown Type (%s) - Only arrays and pyvision images supported."%type(image)) 102 103 mask = array(self.mask) 104 assert len(mask.shape) == 2 105 106 #feature window calculation 107 del_A_1 = conv2(A,mask) 108 del_A_2 = conv2(A,mask.transpose()) 109 110 111 del_A_1_1 = del_A_1 * del_A_1 112 matrix_1_1 = gaussian_filter(del_A_1_1, self.sigma) 113 del del_A_1_1 114 115 del_A_2_2 = del_A_2 * del_A_2 116 matrix_2_2 = gaussian_filter(del_A_2_2, self.sigma) 117 del del_A_2_2 118 119 del_A_1_2 = del_A_1 * del_A_2 120 matrix_1_2 = gaussian_filter(del_A_1_2, self.sigma) 121 del del_A_1_2 122 123 del del_A_1,del_A_2 124 125 dM = matrix_1_1*matrix_2_2 - matrix_1_2*matrix_1_2 126 tM = matrix_1_1+matrix_2_2 127 128 del matrix_1_1 , matrix_1_2, matrix_2_2 129 130 R = dM-self.k*pow(tM,2) 131 132 footprint = ones((self.radius,self.radius)) 133 mx = maximum_filter(R, footprint = footprint) 134 local_maxima = (R == mx) * (R != zeros(R.shape)) # make sure to remove completly dark points 135 del mx 136 137 points = nonzero(local_maxima) 138 del local_maxima 139 140 points = array([points[0],points[1]]).transpose() 141 L = [] 142 for each in points: 143 L.append((R[each[0],each[1]],each[0],each[1],None)) 144 145 del R 146 147 return L
148 149 150
151 -class _CornerTest(unittest.TestCase):
152 - def setUp(self):
153 self.SHOW_IMAGES = False
154 155
156 - def testDetectorCorner1(self):
157 detector = DetectorCorner() 158 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_1.jpg') 159 im = pv.Image(filename,bw_annotate=True) 160 161 points = detector.detect(im) 162 for _,pt,_ in points: 163 im.annotatePoint(pt) 164 165 if self.SHOW_IMAGES: im.show() 166 self.assertEquals(len(points),390)
167
168 - def testDetectorCorner2(self):
169 detector = DetectorCorner() 170 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_19.jpg') 171 im = pv.Image(filename,bw_annotate=True) 172 173 points = detector.detect(im) 174 for _,pt,_ in points: 175 im.annotatePoint(pt) 176 177 if self.SHOW_IMAGES: im.show() 178 self.assertEquals(len(points),390)
179
180 - def testDetectorCorner3(self):
181 detector = DetectorCorner() 182 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_22.jpg') 183 im = pv.Image(filename,bw_annotate=True) 184 185 points = detector.detect(im) 186 for _,pt,_ in points: 187 im.annotatePoint(pt) 188 189 if self.SHOW_IMAGES: im.show() 190 self.assertEquals(len(points),390)
191
192 - def testDetectorCorner4(self):
193 detector = DetectorCorner() 194 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_37.jpg') 195 im = pv.Image(filename,bw_annotate=True) 196 197 points = detector.detect(im) 198 for _,pt,_ in points: 199 im.annotatePoint(pt) 200 201 if self.SHOW_IMAGES: im.show() 202 self.assertEquals(len(points),351)
203
204 - def testDetectorCorner5(self):
205 detector = DetectorCorner(selector='best') 206 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_37.jpg') 207 im = pv.Image(filename,bw_annotate=True) 208 209 points = detector.detect(im) 210 for _,pt,_ in points: 211 im.annotatePoint(pt) 212 213 if self.SHOW_IMAGES: im.show() 214 self.assertEquals(len(points),250)
215
216 - def testDetectorCorner6(self):
217 detector = DetectorCorner(selector='all') 218 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_37.jpg') 219 im = pv.Image(filename,bw_annotate=True) 220 221 points = detector.detect(im) 222 for _,pt,_ in points: 223 im.annotatePoint(pt) 224 225 if self.SHOW_IMAGES: im.show() 226 self.assertEquals(len(points),2143)
227