Ejemplo n.º 1
0
 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
Ejemplo n.º 2
0
 def transformImage(self,im):
     ''' Transform an image. '''
     if isinstance(self.matrix,cv.cvmat):
         matrix = self.matrix
     else:
         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)                    
     return pv.Image(dst)
Ejemplo n.º 3
0
 def test_MatConvertNumpyToOpenCV(self):
     r, c = 10, 20
     mat = np.zeros((r, c), dtype=np.float32)
     for i in range(r):
         for j in range(c):
             mat[i, j] = i * j
     cvmat = pv.NumpyToOpenCV(mat)
     self.assert_(mat.shape == (r, c))
     for i in range(r):
         for j in range(c):
             self.assert_(mat[i, j] == cvmat[i, j])
Ejemplo n.º 4
0
    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