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

Source Code for Module pyvision.point.DetectorHarris

  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   
 35  import unittest 
 36  import os.path 
 37   
 38  import numpy as np 
 39   
 40  import cv 
 41  import scipy.ndimage as ndi 
 42  import pyvision as pv 
 43  from pyvision.point.DetectorROI import DetectorROI 
 44   
 45   
46 -class DetectorHarris(DetectorROI):
47 - def __init__(self,block_size = 7, aperture_size=3, k=0.04, **kwargs):
48 pass 49 DetectorROI.__init__(self,**kwargs) 50 51 self.block_size = block_size 52 self.aperture_size = aperture_size 53 self.k = k
54 55
56 - def _detect(self,im):
57 ''' 58 void cvCornerHarris( const CvArr* image, CvArr* harris_responce, int block_size, int aperture_size=3, double k=0.04 ); 59 ''' 60 gray = im.asOpenCVBW() 61 #gray = opencv.cvCreateImage( opencv.cvGetSize(cvim), 8, 1 ); 62 corners = cv.CreateImage( cv.GetSize(gray), 32, 1 ); 63 #opencv.cvCvtColor( cvim, gray, opencv.CV_BGR2GRAY ); 64 65 cv.CornerHarris(gray,corners,self.block_size,self.aperture_size,self.k) 66 67 data_buffer = corners.tostring() 68 corners = np.frombuffer(data_buffer,np.float32).reshape(corners.height,corners.width).transpose() 69 70 footprint = np.ones((3,3)) 71 mx = ndi.maximum_filter(corners, footprint = footprint) 72 local_maxima = (corners == mx) * (corners != np.zeros(corners.shape)) # make sure to remove completly dark points 73 74 points = np.nonzero(local_maxima) 75 del local_maxima 76 77 points = np.array([points[0],points[1]]).transpose() 78 L = [] 79 for each in points: 80 L.append((corners[each[0],each[1]],each[0],each[1],None)) 81 82 return L
83 84
85 -class _HarrisTest(unittest.TestCase):
86 - def setUp(self):
87 self.SHOW_IMAGES = False
88 89 90
91 - def testDetectorHarris1(self):
92 detector = DetectorHarris() 93 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_1.jpg') 94 im = pv.Image(filename,bw_annotate=True) 95 96 points = detector.detect(im) 97 for _,pt,_ in points: 98 im.annotatePoint(pt) 99 100 if self.SHOW_IMAGES: im.show() 101 self.assertEquals(len(points),390)
102
103 - def testDetectorHarris2(self):
104 detector = DetectorHarris() 105 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_19.jpg') 106 im = pv.Image(filename,bw_annotate=True) 107 108 points = detector.detect(im) 109 for _,pt,_ in points: 110 im.annotatePoint(pt) 111 112 if self.SHOW_IMAGES: im.show() 113 self.assertEquals(len(points),390)
114
115 - def testDetectorHarris3(self):
116 detector = DetectorHarris() 117 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_22.jpg') 118 im = pv.Image(filename,bw_annotate=True) 119 120 points = detector.detect(im) 121 for _,pt,_ in points: 122 im.annotatePoint(pt) 123 124 if self.SHOW_IMAGES: im.show() 125 self.assertEquals(len(points),390)
126
127 - def testDetectorHarris4(self):
128 detector = DetectorHarris() 129 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_37.jpg') 130 im = pv.Image(filename,bw_annotate=True) 131 132 points = detector.detect(im) 133 for _,pt,_ in points: 134 im.annotatePoint(pt) 135 136 if self.SHOW_IMAGES: im.show() 137 self.assertEquals(len(points),351)
138
139 - def testDetectorHarris5(self):
140 detector = DetectorHarris(selector='best') 141 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_37.jpg') 142 im = pv.Image(filename,bw_annotate=True) 143 144 points = detector.detect(im) 145 for _,pt,_ in points: 146 im.annotatePoint(pt) 147 148 if self.SHOW_IMAGES: im.show() 149 self.assertEquals(len(points),250)
150
151 - def testDetectorHarris6(self):
152 detector = DetectorHarris(selector='all') 153 filename = os.path.join(pv.__path__[0],'data','nonface','NONFACE_37.jpg') 154 im = pv.Image(filename,bw_annotate=True) 155 156 points = detector.detect(im) 157 for _,pt,_ in points: 158 im.annotatePoint(pt) 159 160 161 if self.SHOW_IMAGES: im.show() 162 self.assertEquals(len(points),6772)
163