def transformImage(self,im, use_orig=True, inverse=False): ''' Transforms an image into the new coordinate system. If this image was produced via an affine transform of another image, this method will attempt to trace weak references to the original image and directly compute the new image from that image to improve accuracy. To accomplish this a weak reference to the original source image and the affine matrix used for the transform are added to any image produced by this method. This can be disabled using the use_orig parameter. @param im: an Image object @param use_orig: (True or False) attempts to find and use the original image as the source to avoid an accumulation of errors. @returns: the transformed image ''' #TODO: does not support opencv images. see Perspective.py prev_im = im if inverse: inverse = self.matrix else: inverse = self.inverse if use_orig: # Find the oldest image used to produce this one by following week # references. # Check to see if there is an aff_prev list if hasattr(prev_im,'aff_prev'): # If there is... search that list for the oldest image found_prev = False for i in range(len(prev_im.aff_prev)): ref,cmat = prev_im.aff_prev[i] if not found_prev and ref(): im = ref() mat = np.eye(3) found_prev = True if found_prev: mat = np.dot(mat,cmat) if found_prev: inverse = np.dot(mat,inverse) if im.getType() == TYPE_PIL: data = inverse[:2,:].flatten() #data = (matrix[0,0],matrix[0,1],matrix[0,2],matrix[1,0],matrix[1,1],matrix[1,2]) pil = im.asPIL().transform(self.size, AFFINE, data, self.filter) result = Image(pil) elif im.getType() == TYPE_MATRIX_2D: mat = im.asMatrix2D() mat = affine_transform(mat, self.inverse[:2,:2], offset=self.inverse[:2,2]) result = Image(mat) elif im.getType() == TYPE_OPENCV: matrix = pv.NumpyToOpenCV(self.matrix) src = im.asOpenCV() dst = cv.CreateImage( (self.size[0],self.size[1]), cv.IPL_DEPTH_8U, src.nChannels ); cv.WarpPerspective( src, dst, matrix, cv.CV_INTER_LINEAR+cv.CV_WARP_FILL_OUTLIERS,cv.ScalarAll(128)) result = pv.Image(dst) else: raise NotImplementedError("Unhandled image type for affine transform.") # Check to see if there is an aff_prev list for this object if use_orig and hasattr(prev_im,'aff_prev'): # Create one if not result.aff_prev = copy.copy(prev_im.aff_prev) else: result.aff_prev = [] # Append the prev image and new transform result.aff_prev.append( (weakref.ref(prev_im), self.inverse) ) return result
def transformImage(self,im_a, use_orig=True, inverse=False): ''' Transforms an image into the new coordinate system. If this image was produced via an affine transform of another image, this method will attempt to trace weak references to the original image and directly compute the new image from that image to improve accuracy. To accomplish this a weak reference to the original source image and the affine matrix used for the transform are added to any image produced by this method. This can be disabled using the use_orig parameter. @param im_a: an Image object @param use_orig: (True or False) attempts to find and use the original image as the source to avoid an accumulation of errors. @returns: the transformed image ''' #TODO: does not support opencv images. see Perspective.py prev_im = im_a if inverse: inverse = self.matrix else: inverse = self.inverse if use_orig: # Find the oldest image used to produce this one by following week # references. # Check to see if there is an aff_prev list if hasattr(prev_im,'aff_prev'): # If there is... search that list for the oldest image found_prev = False for i in range(len(prev_im.aff_prev)): ref,cmat = prev_im.aff_prev[i] if not found_prev and ref(): im_a = ref() mat = np.eye(3) found_prev = True if found_prev: mat = np.dot(mat,cmat) if found_prev: inverse = np.dot(mat,inverse) if im_a.getType() == TYPE_PIL: data = inverse[:2,:].flatten() #data = (matrix[0,0],matrix[0,1],matrix[0,2],matrix[1,0],matrix[1,1],matrix[1,2]) pil = im_a.asPIL().transform(self.size, AFFINE, data, self.interpolate) result = Image(pil) elif im_a.getType() == TYPE_MATRIX_2D: # Transform a matrix 2d mat = im_a.asMatrix2D() mat = affine_transform(mat, self.inverse[:2,:2], offset=self.inverse[:2,2]) result = Image(mat[:self.size[0],:self.size[1]]) elif im_a.getType() == TYPE_MATRIX_RGB: # Transform a matrix 3d mat = im_a.asMatrix3D() c0 = mat[0,:,:] c1 = mat[1,:,:] c2 = mat[2,:,:] c0 = affine_transform(c0, self.inverse[:2,:2], offset=self.inverse[:2,2]) c1 = affine_transform(c1, self.inverse[:2,:2], offset=self.inverse[:2,2]) c2 = affine_transform(c2, self.inverse[:2,:2], offset=self.inverse[:2,2]) mat = np.array([c0,c1,c2],dtype=np.float32) result = Image(mat[:,:self.size[0],:self.size[1]]) elif im_a.getType() == TYPE_OPENCV2: # Transform an opencv 2 image src = im_a.asOpenCV2() dst = cv2.warpPerspective(src, self.matrix, self.size) result = pv.Image(dst) elif im_a.getType() == TYPE_OPENCV2BW: # Transform a bw opencv 2 image src = im_a.asOpenCV2BW() dst = cv2.warpPerspective(src, self.matrix, self.size) result = pv.Image(dst) else: raise NotImplementedError("Unhandled image type for affine transform.") # Check to see if there is an aff_prev list for this object if use_orig and hasattr(prev_im,'aff_prev'): # Create one if not result.aff_prev = copy.copy(prev_im.aff_prev) else: result.aff_prev = [] # Append the prev image and new transform result.aff_prev.append( (weakref.ref(prev_im), self.inverse) ) return result