Exemplo n.º 1
0
 def apply( self, img, target_header=None,
            interpolation='linear', gaussian_parameter=1.0 ):
     """
     Apply.
     """
     if not isinstance( img, irtk.Image ):
         # point transformation
         pt = np.array(img, dtype='float64', copy=True)
         if len(pt.shape) == 1:
             tmp_pt = np.hstack((pt,[1])).astype('float64')
             return np.dot( self.matrix(), tmp_pt )[:3]
         else:
             pt = _irtk.transform_points( self.matrix(), pt )
             return pt  
             # tmp_pt = np.hstack((pt,[[1]]*pt.shape[0])).astype('float64')
             # return np.transpose( np.dot( self.matrix(),
             #                              np.transpose(tmp_pt) ) )[:,:3]
     
     # if target_header is None:
     #     target_header = img.get_header()
     if target_header is None:
         (x_min, y_min, z_min, x_max, y_max, z_max ) = img.bbox(world=True)
         corners = [[x_min, y_min, z_min],
                    [x_max, y_min, z_min],
                    [x_min, y_max, z_min],
                    [x_min, y_min, z_max],
                    [x_max, y_max, z_min],
                    [x_min, y_max, z_max],
                    [x_max, y_min, z_max],
                    [x_max, y_max, z_max]]
         corners = self.apply( corners )
         x_min, y_min, z_min = corners.min(axis=0)
         x_max, y_max, z_max = corners.max(axis=0)
         res = img.header['pixelSize'][0]
         pixelSize = [res, res, res, 1]
         origin = [ x_min + (x_max+1 - x_min)/2,
                    y_min + (y_max+1 - y_min)/2,
                    z_min + (z_max+1 - z_min)/2,
                    img.header['origin'][3] ]
         dim = [ (x_max+1 - x_min)/res,
                 (y_max+1 - y_min)/res,
                 (z_max+1 - z_min)/res,
                 1 ]
         target_header = irtk.new_header( pixelSize=pixelSize, origin=origin, dim=dim)
     if isinstance( target_header, irtk.Image ):
         target_header = target_header.get_header()
     data = img.get_data('float32','cython')
     new_data = _irtk.transform_rigid( self.tx, self.ty, self.tz,
                                       self.rx, self.ry, self.rz,
                                       data,
                                       img.get_header(),
                                       target_header,
                                       interpolation,
                                       gaussian_parameter )
     return irtk.Image( new_data, target_header )
Exemplo n.º 2
0
 def apply( self, img, target_header=None,
            interpolation='linear', gaussian_parameter=1.0 ):
     """
     Apply.
     """
     if not isinstance( img, irtk.Image ):
         # point transformation
         pt = np.array(img, dtype='float64', copy=True)
         if len(pt.shape) == 1:
             tmp_pt = np.hstack((pt,[1])).astype('float64')
             return np.dot( self.matrix(), tmp_pt )[:3]
         else:
             pt = _irtk.transform_points( self.matrix(), pt )
             return pt  
             # tmp_pt = np.hstack((pt,[[1]]*pt.shape[0])).astype('float64')
             # return np.transpose( np.dot( self.matrix(),
             #                              np.transpose(tmp_pt) ) )[:,:3]
     
     # if target_header is None:
     #     target_header = img.get_header()
     if target_header is None:
         (x_min, y_min, z_min, x_max, y_max, z_max ) = img.bbox(world=True)
         corners = [[x_min, y_min, z_min],
                    [x_max, y_min, z_min],
                    [x_min, y_max, z_min],
                    [x_min, y_min, z_max],
                    [x_max, y_max, z_min],
                    [x_min, y_max, z_max],
                    [x_max, y_min, z_max],
                    [x_max, y_max, z_max]]
         corners = self.apply( corners )
         x_min, y_min, z_min = corners.min(axis=0)
         x_max, y_max, z_max = corners.max(axis=0)
         res = img.header['pixelSize'][0]
         pixelSize = [res, res, res, 1]
         origin = [ x_min + (x_max+1 - x_min)/2,
                    y_min + (y_max+1 - y_min)/2,
                    z_min + (z_max+1 - z_min)/2,
                    img.header['origin'][3] ]
         dim = [ (x_max+1 - x_min)/res,
                 (y_max+1 - y_min)/res,
                 (z_max+1 - z_min)/res,
                 1 ]
         target_header = irtk.new_header( pixelSize=pixelSize, origin=origin, dim=dim)
     if isinstance( target_header, irtk.Image ):
         target_header = target_header.get_header()
     data = img.get_data('float32','cython')
     new_data = _irtk.transform_rigid( self.tx, self.ty, self.tz,
                                       self.rx, self.ry, self.rz,
                                       data,
                                       img.get_header(),
                                       target_header,
                                       interpolation,
                                       gaussian_parameter )
     return irtk.Image( new_data, target_header )
Exemplo n.º 3
0
 def WorldToImage( self, pt ):
     """
     Returns integers
     """
     tmp_pt = np.array( pt, dtype='float64' ).copy()
     if len(tmp_pt.shape) == 1:
         tmp_pt = np.hstack((tmp_pt,[1])).astype('float64')
         return np.rint( np.dot( self.W2I, tmp_pt )[:3] ).astype('int32')
     else:
         tmp_pt = np.rint( _irtk.transform_points( self.W2I, tmp_pt ) ).astype('int32')
         return tmp_pt
Exemplo n.º 4
0
 def WorldToImage(self, pt):
     """
     Returns integers
     """
     tmp_pt = np.array(pt, dtype='float64').copy()
     if len(tmp_pt.shape) == 1:
         tmp_pt = np.hstack((tmp_pt, [1])).astype('float64')
         return np.rint(np.dot(self.W2I, tmp_pt)[:3]).astype('int32')
     else:
         tmp_pt = np.rint(_irtk.transform_points(self.W2I,
                                                 tmp_pt)).astype('int32')
         return tmp_pt
Exemplo n.º 5
0
 def WorldToImage(self, pt):
     """
     Returns integers
     """
     # make sure I2W an W2I are up-to-date
     self.__updateMatrix()
     tmp_pt = np.array(pt, dtype='float64').copy()
     if len(tmp_pt.shape) == 1:
         tmp_pt = np.hstack((tmp_pt, [1])).astype('float64')
         return np.rint(np.dot(self.W2I, tmp_pt)[:3]).astype('int32')
     else:
         tmp_pt = np.rint(_irtk.transform_points(self.W2I,
                                                 tmp_pt)).astype('int32')
         return tmp_pt
Exemplo n.º 6
0
    def ImageToWorld(self, pt):
        """
        BEWARE: images are indexed as img[t,z,y,x],
        so for instance:

        Examples
        --------
        >>> tx,ty,tz = img.ImageToWorld( [(img.shape[2]-1)/2,(img.shape[1]-1)/2,(img.shape[0]-1)/2] )
        """
        tmp_pt = np.array(pt, dtype='float64').copy()
        if len(tmp_pt.shape) == 1:
            tmp_pt = np.hstack((tmp_pt, [1])).astype('float64')
            return np.dot(self.I2W, tmp_pt)[:3]
        else:
            tmp_pt = _irtk.transform_points(self.I2W, tmp_pt)
            return tmp_pt
Exemplo n.º 7
0
    def ImageToWorld( self, pt ):
        """
        BEWARE: images are indexed as img[t,z,y,x],
        so for instance:

        Examples
        --------
        >>> tx,ty,tz = img.ImageToWorld( [(img.shape[2]-1)/2,(img.shape[1]-1)/2,(img.shape[0]-1)/2] )
        """
        tmp_pt = np.array( pt, dtype='float64' ).copy()
        if len(tmp_pt.shape) == 1:
            tmp_pt = np.hstack((tmp_pt,[1])).astype('float64')
            return np.dot( self.I2W, tmp_pt )[:3]
        else:
            tmp_pt = _irtk.transform_points( self.I2W, tmp_pt )
            return tmp_pt