def asHomography(self,forward=True): ''' Get the transformation as a homography. @keyword forward: switch between the forward and reverse transform. @ktype forward: True|False ''' fw,fh = self.frame_size tw,th = self.frame_size if forward: if self.homography == None: matrix = np.eye(3) else: matrix = np.linalg.inv(pv.OpenCVToNumpy(self.homography)) matrix = np.dot(np.diag([tw/fw,th/fh,1.0]),matrix) perspective = pv.PerspectiveTransform(matrix,self.frame_size) else: if self.homography_rev == None: matrix = np.eye(3) else: matrix = np.linalg.inv(pv.OpenCVToNumpy(self.homography_rev)) matrix = np.dot(np.diag([tw/fw,th/fh,1.0]),matrix) perspective = pv.PerspectiveTransform(matrix,self.frame_size) return perspective
def PerspectiveFromPoints(source, dest, new_size, method=0, ransacReprojThreshold=1.5): ''' Calls the OpenCV function: cvFindHomography. This method has additional options to use the CV_RANSAC or CV_LMEDS methods to find a robust homography. Method=0 appears to be similar to PerspectiveFromPoints. ''' assert len(source) == len(dest) n_points = len(source) s = cv.CreateMat(n_points, 2, cv.CV_32F) d = cv.CreateMat(n_points, 2, cv.CV_32F) p = cv.CreateMat(3, 3, cv.CV_32F) for i in range(n_points): s[i, 0] = source[i].X() s[i, 1] = source[i].Y() d[i, 0] = dest[i].X() d[i, 1] = dest[i].Y() results = cv.FindHomography(s, d, p, method, ransacReprojThreshold) matrix = pv.OpenCVToNumpy(p) return PerspectiveTransform(matrix, new_size)
def annotateFrame(self,frame,color='white'): ''' Renders optical flow information to the frame. @param frame: the frame that will be annotated @type frame: pv.Image @keyword type: @ktype type: "TRACKING" ''' w,h = self.tile_size rect = pv.Rect(0,0,w,h) affine = pv.AffineFromRect(rect,frame.size) for pt in affine.transformPoints(self.tracks[:self.n]): frame.annotatePoint(pt,color=color) for pt in affine.transformPoints(self.tracks[self.n:]): frame.annotatePoint(pt,color='red') for pt in affine.transformPoints(self.bad_points): frame.annotatePoint(pt,color='gray') if self.homography != None: matrix = pv.OpenCVToNumpy(self.homography) perspective = pv.PerspectiveTransform(matrix,frame.size) for pt in self.tracks[:self.n]: # Transform the point multiple times to amplify the track # for visualization old = perspective.invertPoint(pt) old = perspective.invertPoint(old) old = perspective.invertPoint(old) frame.annotateLine(pt,old,color=color)
def _preprocess(self,image_tile): ''' preprocess an image tile. ''' # TODO: This function has problems in opencv 2.2. There appears to be a bug. image_tile = pv.OpenCVToNumpy(image_tile) self.image = pv.NumpyToOpenCV(np.log(image_tile + 1.0).astype(np.float32)) return self.image
def locateEyes(self, im, face_rects, ilog=None): ''' Finds the eyes in the image. @param im: full sized image @param face_rects: list of rectangle which are the output from the cascade face detector. ''' cvim = im.asOpenCVBW() faces = [] for rect in face_rects: faceim = cv.GetSubRect(cvim, rect.asOpenCV()) cv.Resize(faceim, self.bwtile) affine = pv.AffineFromRect(rect, (128, 128)) #cv.cvCvtColor( self.cvtile, self.bwtile, cv.CV_BGR2GRAY ) leye, reye, lcp, rcp = self.fel.locateEyes(self.bwtile) le = pv.Point(leye) re = pv.Point(reye) leye = affine.invertPoint(le) reye = affine.invertPoint(re) faces.append([rect, leye, reye]) if ilog != None: ilog.log(pv.Image(self.bwtile), label="FaceDetection") lcp = pv.OpenCVToNumpy(lcp).transpose() lcp = lcp * (lcp > 0.0) rcp = pv.OpenCVToNumpy(rcp).transpose() rcp = rcp * (rcp > 0.0) ilog.log(pv.Image(lcp), label="Left_Corr") ilog.log(pv.Image(rcp), label="Right_Corr") tmp = pv.Image(self.bwtile) tmp.annotatePoint(le) tmp.annotatePoint(re) ilog.log(tmp, "EyeLocations") return faces
def test_MatConvertOpenCVToNumpy(self): r, c = 10, 20 cvmat = cv.CreateMat(r, c, cv.CV_32F) for i in range(r): for j in range(c): cvmat[i, j] = i * j mat = pv.OpenCVToNumpy(cvmat) self.assert_(mat.shape == (r, c)) for i in range(r): for j in range(c): self.assert_(mat[i, j] == cvmat[i, j])
def loadFilterEyeLocator(filename, ilog=None): ''' Loads the eye locator from a file.' ''' # open the file f = open(filename, 'rb') # Check the first line line = f.readline().strip() assert line == "CFEL" # read past the comment and copyright. f.readline() f.readline() # get the width and the height r, c = f.readline().split() r, c = int(r), int(c) # read in the left bounding rectangle x, y, w, h = f.readline().split() left_rect = (int(x), int(y), int(w), int(h)) # read in the right bounding rectangle x, y, w, h = f.readline().split() right_rect = (int(x), int(y), int(w), int(h)) # read the magic number magic_number = f.readline().strip() assert len(magic_number) == 4 magic_number = struct.unpack('i', magic_number)[0] # Read in the filter data lf = array.array('f') rf = array.array('f') lf.fromfile(f, r * c) rf.fromfile(f, r * c) # Test the magic number and byteswap if necessary. if magic_number == 0x41424344: pass elif magic_number == 0x44434241: lf.byteswap() rf.byteswap() else: raise ValueError("Bad Magic Number: Unknown byte ordering in file") # Create the left and right filters left_filter = cv.CreateMat(r, c, cv.CV_32F) right_filter = cv.CreateMat(r, c, cv.CV_32F) # Copy data into the left and right filters cv.SetData(left_filter, lf.tostring()) cv.SetData(right_filter, rf.tostring()) tmp = pv.OpenCVToNumpy(left_filter) t1 = tmp.mean() t2 = tmp.std() cv.Scale(left_filter, left_filter, 1.0 / t2, -t1 * 1.0 / t2) tmp = pv.OpenCVToNumpy(right_filter) t1 = tmp.mean() t2 = tmp.std() cv.Scale(right_filter, right_filter, 1.0 / t2, -t1 * 1.0 / t2) #tmp = pv.OpenCVToNumpy(left_filter) #print tmp.mean(),tmp.std() if ilog != None: #lf = cv.cvCreateMat(r,c,cv.CV_8U) #rf = cv.cvCreateMat(r,c,cv.CV_8U) lf = pv.OpenCVToNumpy(left_filter) rf = pv.OpenCVToNumpy(right_filter) lf = np.fft.fftshift(lf).transpose() rf = np.fft.fftshift(rf).transpose() ilog.log(pv.Image(lf), label="LeftEyeFilter") ilog.log(pv.Image(rf), label="RightEyeFilter") # Return the eye locator return OpenCVFilterEyeLocator(left_filter, right_filter, left_rect, right_rect)