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
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
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
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
62 corners = cv.CreateImage( cv.GetSize(gray), 32, 1 );
63
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))
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
87 self.SHOW_IMAGES = False
88
89
90
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
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
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
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
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
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