Example #1
0
def f(x, datareg, rot):
    # x : input parameters. If x.shape[0] = 6, optimization is performed for rigid transforms. If x.shape[0]=3, optimization is performed only for translation
    # datareg : container containing all required information for minimization
    # rot : input array that may contain list of rotation values (in this case, only translation is estimated)

    if x.shape[0] == 6:
        realParameter = (
            x - datareg.Kcte
        ) / datareg.K  # x is the parameter of the transformation multiplied by datareg.K
    else:
        realParameter = np.zeros(
            6
        )  # x is the parameter of the transformation multiplied by datareg.K
        realParameter[0:3] = np.copy(x)
        realParameter[3:6] = np.copy(rot)
        realParameter = (
            realParameter - datareg.Kcte
        ) / datareg.K  # x is the parameter of the transformation multiplied by datareg.K

    Mref2in = computeMref2in(
        realParameter, datareg)  #Compute transform from refimage to inputimage

    #Apply current transform on input image and mask

    warpedarray = affine_transform(datareg.inputarray,
                                   Mref2in[0:3, 0:3],
                                   offset=Mref2in[0:3, 3],
                                   output_shape=datareg.refarray.shape,
                                   order=1,
                                   mode='constant',
                                   cval=np.nan,
                                   prefilter=False)
    warpedmask = affine_transform(datareg.inputmask,
                                  Mref2in[0:3, 0:3],
                                  offset=Mref2in[0:3, 3],
                                  output_shape=datareg.refarray.shape,
                                  order=0,
                                  mode='constant',
                                  cval=0,
                                  prefilter=False)

    #Compute similarity criterion
    index = np.multiply(~np.isnan(warpedarray), warpedmask > 0)
    index = np.multiply(datareg.refindex, index)

    refval = datareg.refarray[index]
    inval = warpedarray[index]

    res = datareg.criterium.compute(refval, inval)

    #Check if overlap between the two images is enough ... otherwise return infinity
    overlap = np.sum(index) * 100.0 / np.sum(datareg.refindex)
    if overlap < 5.0:
        res = 100000  # np.inf to remove warning during execution
    #print '-----------------'
    #print "compute"
    #print realParameter
    #print res
    #print '-----------------'
    return res
Example #2
0
def rotation_zoom3D(X, y):
    """
    Rotate a 3D image with alfa, beta and gamma degree respect the axis x, y and z respectively.
    The three angles are chosen randomly between 0-30 degrees
    """
    alpha, beta, gamma = np.random.random_sample(3) * np.pi / 2
    Rx = np.array([[1, 0, 0], [0, np.cos(alpha), -np.sin(alpha)],
                   [0, np.sin(alpha), np.cos(alpha)]])

    Ry = np.array([[np.cos(beta), 0, np.sin(beta)], [0, 1, 0],
                   [-np.sin(beta), 0, np.cos(beta)]])

    Rz = np.array([[np.cos(gamma), -np.sin(gamma), 0],
                   [np.sin(gamma), np.cos(gamma), 0], [0, 0, 1]])

    R_rot = np.dot(np.dot(Rx, Ry), Rz)

    a, b = 0.8, 1.2
    alpha, beta, gamma = (b - a) * np.random.random_sample(3) + a
    R_scale = np.array([[alpha, 0, 0], [0, beta, 0], [0, 0, gamma]])

    R = np.dot(R_rot, R_scale)
    X_rot = np.empty_like(X)
    for channel in range(X.shape[-1]):
        X_rot[:, :, :, channel] = affine_transform(X[:, :, :, channel],
                                                   R,
                                                   offset=0,
                                                   order=1,
                                                   mode='constant')
    y_rot = affine_transform(y, R, offset=0, order=0, mode='constant')

    return X_rot, y_rot
Example #3
0
def rotation3D(X, y):
    """
    Rotate a 3D image with alfa, beta and gamma degree respect the axis x, y and z respectively.
    The three angles are chosen randomly between 0-30 degrees
    """
    alpha, beta, gamma = np.random.randint(0, 31, size=3) / 180 * np.pi
    Rx = np.array([[1, 0, 0], [0, np.cos(alpha), -np.sin(alpha)],
                   [0, np.sin(alpha), np.cos(alpha)]])

    Ry = np.array([[np.cos(beta), 0, np.sin(beta)], [0, 1, 0],
                   [-np.sin(beta), 0, np.cos(beta)]])

    Rz = np.array([[np.cos(gamma), -np.sin(gamma), 0],
                   [np.sin(gamma), np.cos(gamma), 0], [0, 0, 1]])

    R = np.dot(np.dot(Rx, Ry), Rz)

    X_rot = np.empty_like(X)
    for channel in range(X.shape[-1]):
        X_rot[:, :, :, channel] = affine_transform(X[:, :, :, channel],
                                                   R,
                                                   offset=0,
                                                   order=3,
                                                   mode='constant')
    y_rot = affine_transform(y, R, offset=0, order=0, mode='constant')

    return X_rot, y_rot
Example #4
0
def rotation3D(X, y):
    # 3D random rotations between 0°-30°
    channel_y = np.shape(y)[0]
    alpha, beta, gamma = np.random.randint(0, 31, size=3) / 180 * np.pi
    Rx = np.array([[1, 0, 0], [0, np.cos(alpha), -np.sin(alpha)],
                   [0, np.sin(alpha), np.cos(alpha)]])

    Ry = np.array([[np.cos(beta), 0, np.sin(beta)], [0, 1, 0],
                   [-np.sin(beta), 0, np.cos(beta)]])

    Rz = np.array([[np.cos(gamma), -np.sin(gamma), 0],
                   [np.sin(gamma), np.cos(gamma), 0], [0, 0, 1]])

    R = np.dot(np.dot(Rx, Ry), Rz)
    X_rot = np.empty_like(X)
    y_rot = np.empty_like(y)

    for channel in range(X.shape[0]):
        X_rot[channel, :, :, :] = affine_transform(X[channel, :, :, :],
                                                   R,
                                                   offset=0,
                                                   order=3,
                                                   mode='constant')

    for i in range(channel_y):
        y_rot[i, :] = affine_transform(y[i, :],
                                       R,
                                       offset=0,
                                       order=0,
                                       mode='constant')

    return X_rot, y_rot
Example #5
0
    def sample(self, lesion):
        # We first go from subject space to train space of the VAE
        # Since we are using scipy affine transform that takes an INVERSE transformation
        # we pass to the function the inverse of subjectToTrainMat, so trainToSubjectMat
        lesionTrainSpace = affine_transform(lesion,
                                            self.trainToSubjectMat,
                                            output_shape=self.net_shape,
                                            order=1)

        # We go through the VAE to get the factorized prior
        # tensorflow wants a 5 dimensional input where the first dimension is batch number
        # and last dimension is number of channels, both set to 1 in our case.
        expandedLesion = np.expand_dims(np.expand_dims(lesionTrainSpace, 0), 4)
        lesionVAETrainSpace = np.squeeze(
            np.squeeze(
                self.sess.run(self.net,
                              {self.lesionPlaceholder: expandedLesion}), 4), 0)

        # We then go back to subject space from train space
        # Also here, since we are using scipy affine transform that takes an INVERSE transformation
        # we pass to the function the inverse of trainToSubjectMat, so subjectToTrainMat
        lesionPriorVAE = affine_transform(lesionVAETrainSpace,
                                          self.subjectToTrainMat,
                                          output_shape=self.imageSize,
                                          order=1)

        return lesionPriorVAE
Example #6
0
    def transform(self, xb, yb):
        xb, yb = super(Affine3DTransformBatchIterator,
                       self).transform(xb, yb)
        # Skip if affine_p is 0. Setting affine_p may be useful for quickly
        # disabling affine transformation
        if self.affine_p == 0:
            return xb, yb

        seed = np.random.randint(clock())
        xb_transformed = xb.copy()
        if isinstance(xb, dict):
            for k in self.input_layers:
                np.random.seed(seed)
                x_t = np.random.permutation(xb_transformed[k])
                for i in range(int(x_t.shape[0] * self.affine_p)):
                    t = random_affine3d_matrix()
                    img_transformed = affine_transform(x_t[i], t)
                    xb_transformed[k][i] = img_transformed
        else:
            np.random.seed(seed)
            xb_transformed = np.random.permutation(xb)
            for i in range(int(xb.shape[0] * self.affine_p)):
                t = random_affine3d_matrix
                img_transformed = affine_transform(xb[i], t)
                xb_transformed[i] = img_transformed

        return xb_transformed, yb
def scale_and_rotate_image(im, angle=0.0, scale=None, order=5):
    """ Scale and then rotate and image in x, y axes

    Applies scale then rotates

    :param im: Image
    :param angle: Angle in radians
    :param scale: Scale [scale_x, scale_y]
    :param order: Order of interpolation (0-5)
    :return:
    """
    from scipy.ndimage.interpolation import affine_transform

    nchan, npol, ny, nx = im.shape
    c_in = 0.5 * numpy.array([ny, nx])
    c_out = 0.5 * numpy.array([ny, nx])
    rot = numpy.array([[numpy.cos(angle), -numpy.sin(angle)],
                       [numpy.sin(angle), numpy.cos(angle)]])
    inv_rot = rot.T
    if scale is None:
        scale = [1.0, 1.0]

    newim = copy_image(im)
    inv_scale = numpy.diag(scale)
    inv_transform = numpy.dot(inv_scale, inv_rot)
    offset = c_in - numpy.dot(inv_transform, c_out)
    for chan in range(nchan):
        for pol in range(npol):
            if im.data.dtype == "complex":
                newim.data[chan, pol] = affine_transform(im.data[chan, pol].real,
                                                         inv_transform,
                                                         offset=offset,
                                                         order=order,
                                                         output_shape=(ny, nx)) + \
                                        1.0j * affine_transform(im.data[chan, pol].imag,
                                                                inv_transform,
                                                                offset=offset,
                                                                order=order,
                                                                output_shape=(ny, nx))
            elif im.data.dtype == "float":
                newim.data[chan, pol] = affine_transform(im.data[chan,
                                                                 pol].real,
                                                         inv_transform,
                                                         offset=offset,
                                                         order=order,
                                                         output_shape=(ny, nx))
            else:
                raise ValueError("Cannot process data type {}".format(
                    im.data.dtype))

    return newim
Example #8
0
def afine(img, dx, dy, sx, sy, angle):
    imat=np.identity(2)
    imat[0,0]=imat[0,0]*sx
    imat[1,1]=imat[1,1]*sy
    #print imat
    #anglemat=np.identity(2)
    img =affine_transform(img, imat)
    #return img
    a=radians(angle)
    transform=np.array([[cos(a),-sin(a)],[sin(a),cos(a)]])
    centre=0.5*np.array(img.shape)
    offset=(centre-centre.dot(transform)).dot(np.linalg.inv(transform))
    finoff = -1*offset + [dx,dy]
    return affine_transform(img, transform, finoff)
def collate(depth, tiles, outputDirectory=None, outputAccumulo=None, layer="RGB", splineOrder=3):
    """Performs the part of the reducing step of the Hadoop map-reduce job after all data have been collected.

    Actions: combine deep images to produce more shallow images.

        * depth: depth of input images; output images have (depth - 1)
        * tiles: dictionary of tiles built up by this procedure (this function adds to tiles)
        * outputDirectory: local filesystem directory for debugging output (usually the output goes to Accumulo)
        * outputAccumulo: Java virtual machine containing AccumuloInterface classes
        * layer: name of the layer
        * splineOrder: order of the spline used to calculate the affine_transformation (see SciPy docs); must be between 0 and 5
    """

    for depthIndex, longIndex, latIndex, l in tiles.keys():
        if l == layer and depthIndex == depth:
            parentDepth, parentLongIndex, parentLatIndex = tileParent(depthIndex, longIndex, latIndex)
            if (parentDepth, parentLongIndex, parentLatIndex, layer) not in tiles:
                shape = tiles[depthIndex, longIndex, latIndex, layer][0].shape
                outputRed = numpy.zeros(shape, dtype=numpy.uint8)
                outputGreen = numpy.zeros(shape, dtype=numpy.uint8)
                outputBlue = numpy.zeros(shape, dtype=numpy.uint8)
                outputMask = numpy.zeros(shape, dtype=numpy.uint8)
                tiles[parentDepth, parentLongIndex, parentLatIndex, layer] = outputRed, outputGreen, outputBlue, outputMask
            outputRed, outputGreen, outputBlue, outputMask = tiles[parentDepth, parentLongIndex, parentLatIndex, layer]
            rasterYSize, rasterXSize = outputRed.shape

            inputRed, inputGreen, inputBlue, inputMask = tiles[depthIndex, longIndex, latIndex, layer]

            trans = numpy.matrix([[2., 0.], [0., 2.]])
            offset = 0., 0.

            affine_transform(inputRed, trans, offset, (rasterYSize, rasterXSize), inputRed, splineOrder)
            affine_transform(inputGreen, trans, offset, (rasterYSize, rasterXSize), inputGreen, splineOrder)
            affine_transform(inputBlue, trans, offset, (rasterYSize, rasterXSize), inputBlue, splineOrder)
            affine_transform(inputMask, trans, offset, (rasterYSize, rasterXSize), inputMask, splineOrder)

            longOffset, latOffset = tileOffset(depthIndex, longIndex, latIndex)
            if longOffset == 0:
                longSlice = slice(0, rasterXSize/2)
            else:
                longSlice = slice(rasterXSize/2, rasterXSize)
            if latOffset == 0:
                latSlice = slice(rasterYSize/2, rasterYSize)
            else:
                latSlice = slice(0, rasterYSize/2)

            outputRed[latSlice,longSlice] = inputRed[0:rasterYSize/2,0:rasterXSize/2]
            outputGreen[latSlice,longSlice] = inputGreen[0:rasterYSize/2,0:rasterXSize/2]
            outputBlue[latSlice,longSlice] = inputBlue[0:rasterYSize/2,0:rasterXSize/2]
            outputMask[latSlice,longSlice] = inputMask[0:rasterYSize/2,0:rasterXSize/2]

            image = Image.fromarray(numpy.dstack((outputRed, outputGreen, outputBlue, outputMask)))
            if outputDirectory is not None:
                image.save("%s/%s.png" % (outputDirectory, tileName(parentDepth, parentLongIndex, parentLatIndex)), "PNG", options="optimize")
            if outputAccumulo is not None:
                buff = BytesIO()
                image.save(buff, "PNG", options="optimize")
                outputAccumulo.write("%s-%s" % (tileName(parentDepth, parentLongIndex, parentLatIndex), layer), "{}", buff.getvalue())
Example #10
0
def anat2epispace(anatdata, subject, xfmname, order=1):
    """Resamples data from anatomical space into epi space
    
    Parameters
    ----------
    anatdata : ndarray
        data in anatomical space
    subject : str
        Name of subject
    xfmname : str
        Name of transform
    order : int
        Order of spline interpolation
        
    Returns
    -------
    epidata : ndarray
        data in EPI space
    """
    from scipy.ndimage.interpolation import affine_transform
    anatref = db.get_anat(subject)
    target = db.get_xfm(subject, xfmname, "coord")

    allxfm =  Transform(anatref.get_affine(), anatref.shape).inv * target.inv
    #allxfm = xfm * Transform(anat.get_affine(), anat.shape)

    rotpart = allxfm.xfm[:3, :3]
    transpart = allxfm.xfm[:3,-1]
    
    return affine_transform(anatdata.T, rotpart, offset=transpart, output_shape=target.shape[::-1], cval=np.nan, order=order).T
Example #11
0
 def rotate(self,rot_mat):
     """
     Rotate the 3D model
     """
     displace = np.array([self.R,self.R,self.R])
     offset = -np.dot(rot_mat,displace) + displace
     self.array = affine_transform(self.array,rot_mat,offset)
Example #12
0
def csnormalize(image, f=0.75):
    """Center and size-normalize an image."""
    bimage = 1 * (image > mean([amax(image), amin(image)]))
    w, h = bimage.shape
    [xs, ys] = mgrid[0:w, 0:h]
    s = sum(bimage)
    if s < 1e-4: return image
    s = 1.0 / s
    cx = sum(xs * bimage) * s
    cy = sum(ys * bimage) * s
    sxx = sum((xs - cx)**2 * bimage) * s
    sxy = sum((xs - cx) * (ys - cy) * bimage) * s
    syy = sum((ys - cy)**2 * bimage) * s
    w, v = eigh(array([[sxx, sxy], [sxy, syy]]))
    l = sqrt(amax(w))
    if l > 0.01:
        scale = f * max(image.shape) / (4.0 * l)
    else:
        scale = 1.0
    m = array([[1.0 / scale, 0], [0.0, 1.0 / scale]])
    w, h = image.shape
    c = array([cx, cy])
    d = c - dot(m, array([w / 2, h / 2]))
    image = interpolation.affine_transform(image, m, offset=d, order=1)
    return image
Example #13
0
 def normalize(self,img,order=1,dtype=dtype('f'),cval=0):
     assert img.shape==self.shape
     h,w = self.shape
     M = array([[self.scale,self.scale*self.m],[0,self.scale]])
     offset = (self.offset,0)
     shape = (self.target_height,int(w/self.scale)) 
     return interpolation.affine_transform(img,M,offset=offset,output_shape=shape,order=order,cval=cval,output=dtype)
Example #14
0
    def view_3d(self, x, scale=0.7, alpha=1.0, bg_val=-1):
        """
        Visualize the 3d pictures as expanded slideshow
        args:
            x: 3d matrix (img_h, img_w, img_z)
            sclae: define the distance between slides
            alpha: visibility
            bg_val: background value
        """
        # List of images instead 3d matrix
        x = x.numpy()
        images = [x[i, :, :] for i in range(self.crop_size)]

        # Define size of new picture
        stacked_height = 2 * self.pic_size
        stacked_width = int(
            self.pic_size + (self.crop_size - 1) * self.pic_size * scale
        )
        stacked = np.full((stacked_height, stacked_width), bg_val)

        # Go over each slide
        for i in range(self.crop_size):
            # The first image will be right most and on the "bottom" of the stack.
            o = (self.crop_size - i - 1) * self.pic_size * scale
            out = affine_transform(
                images[i][0, :, :],
                self.T,
                offset=[o, -o],
                output_shape=stacked.shape,
                cval=bg_val,
            )
            stacked[out != bg_val] = out[out != bg_val]

        # plot the image series
        plt.imshow(stacked, alpha=alpha, interpolation="nearest", cmap="gray")
Example #15
0
def deskew_block(block, mat=None, out_shape=None, padval=0):
    extradims = block.ndim - 3
    last3dims = (0,) * extradims + (slice(None),) * 3
    array = block[last3dims]

    deskewed = affine_transform(array, mat, output_shape=tuple(out_shape[-3:]), order=0)
    return deskewed[(None,) * extradims + (...,)]
Example #16
0
    def affine_transformations(self,set):
        
        DA_set = dataset(set)

        # for every samples in the training set
        for i in range(set.X.shape[0]):
            
            # making an affine transformation of the coordinate of the points of the image
            # (x',y') = A(x,y) + B
            # result is rotation, translation, scaling on each axis
            # to adjust a and b, limit the size of the dataset
            
            # a = .1 # best for CNN MNIST, 128 samples
            A = np.identity(n=2)+self.rng.uniform(low=-self.affine_transform_a,high=self.affine_transform_a,size=(2, 2))
            # b = .5 # best for CNN MNIST, 128 samples
            B = self.rng.uniform(low=-self.affine_transform_b,high=self.affine_transform_b,size=(2))
            
            # for every channels
            for j in range(set.X.shape[1]):
            
                DA_set.X[i][j]=affine_transform(set.X[i][j],A,offset=B,order=2)
                
                # max_rot = 15
                # angle = self.rng.random_integers(-max_rot,max_rot)
                # DA_set.X[i] = rotate(DA_set.X[i].reshape(28,28),angle, reshape=False).reshape(784)
        
        return DA_set
Example #17
0
def _transform_img(img, 
                   transform_matrix, 
                   is_h_flip, 
                   is_v_flip, 
                   elastic_inds
                   ):
    
    
    
    final_affine_matrix = transform_matrix[:2, :2]
    final_offset = transform_matrix[:2, 2]
    img_aug = affine_transform(
            img,
            final_affine_matrix,
            final_offset,
            order=0,
            mode='reflect',
            output=np.float32,
            cval=0.)
    if is_h_flip:
        img_aug = img_aug[::-1, :] 
    if is_v_flip:
        img_aug = img_aug[:, ::-1] 
    
    if elastic_inds is not None:
        img_aug = map_coordinates(img_aug, elastic_inds, order=1).reshape((img.shape))
    
    return img_aug
Example #18
0
    def shear_back(self, img, inv_shear):
        """
        TODO

        Parameters
        ----------
        img : numpy.ndarray (N,N),
              array of diffraction intensities
        inv_shear : numpy.ndarray (2,2),
                    inverse shear matrix

        Returns
        -------
        numpy.ndarray (N,N),
            sheared array of diffraction intensities
        """
        roll = img.shape[0] / 2 - 1
        ss = np.max(self.box) * inv_shear
        A1 = np.array([[1, 0, -roll], [0, 1, -roll], [0, 0, 1]])

        A2 = np.array([[ss[1, 0], ss[0, 0], roll], [ss[1, 1], ss[0, 1], roll],
                       [0, 0, 1]])

        A3 = np.linalg.inv(np.dot(A2, A1))
        A4 = A3[0:2, 0:2]
        A5 = A3[0:2, 2]
        img = affine_transform(img, A4, A5, mode="constant")
        return img
Example #19
0
 def transformimage(self, img):
     # self.cof: change-of-frame matrix for coordinates (x,y)
     if self.isidentity():
         return img
     if self.transfotype == transformationType.translation:
         # shift: takes transformation vector for coordinates (y,x)
         return ndtransform.shift(
             img, -self.cof[1::-1], cval=self.cval, **self.interpolationargs
         )
     else:
         if self.islinearidentity():
             # shift: takes transformation vector for coordinates (y,x)
             return ndtransform.shift(
                 img, -self.cof[1::-1, 2], cval=self.cval, **self.interpolationargs
             )
         elif self.isprojidentity():
             # affine_transform: takes change-of-frame matrix
             #                   for coordinates (y,x)
             return ndtransform.affine_transform(
                 img,
                 self.cof[0:2, 0:2].T,
                 offset=self.cof[1::-1, 2],
                 cval=self.cval,
                 **self.interpolationargs
             )
         else:
             raise NotImplementedError()
Example #20
0
def zoom_and_rotate_patch(patch, angle, zoom):
    theta = np.deg2rad(angle)
    rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
                                [np.sin(theta),
                                 np.cos(theta), 0], [0, 0, 1]])
    transform_matrix = rotation_matrix

    zoom_matrix = np.array([[zoom, 0, 0], [0, zoom, 0], [0, 0, 1]])

    transform_matrix = np.dot(transform_matrix, zoom_matrix)

    d, h, w = patch.shape
    transform_matrix = transform_matrix_offset_center(transform_matrix, h, w)
    patch = np.rollaxis(patch, 0, 0)
    final_affine_matrix = transform_matrix[:2, :2]
    final_offset = transform_matrix[:2, 2]

    channel_images = [
        affine_transform(x_channel,
                         final_affine_matrix,
                         final_offset,
                         order=1,
                         mode='reflect') for x_channel in patch
    ]

    patch = np.stack(channel_images, axis=0)
    patch = np.rollaxis(patch, 0, 1)

    return patch
Example #21
0
def deskew(image):
    c, v = moments(image)
    alpha = v[0, 1] / v[0, 0]
    affine = np.array([[1, 0], [alpha, 1]])
    ocenter = np.array(image.shape) / 2.0
    offset = c - np.dot(affine, ocenter)
    return interpolation.affine_transform(image, affine, offset=offset)
Example #22
0
def extract(image, y0, x0, y1, x1, mode='nearest', cval=0):
    h, w = image.shape
    ch, cw = y1 - y0, x1 - x0
    y, x = clip(y0, 0, max(h - ch, 0)), clip(x0, 0, max(w - cw, 0))
    sub = image[y:y + ch, x:x + cw]
    # print("extract", image.dtype, image.shape)
    try:
        r = interpolation.shift(sub, (y - y0, x - x0),
                                mode=mode,
                                cval=cval,
                                order=0)
        if cw > w or ch > h:
            pady0, padx0 = max(-y0, 0), max(-x0, 0)
            r = interpolation.affine_transform(r,
                                               eye(2),
                                               offset=(pady0, padx0),
                                               cval=1,
                                               output_shape=(ch, cw))
        return r

    except RuntimeError:
        # workaround for platform differences between 32bit and 64bit
        # scipy.ndimage
        dtype = sub.dtype
        sub = array(sub, dtype='float64')
        sub = interpolation.shift(sub, (y - y0, x - x0),
                                  mode=mode,
                                  cval=cval,
                                  order=0)
        sub = array(sub, dtype=dtype)
        return sub
Example #23
0
def imageResampling(inputimage, outputspacing, order=1):
    #inputimage : a nibabel image
    #outputspacing : a numpy array with three scaling factors
    #order : order for spline based interpolation (0: nn, 1 : linear, ...)
    M1 = np.diag(outputspacing)
    T1 = outputspacing / 2
    inputspacing = inputimage.header.get_zooms()[0:3]
    M2 = np.diag(np.asarray(inputspacing))
    T2 = np.asarray(inputspacing) / 2

    M = np.linalg.inv(homogeneousMatrix(M2, T2)).dot(homogeneousMatrix(M1, T1))
    outputaffine = np.dot(inputimage.affine, M)

    T = M[0:3, 3]
    M = M[0:3, 0:3]

    zoom = np.diag(M[0:3, 0:3])
    outputshape = np.ceil(
        (inputimage.get_data().shape[0:3]) / zoom).astype(int)
    inputarray = inputimage.get_data()
    inputarray = np.reshape(inputarray, inputarray.shape[0:3])
    outputarray = affine_transform(inputarray,
                                   M,
                                   offset=T,
                                   output_shape=outputshape,
                                   order=order,
                                   mode='constant',
                                   cval=0.0,
                                   prefilter=False)

    return nibabel.Nifti1Image(outputarray, outputaffine)
Example #24
0
def deskew_block(block, mat=None, out_shape=None, padval=0):
    try:
        from ._cupy_affine import affine_transform

        backend_name = 'CuPy'
    except ImportError:
        logger.warning(
            "Could not import CuPy. "
            "Falling back to PyOpenCL for affine transforms"
        )
        try:
            from ._ocl_affine import affine_transform

            backend_name = 'PyOpenCL'
        except ImportError:
            from scipy.ndimage.interpolation import affine_transform

            backend_name = 'SciPy'
            logger.warning(
                "Could not import CuPy or PyOpenCL. "
                "Falling back to SciPy for CPU affine transforms"
            )

    extradims = block.ndim - 3
    last3dims = (0,) * extradims + (slice(None),) * 3
    array = block[last3dims]

    t_start = time()
    deskewed = affine_transform(array, mat, output_shape=tuple(out_shape[-3:]), order=0)
    logger.debug(f"\tduration ({backend_name}): {time() - t_start} s")
    return deskewed[(None,) * extradims + (...,)]
Example #25
0
    def instantiateAt(self, x, y, nativeScale=False):
        from scipy.ndimage.interpolation import affine_transform
        psf = np.zeros_like(self.psfbases[0])
        #print 'psf', psf.shape
        dx = (x - self.x0) / self.xscale
        dy = (y - self.y0) / self.yscale
        i = 0
        #print 'dx',dx,'dy',dy
        for d in range(self.degree + 1):
            #print 'degree', d
            for j in range(d + 1):
                k = d - j
                #print 'x',j,'y',k,
                #print 'component', i
                amp = dx**j * dy**k
                #print 'amp', amp,
                # PSFEx manual pg. 111 ?
                ii = j + (self.degree + 1) * k - (k * (k - 1)) / 2
                #print 'ii', ii, 'vs i', i
                psf += self.psfbases[ii] * amp
                #print 'basis rms', np.sqrt(np.mean(self.psfbases[i]**2)),
                i += 1
                #print 'psf sum', psf.sum()
        #print 'min', psf.min(), 'max', psf.max()

        if (self.scale or nativeScale) and self.sampling != 1:
            ny, nx = psf.shape
            spsf = affine_transform(psf, [1. / self.sampling] * 2,
                                    offset=nx / 2 * (self.sampling - 1.))
            return spsf

        return psf
Example #26
0
    def set_requested_array(self, name, array, array_name):
        if name != self.current_simulation:
            return
        if array_name == 'organ':
            self.organ_array = affine_transform(array,
                                 self.scale,
                                 output_shape=np.floor(np.array(array.shape)/self.scale),
                                 cval=0, output=np.uint8, prefilter=True,
                                 order=0).astype(np.uint8)
        elif array_name == 'organ_map':
            self.organ_map = {array['organ'][i]: str(array['organ_name'][i], encoding='utf-8') for i in range(len(array))}
        elif array_name == 'organ_material_map':
            self.organ_material_map = {array['organ'][i]: str(array['material_name'][i], encoding='utf-8') for i in range(len(array))}

        if self.organ_array is not None and self.organ_map is not None and self.organ_material_map is not None:
            self.layoutAboutToBeChanged.emit()
            self._data = {}
            self._data_keys = []
            for key, value in self.organ_map.items():
                self._data[key] = [value, self.organ_material_map.get(key, 'Unknown'), 0, 0]
                self._data_keys.append(key)
            self.dose_z_lenght = list(range(self.organ_array.shape[0]))
#            self.dataChanged.emit(self.index(0, 0), self.index(len(self._data), 2))
            self.request_array_slice.emit(self.current_simulation, 'dose', 0, 0)
            self.layoutChanged.emit()
            self.hide_view.emit(False)
Example #27
0
def apply_transform(x,
                    transform_matrix,
                    channel_axis=0,
                    fill_mode='constant',
                    cval=0.):
    """Apply the image transformation specified by a matrix.
  Arguments:
      x: 2D numpy array, single image.
      transform_matrix: Numpy array specifying the geometric transformation.
      channel_axis: Index of axis for channels in the input tensor.
      fill_mode: Points outside the boundaries of the input
          are filled according to the given mode
          (one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
      cval: Value used for points outside the boundaries
          of the input if `mode='constant'`.
  Returns:
      The transformed version of the input.
  """
    x = np.rollaxis(x, channel_axis, 0)
    final_affine_matrix = transform_matrix[:2, :2]
    final_offset = transform_matrix[:2, 2]
    channel_images = [
        interpolation.affine_transform(x_channel,
                                       final_affine_matrix,
                                       final_offset,
                                       order=0,
                                       mode=fill_mode,
                                       cval=cval) for x_channel in x
    ]
    x = np.stack(channel_images, axis=0)
    x = np.rollaxis(x, 0, channel_axis + 1)
    return x
Example #28
0
    def affine_transformations(self, set):

        DA_set = dataset(set)

        # for every samples in the training set
        for i in range(set.X.shape[0]):

            # making an affine transformation of the coordinate of the points of the image
            # (x',y') = A(x,y) + B
            # result is rotation, translation, scaling on each axis
            # to adjust a and b, limit the size of the dataset

            # a = .1 # best for CNN MNIST, 128 samples
            A = np.identity(n=2) + self.rng.uniform(
                low=-self.affine_transform_a,
                high=self.affine_transform_a,
                size=(2, 2))
            # b = .5 # best for CNN MNIST, 128 samples
            B = self.rng.uniform(low=-self.affine_transform_b,
                                 high=self.affine_transform_b,
                                 size=(2))

            # for every channels
            for j in range(set.X.shape[1]):

                DA_set.X[i][j] = affine_transform(set.X[i][j],
                                                  A,
                                                  offset=B,
                                                  order=2)

                # max_rot = 15
                # angle = self.rng.random_integers(-max_rot,max_rot)
                # DA_set.X[i] = rotate(DA_set.X[i].reshape(28,28),angle, reshape=False).reshape(784)

        return DA_set
Example #29
0
def anat2epispace(anatdata, subject, xfmname, order=1):
    """Resamples data from anatomical space into epi space
    
    Parameters
    ----------
    anatdata : ndarray
        data in anatomical space
    subject : str
        Name of subject
    xfmname : str
        Name of transform
    order : int
        Order of spline interpolation
        
    Returns
    -------
    epidata : ndarray
        data in EPI space
    """
    from scipy.ndimage.interpolation import affine_transform
    anatref = db.get_anat(subject)
    target = db.get_xfm(subject, xfmname, "coord")

    allxfm = Transform(anatref.get_affine(), anatref.shape).inv * target.inv
    #allxfm = xfm * Transform(anat.get_affine(), anat.shape)

    rotpart = allxfm.xfm[:3, :3]
    transpart = allxfm.xfm[:3, -1]

    return affine_transform(anatdata.T,
                            rotpart,
                            offset=transpart,
                            output_shape=target.shape[::-1],
                            cval=np.nan,
                            order=order).T
Example #30
0
def epi2anatspace(volumedata, order=1):
    """Resample an epi volume data into anatomical space using scipy.

    Parameters
    ----------
    volumedata : VolumeData
        The input epi volumedata object.
    order : int
        The order of the resampler, in terms of splines. 0 is nearest, 1 is linear.

    Returns
    -------
    anatspace : ndarray
        The ND array of the anatomy space data
    """
    from scipy.ndimage.interpolation import affine_transform
    ds = dataset.normalize(volumedata)
    volumedata = ds#.data

    anat = db.get_anat(volumedata.subject, "raw")
    xfm = db.get_xfm(volumedata.subject, volumedata.xfmname, "coord")

    #allxfm =  Transform(anat.get_affine(), anat.shape).inv * xfm.inv
    allxfm = xfm * Transform(anat.get_affine(), anat.shape)

    rotpart = allxfm.xfm[:3, :3]
    transpart = allxfm.xfm[:3,-1]
    return affine_transform(volumedata.volume.T.squeeze(), rotpart,
                            offset=transpart, output_shape=anat.shape[::-1],
                            cval=np.nan, order=order).T
Example #31
0
    def undo_transform(self, sample, metadata=None):
        assert "rotation" in metadata
        assert "scale" in metadata
        assert "translation" in metadata
        # Opposite rotation, same axes
        angle, axes = -metadata['rotation'][0], metadata['rotation'][1]
        scale = 1 / np.array(metadata['scale'])
        translation = -np.array(metadata['translation'])

        # Undo rotation
        dict_params = {
            "rotation": [angle, axes],
            "scale": scale,
            "translation": [0, 0, 0],
            "undo": True
        }

        data_out, _ = self.__call__(sample, dict_params)

        data_out = affine_transform(data_out,
                                    np.identity(3),
                                    order=1,
                                    offset=translation,
                                    output_shape=sample.shape).astype(
                                        sample.dtype)

        return data_out, metadata
Example #32
0
def rgeometry(im, eps=0.04, delta=0.8, cval=None, severity=1):
    """
    affine transform
    """
    if severity == 0:
        return im

    if cval is None:
        cval = [0] * im.shape[2]
    elif isinstance(cval, (float, int)):
        cval = [cval] * im.shape[2]

    severity = abs(severity)
    eps = severity * eps
    delta = severity * delta
    m = np.array([[1 + eps * randn(), 0.0],
                  [eps * randn(), 1.0 + eps * randn()]])
    c = np.array(im.shape[:2]) * 0.5
    d = c - np.dot(m, c) + np.array([randn() * delta, randn() * delta])

    im = cv2.split(im)
    im = [
        interpolation.affine_transform(i,
                                       m,
                                       offset=d,
                                       order=1,
                                       mode='constant',
                                       cval=cval[e]) for e, i in enumerate(im)
    ]
    im = cv2.merge(im)

    return np.array(im)
Example #33
0
def csnormalize(image,f=0.75):
    """Center and size-normalize an image."""
    bimage = 1*(image>mean([amax(image),amin(image)]))
    w,h = bimage.shape
    [xs,ys] = mgrid[0:w,0:h]
    s = sum(bimage)
    if s<1e-4: return image
    s = 1.0/s
    cx = sum(xs*bimage)*s
    cy = sum(ys*bimage)*s
    sxx = sum((xs-cx)**2*bimage)*s
    sxy = sum((xs-cx)*(ys-cy)*bimage)*s
    syy = sum((ys-cy)**2*bimage)*s
    w,v = eigh(array([[sxx,sxy],[sxy,syy]]))
    l = sqrt(amax(w))
    if l>0.01:
        scale = f*max(image.shape)/(4.0*l)
    else:
        scale = 1.0
    m = array([[1.0/scale,0],[0.0,1.0/scale]])
    w,h = image.shape
    c = array([cx,cy])
    d = c-dot(m,array([w/2,h/2]))
    image = interpolation.affine_transform(image,m,offset=d,order=1)
    return image
Example #34
0
def rotate(v, angle=None, rm=None, c1=None, c2=None, loc_r=None, siz2=None, default_val=float('NaN')):
    if (angle is not None):
        assert (rm is None)
        angle = N.array(angle, dtype=N.float).flatten()
        rm = AA.rotation_matrix_zyz(angle)
    if (rm is None):
        rm = N.eye(v.ndim)
    siz1 = N.array(v.shape, dtype=N.float)
    if (c1 is None):
        c1 = ((siz1 - 1) / 2.0)
    else:
        c1 = c1.flatten()
    assert (c1.shape == (3,))
    if (siz2 is None):
        siz2 = siz1
    siz2 = N.array(siz2, dtype=N.float)
    if (c2 is None):
        c2 = ((siz2 - 1) / 2.0)
    else:
        c2 = c2.flatten()
    assert (c2.shape == (3,))
    if (loc_r is not None):
        loc_r = N.array(loc_r, dtype=N.float).flatten()
        assert (loc_r.shape == (3,))
        c2 += loc_r
    c = ((- rm.dot(c2)) + c1)
    vr = SNI.affine_transform(input=v, matrix=rm, offset=c, output_shape=siz2.astype(N.int), cval=default_val)
    return vr
Example #35
0
    def instantiateAt(self, x, y, nativeScale=False):
        from scipy.ndimage.interpolation import affine_transform
        psf = np.zeros_like(self.psfbases[0])
        #print 'psf', psf.shape
        dx = (x - self.x0) / self.xscale
        dy = (y - self.y0) / self.yscale
        i = 0
        #print 'dx',dx,'dy',dy
        for d in range(self.degree + 1):
            #print 'degree', d
            for j in range(d+1):
                k = d - j
                #print 'x',j,'y',k,
                #print 'component', i
                amp = dx**j * dy**k
                #print 'amp', amp,
                # PSFEx manual pg. 111 ?
                ii = j + (self.degree+1) * k - (k * (k-1))/ 2
                #print 'ii', ii, 'vs i', i
                psf += self.psfbases[ii] * amp
                #print 'basis rms', np.sqrt(np.mean(self.psfbases[i]**2)),
                i += 1
                #print 'psf sum', psf.sum()
        #print 'min', psf.min(), 'max', psf.max()

        if (self.scale or nativeScale) and self.sampling != 1:
            ny,nx = psf.shape
            spsf = affine_transform(psf, [1./self.sampling]*2,
                                    offset=nx/2 * (self.sampling - 1.))
            return spsf
            
        return psf
Example #36
0
    def transform(self, Xb, yb):
        Xb, yb = super(ImageTransformationIterator, self).transform(Xb, yb)

        for i, img in enumerate(Xb[:, 0, :, :]):

            c_in = 0.5 * np.array(img.shape)
            c_out = np.array(
                (14.0 + random.randint(-1, 1), 14.0 + random.randint(-1, 1)))

            theta = 20 * (random.random() - 0.5) * math.pi / 180.0
            scaling = np.diag(([
                1. + 0.4 * (random.random() - 0.5),
                1. + 0.4 * (random.random() - 0.5)
            ]))

            transform = np.array([[math.cos(theta), -math.sin(theta)],
                                  [math.sin(theta),
                                   math.cos(theta)]]).dot(scaling)
            offset = c_in - c_out.dot(transform)
            dst = affine_transform(img,
                                   transform.T,
                                   order=2,
                                   offset=offset,
                                   output_shape=(28, 28),
                                   cval=0.0,
                                   output=np.float32)
            dst[dst < 0.01] = 0.

            Xb[i, 0] = dst

        return Xb, yb
def transform_image(image, T, origin='center', inverse=False):
    # image: 2 or 3 dimensional image (h, w, c)
    # T: 3x3 augmented transformation matrix.
    # origin: 'center' or 'topleft': where is the origin of the T transformation.
    # inverse: T is defined as the transformation from the output to the input (invers).
    # If inverse is False, T is inversed.
    # now the transformation is from the input to the output
    if image.ndim == 3:
        # do the transformation for every channel
        num_channels = image.shape[2]
        transformed_channels = []
        for i in range(num_channels):
            transformed_channels.append(transform_image(image[..., i], T, origin=origin, inverse=inverse))
        return np.stack(transformed_channels, axis=-1)
    if image.ndim != 2:
        raise ValueError('Wrong number of dimensions.')

    h, w = image.shape

    if not inverse:
        T_i = np.linalg.inv(T)
    else:
        T_i = T

    if origin == 'center':
        c = (h / 2, w / 2)
    else:
        c = (0, 0)

    T_o = [[1, 0, c[0]], [0, 1, c[1]], [0, 0, 1]] @ T_i @ [[1, 0, -c[0]], [0, 1, -c[1]], [0, 0, 1]]

    transformed = affine_transform(image, T_o, mode='constant')
    return transformed
Example #38
0
def affineRotAroundCenter(arr, theta, axis=[0, 0, 1]):
    """
    scipy based rotation about center of cube, CPU
    :param arr: 3D array to be rotated
    :param theta: radients
    :param axis: axis
    :return: ratated 3D array
    """
    def Mrot3d(axis, theta):
        axis = np.array(axis)
        return expm(np.cross(np.eye(3), axis / norm(axis) * theta))

    def Mrot2d(theta):
        return np.array([[np.cos(theta), -np.sin(theta)],
                         [np.sin(theta), np.cos(theta)]])

    dim = len(arr.shape)
    if dim == 2:
        M = Mrot2d(theta)
    elif dim == 3:
        M = Mrot3d(axis, theta)
    else:
        raise RuntimeError(
            "Affine rotation cannot rotate array with shape %s" %
            (arr.shape, ))
    center = 0.5 * (np.array(arr.shape) - np.ones((dim, )))
    return affine_transform(arr,
                            M,
                            mode="nearest",
                            offset=center - M.dot(center))
Example #39
0
def extract_centered(image, shape, center):
    """Extracts a patch of size `shape` centered on `center`."""
    center = array(center) - array(shape) / 2.0
    return interpolation.affine_transform(1.0 * image,
                                          diag([1, 1]),
                                          offset=center,
                                          output_shape=shape,
                                          order=1)
Example #40
0
def deskew_image(image):
    # From https://fsix.github.io/mnist/Deskewing.html
    c, v = moments(image)
    alpha = v[0, 1] / v[0, 0]
    affine = np.array([[1, 0], [alpha, 1]])
    ocenter = np.array(image.shape) / 2.0
    offset = c - np.dot(affine, ocenter)
    return interpolation.affine_transform(image, affine, offset=offset)
def deskew(image):
    """deskew image"""
    image = numpy.reshape(image, (28, 28)) if image.shape != (28, 28) else image
    c,v = moments(image)
    alpha = v[0,1]/v[0,0]
    affine = numpy.array([[1,0],[alpha,1]])
    ocenter = numpy.array(image.shape)/2.0
    offset = c-numpy.dot(affine,ocenter)
    return interpolation.affine_transform(image,affine,offset=offset)
Example #42
0
def scale_to_h(img,target_height,order=1,dtype=np.dtype('f'),cval=0):
    h,w = img.shape
    scale = target_height*1.0/h
    target_width = int(scale*w)
    output = interpolation.affine_transform(1.0*img,np.eye(2)/scale,order=order,
                                            output_shape=(target_height,target_width),
                                            mode='constant',cval=cval)
    output = np.array(output,dtype=dtype)
    return output
Example #43
0
def extract_centered_scaled(image,shape,center,scale):
    """Extracts a patch of size `shape` centered on `center`
    and scaled by `scale`."""
    scale = 1.0/scale
    rshape = scale*array(shape)
    center = array(center)-rshape/2.0
    result = interpolation.affine_transform(1.0*image,diag([scale,scale]),offset=center,
                                            output_shape=shape,order=1)
    return result
Example #44
0
def anat2epispace(anatdata, subject, xfmname, order=1):
    anatref = surfs.getAnat(subject)
    target = surfs.getXfm(subject, xfmname, "coord")

    allxfm =  Transform(anatref.get_affine(), anatref.shape).inv * target.inv
    #allxfm = xfm * Transform(anat.get_affine(), anat.shape)

    rotpart = allxfm.xfm[:3, :3]
    transpart = allxfm.xfm[:3,-1]
    return affine_transform(anatdata.T, rotpart, offset=transpart, output_shape=target.shape[::-1], cval=np.nan, order=order).T
Example #45
0
 def rotate(self, fig, angle=None):
     if angle is None:
         angle = np.random.uniform(low=0.0, high=2.0*np.pi)
     matrix = np.array([
         [np.cos(angle), -np.sin(angle)],
         [np.sin(angle),  np.cos(angle)]])
     in_center = 0.5*np.array(fig.data.shape)
     offset = in_center - in_center.dot(matrix)
     fig.data = interpolation.affine_transform(fig.data, matrix.T,
                                               offset=offset)
Example #46
0
def zoomOutImage(parentKey, childKeys, childImages, splineOrder):
    rasterYSize, rasterXSize = childImages[0].shape[0:2]
    outputRed = numpy.zeros((rasterYSize, rasterXSize), dtype=numpy.uint8)
    outputGreen = numpy.zeros((rasterYSize, rasterXSize), dtype=numpy.uint8)
    outputBlue = numpy.zeros((rasterYSize, rasterXSize), dtype=numpy.uint8)
    outputMask = numpy.zeros((rasterYSize, rasterXSize), dtype=numpy.uint8)

    for key, image in zip(childKeys, childImages):
        inputRed = image[:,:,0]
        inputGreen = image[:,:,1]
        inputBlue = image[:,:,2]
        inputMask = image[:,:,3]

        trans = numpy.matrix([[2., 0.], [0., 2.]])
        offset = 0., 0.

        inputRed = affine_transform(inputRed, trans, offset, (rasterYSize, rasterXSize), None, splineOrder)
        inputGreen = affine_transform(inputGreen, trans, offset, (rasterYSize, rasterXSize), None, splineOrder)
        inputBlue = affine_transform(inputBlue, trans, offset, (rasterYSize, rasterXSize), None, splineOrder)
        inputMask = affine_transform(inputMask, trans, offset, (rasterYSize, rasterXSize), None, splineOrder)

        depth, longIndex, latIndex, layer, timestamp = key.split("-")
        depth = int(depth[1:])
        longIndex = int(longIndex)
        latIndex = int(latIndex)

        longOffset, latOffset = tileOffset(depth, longIndex, latIndex)
        if longOffset == 0:
            longSlice = slice(0, rasterXSize/2)
        else:
            longSlice = slice(rasterXSize/2, rasterXSize)
        if latOffset == 0:
            latSlice = slice(rasterYSize/2, rasterYSize)
        else:
            latSlice = slice(0, rasterYSize/2)

        outputRed[latSlice,longSlice] = inputRed[0:rasterYSize/2,0:rasterXSize/2]
        outputGreen[latSlice,longSlice] = inputGreen[0:rasterYSize/2,0:rasterXSize/2]
        outputBlue[latSlice,longSlice] = inputBlue[0:rasterYSize/2,0:rasterXSize/2]
        outputMask[latSlice,longSlice] = inputMask[0:rasterYSize/2,0:rasterXSize/2]

    return numpy.dstack((outputRed, outputGreen, outputBlue, outputMask))
Example #47
0
def ocropy_degrade(im, distort=1.0, dsigma=20.0, eps=0.03, delta=0.3, degradations=[(0.5, 0.0, 0.5, 0.0)]):
    """
    Degrades and distorts a line using the same noise model used by ocropus.

    Args:
        im (PIL.Image): Input image
        distort (float):
        dsigma (float):
        eps (float):
        delta (float): 
        degradations (list): list returning 4-tuples corresponding to
                             the degradations argument of ocropus-linegen.

    Returns:
        PIL.Image in mode 'L'
    """
    w, h = im.size
    # XXX: determine correct output shape from transformation matrices instead
    # of guesstimating.
    image = Image.new('L', (int(1.5*w), 4*h), 255)
    image.paste(im, (int((image.size[0] - w) / 2), int((image.size[1] - h) / 2)))
    a = pil2array(image.convert('L'))
    (sigma,ssigma,threshold,sthreshold) = degradations[np.random.choice(len(degradations))]
    sigma += (2*np.random.rand()-1)*ssigma
    threshold += (2*np.random.rand()-1)*sthreshold
    a = a*1.0/np.amax(a)
    if sigma>0.0:
        a = gaussian_filter(a,sigma)
    a += np.clip(np.random.randn(*a.shape)*0.2,-0.25,0.25)
    m = np.array([[1+eps*np.random.randn(),0.0],[eps*np.random.randn(),1.0+eps*np.random.randn()]])
    w,h = a.shape
    c = np.array([w/2.0,h/2])
    d = c-np.dot(m, c)+np.array([np.random.randn()*delta, np.random.randn()*delta])
    a = affine_transform(a, m, offset=d, order=1, mode='constant', cval=a[0,0])
    a = np.array(a>threshold,'f')
    [[r,c]] = find_objects(np.array(a==0,'i'))
    r0 = r.start
    r1 = r.stop
    c0 = c.start
    c1 = c.stop
    a = a[r0-5:r1+5,c0-5:c1+5]
    if distort > 0:
        h,w = a.shape
        hs = np.random.randn(h,w)
        ws = np.random.randn(h,w)
        hs = gaussian_filter(hs, dsigma)
        ws = gaussian_filter(ws, dsigma)
        hs *= distort/np.amax(hs)
        ws *= distort/np.amax(ws)
        def f(p):
            return (p[0]+hs[p[0],p[1]],p[1]+ws[p[0],p[1]])
        a = geometric_transform(a, f, output_shape=(h,w), order=1, mode='constant', cval=np.amax(a))
    im = array2pil(a).convert('L')
    return im
Example #48
0
File: RPN.py Project: jlettvin/RPN
 def zoom(self):
     """
     zoom each color plane proportional to its wavelength
     zoom shrinks in proportion to wavelength
     """
     X, Y   = self.X, self.Y
     coeff  = self.internal_pop()
     offset = [X*(1.0-coeff)/2.0, Y*(1.0-coeff)/2.0]
     kernel = [[coeff, 0.0], [0.0, coeff]]
     self.internal_push(affine_transform(
         self.internal_pop(), kernel, offset=offset, prefilter=False))
Example #49
0
def anat2epispace(anatdata, subject, xfmname, order=1):
    from scipy.ndimage.interpolation import affine_transform
    anatref = db.get_anat(subject)
    target = db.get_xfm(subject, xfmname, "coord")

    allxfm =  Transform(anatref.get_affine(), anatref.shape).inv * target.inv
    #allxfm = xfm * Transform(anat.get_affine(), anat.shape)

    rotpart = allxfm.xfm[:3, :3]
    transpart = allxfm.xfm[:3,-1]
    return affine_transform(anatdata.T, rotpart, offset=transpart, output_shape=target.shape[::-1], cval=np.nan, order=order).T
Example #50
0
 def scale(self, fig, scale=None):
     if scale is None:
         scale = np.random.uniform(low=self.min_size/self.max_size,
                                   high=1.0)
     matrix = np.array([[1.0/scale, 0.0], [0.0, 1.0/scale]])
     in_center = 0.5*np.array(fig.data.shape)
     offset = in_center - in_center.dot(matrix)
     in_center = 0.5*np.array(fig.data.shape)
     offset = in_center - in_center.dot(matrix)
     fig.data = interpolation.affine_transform(fig.data, matrix,
                                               offset=offset)
     fig.size *= scale
Example #51
0
def scale_to_h(img, target_height, order=1, dtype=np.dtype('f'), cval=0):
    h, w = img.shape
    scale = target_height*1.0/h
    target_width = int(scale*w)
    with warnings.catch_warnings():
        warnings.simplefilter('ignore', UserWarning)
        output = interpolation.affine_transform(1.0*img, np.ones(2)/scale,
                                                order=order,
                                                output_shape=(target_height,
                                                              target_width),
                                                mode='constant', cval=cval)
    output = np.array(output, dtype=dtype)
    return output
Example #52
0
def test_net(
        net,
        x,
        b_name='\033[30mbaseline\033[0m',
        f_name='\033[30mfollow\033[0m',
        loc_name='\033[33mloc_net\033[0m'
):
    import theano
    import lasagne
    c = color_codes()
    # We try to get the last weights to keep improving the net over and over
    x_test = np.split(x, 2, axis=1)
    b_inputs = (b_name, x_test[0])
    f_inputs = (f_name, x_test[1])
    inputs = dict([b_inputs, f_inputs])
    print(c['c'] + '[' + strftime("%H:%M:%S") + ']    ' + c['g'] +
          'Predicting (' + c['b'] + 'images' + c['nc'] + c['g'] + ')' + c['nc'])
    print('                Testing vector shape ='
          ' (' + ','.join([str(length) for length in x.shape]) + ')')
    y_test = net.predict(inputs)
    print(c['c'] + '[' + strftime("%H:%M:%S") + ']    ' + c['g'] +
          'Predicting (' + c['b'] + 'transformations' + c['nc'] + c['g'] + ')' + c['nc'])

    f = theano.function(
        [
            net.layers_[b_name].input_var,
            net.layers_[f_name].input_var
        ],
        lasagne.layers.get_output(
            net.layers_[loc_name],
            {
                b_name: net.layers_[b_name].input_var,
                f_name: net.layers_[f_name].input_var
            }
        ),
        name='registration'
    )

    rand_transf = [random_affine3d_matrix(x_range=1/36.0, y_range=1/36.0, z_range=1/36.0) for x_t in x_test[0]]
    x_test_random = np.stack([affine_transform(x_t, t) for x_t, t in zip(x_test[0], rand_transf)])
    transforms = f(x_test_random, x_test[0])

    for tt, t in zip(transforms, rand_transf):
        print('%s   %s' % (','.join(['%f' % ts for ts in t[0, :]]), ','.join(['%f' % tts for tts in tt[:4]])))
        print('%s = %s' % (','.join(['%f' % ts for ts in t[1, :]]), ','.join(['%f' % tts for tts in tt[4:8]])))
        print('%s   %s' % (','.join(['%f' % ts for ts in t[2, :]]), ','.join(['%f' % tts for tts in tt[8:12]])))

    return y_test, transforms
Example #53
0
def horizontal_shear(im, degree):
    rad = degree / 180.0 * math.pi
    padding_x = int(abs(np.tan(rad)) * im.shape[0])
    padding_y = im.shape[0]
    if rad > 0:
        im_pad = np.concatenate(
            (255 * np.ones((padding_y, padding_x), dtype=int), im), axis=1)
    elif rad < 0:
        im_pad = np.concatenate(
            (im, 255 * np.ones((padding_y, padding_x), dtype=int)), axis=1)
    else:
        im_pad = im
    shear_matrix = np.array([[1, 0],
                             [np.tan(rad), 1]])
    sheared_im = affine_transform(im_pad, shear_matrix, cval=255.0)
    return sheared_im
 def setupVolumes(self):
     if (self.imBg is not None) and (self.imFg is not None):
         self.mgr = VolumeRenderingManager(_gray_data)
         self.ren.AddVolume(self.mgr.volume)
         print 'Transforming volume...'
         self.imFg_t =\
             affine_transform(self.imFg,
                              matrix=self.matrix[0:3, 0:3],
                              offset=self.matrix[0:3, 3],
                              output_shape=self.imBg.shape)
         print 'Done'
         imOut = self.imBg * self.imFg_t
         imOut[imOut < 0.0] = 0.0
         imOut[imOut > 1.0] = 1.0
         self.mgr.setInput((imOut * 255.0).astype(np.uint8))
         self.mgr.update()
Example #55
0
def extract_keypoint(img, keypoint, patch_shape, scale):
    x = keypoint[0]
    y = keypoint[1]
    a = keypoint[2]
    b = keypoint[3]
    c = keypoint[4]
    A = np.array([[c, b], [b, a]])
    u, s, v = np.linalg.svd(A)
    s = 1/np.sqrt(s)
    A = np.dot(u, np.dot(np.diag(s), v))
    A = A * scale * 2/patch_shape[0]

    offset = np.array([y, x])
    offset -= np.dot(A, np.array([patch_shape[0], patch_shape[1]]))/2
    patch = affine_transform(img, A, offset=offset, output_shape=patch_shape,
                             order=1, prefilter=False)
    return patch
Example #56
0
def extract_centered_scaled_barred(image,shape,center,scale,bar=None):
    """Extracts a patch of size `shape` centered on `center`
    and scaled by `scale`. Optionally adds a "bar" to the left side
    of the image, usually used to indicate the baseline and x-line
    of a text line."""
    scale = 1.0/scale
    rshape = scale*array(shape)
    center = array(center)-rshape/2.0
    result = interpolation.affine_transform(1.0*image,diag([scale,scale]),offset=center,
                                            output_shape=shape,order=1)
    if bar is not None:
        bar = array(bar,'f')
        bar -= center[0]
        bar /= scale
        result[int(bar[0]):int(bar[1]),0] = amax(result)

    return result
Example #57
0
def distort_line(im, distort=3.0, sigma=10, eps=0.03, delta=0.3):
    """
    Distorts a line image.

    Run BEFORE degrade_line as a white border of 5 pixels will be added.

    Args:
        im (PIL.Image): Input image
        distort (float):
        sigma (float):
        eps (float):
        delta (float):

    Returns:
        PIL.Image in mode 'L'
    """
    w, h = im.size
    # XXX: determine correct output shape from transformation matrices instead
    # of guesstimating.
    logger.debug(u'Pasting source image into canvas')
    image = Image.new('L', (int(1.5*w), 4*h), 255)
    image.paste(im, (int((image.size[0] - w) / 2), int((image.size[1] - h) / 2)))
    line = pil2array(image.convert('L'))

    # shear in y direction with factor eps * randn(), scaling with 1 + eps *
    # randn() in x/y axis (all offset at d)
    logger.debug(u'Performing affine transformation')
    m = np.array([[1 + eps * np.random.randn(), 0.0], [eps * np.random.randn(), 1.0 + eps * np.random.randn()]])
    c = np.array([w/2.0, h/2])
    d = c - np.dot(m, c) + np.array([np.random.randn() * delta, np.random.randn() * delta])
    line = affine_transform(line, m, offset=d, order=1, mode='constant', cval=255)

    hs = gaussian_filter(np.random.randn(4*h, int(1.5*w)), sigma)
    ws = gaussian_filter(np.random.randn(4*h, int(1.5*w)), sigma)
    hs *= distort/np.amax(hs)
    ws *= distort/np.amax(ws)

    def _f(p):
        return (p[0] + hs[p[0], p[1]], p[1] + ws[p[0], p[1]])

    logger.debug(u'Performing geometric transformation')
    im = array2pil(geometric_transform(line, _f, order=1, mode='nearest'))
    logger.debug(u'Cropping canvas to content box')
    im = im.crop(ImageOps.invert(im).getbbox())
    return im
Example #58
0
    def at(self, x, y, nativeScale=True):
        """
        Returns an image of the PSF at the given pixel coordinates.
        """
        psf = np.zeros_like(self.psfbases[0])

        # print('Evaluating PsfEx at', x,y)
        for term, base in zip(self.polynomials(x, y), self.psfbases):
            # print('  polynomial', term, 'x base w/ range', base.min(), base.max())
            psf += term * base

        if nativeScale and self.sampling != 1:
            from scipy.ndimage.interpolation import affine_transform

            ny, nx = psf.shape
            spsf = affine_transform(psf, [1.0 / self.sampling] * 2, offset=nx / 2 * (self.sampling - 1.0))
            return spsf

        return psf
Example #59
0
def extract(image,y0,x0,y1,x1,mode='nearest',cval=0):
    h,w = image.shape
    ch,cw = y1-y0,x1-x0
    y,x = np.clip(y0,0,max(h-ch,0)),np.clip(x0,0,max(w-cw, 0))
    sub = image[y:y+ch,x:x+cw]
    # print("extract", image.dtype, image.shape)
    try:
        r = interpolation.shift(sub,(y-y0,x-x0),mode=mode,cval=cval,order=0)
        if cw > w or ch > h:
            pady0, padx0 = max(-y0, 0), max(-x0, 0)
            r = interpolation.affine_transform(r, np.eye(2), offset=(pady0, padx0), cval=1, output_shape=(ch, cw))
        return r

    except RuntimeError:
        # workaround for platform differences between 32bit and 64bit
        # scipy.ndimage
        dtype = sub.dtype
        sub = np.array(sub,dtype='float64')
        sub = interpolation.shift(sub,(y-y0,x-x0),mode=mode,cval=cval,order=0)
        sub = np.array(sub,dtype=dtype)
        return sub