Exemplo n.º 1
0
    def vis_feature(self, s, measurement, **kwargs):

        """
        This method extracts patches of shape same as the input shape of 2D CNN, measures a feature for each patch's segmentation map and stitches them back to form a checkered image. 
        
        s : numpy.array  
            greyscale image slice of shape (ny, nx)  
        
        """
        
        # Handle patching parameter inputs
        patch_size = self.model.output_shape[1:-1]    
    
        # Resize images
        orig_shape = s.shape
        s = cv2.resize(s, (self.max_patches[1]*patch_size[1] - self.overlap[1],\
                           self.max_patches[0]*patch_size[0] - self.overlap[0]))
        
        # Make patches
        downres_shape = s.shape
        steps = PM.get_stepsize(downres_shape, patch_size)
        s = PM.get_patches(s, patch_size = patch_size, steps = steps)
        
        # The dataset now has shape: (ny, nx, py, px). ny, nx are # of patches, and py, px is patch_shape.
        # Reshape this dataset into (n, py, px) where n = ny*nx. Trust numpy to preserve order.
        dataset_shape = s.shape
        s = s.reshape((-1,) + patch_size)
        
        # Predict using the model.
        s = self.model.predict(s[...,np.newaxis])
        s = s[...,0]
        
        s = np.round(s).astype(np.uint8)
        
        f = [measurement(s[idx], **kwargs) for idx in range(len(s))]
        f = np.asarray(f)
        # the shape of f now is n_imgs, n_features
        nimgs, nfeatures = f.shape
        
        s = np.ones((nimgs, patch_size[0], patch_size[1], nfeatures))
        
        for ife in range(nfeatures):
            s[...,ife] = [s[ii,...,ife]*f[ii,ife] for ii in range(len(s))]
        s = np.asarray(s)
        
        
        
        F_img = []
        for ife in range(nfeatures):
            # Now, reshape the data back...
            f_img = s[...,ife].reshape(dataset_shape)
            # Reconstruct from patches...
            f_img = PM.recon_patches(f_img, img_shape = downres_shape, steps = steps)    
            # Finally, resize the images to the original shape of slices
            f_img = cv2.resize(f_img, (orig_shape[1], orig_shape[0]))            
            F_img.append(f_img)
        
        
        return np.asarray(F_img)
Exemplo n.º 2
0
    def seg_image(self, s, max_patches = None, overlap = None):

        """
        Test the segmenter on arbitrary sized 2D image. This method extracts patches of shape same as the input shape of 2D CNN, segments them and stitches them back to form the original image.  
        
        max_patches : tuple  
            (my, mx) are # of patches along Y, X in image  
        
        s : numpy.array  
            greyscale image slice of shape (ny, nx)  
        
        overlap : tuple or int  
            number of overlapping pixels between patches  
        
        """
        # Handle patching parameter inputs
        patch_size = self.model.output_shape[1:-1]    
        if type(max_patches) is not tuple:
            max_patches = (max_patches, max_patches)
            
        if type(overlap) is not tuple:
            overlap = (overlap, overlap)
        overlap = (0 if max_patches[0] == 1 else overlap[0],\
                   0 if max_patches[1] == 1 else overlap[1])
    
        # Resize images
        orig_shape = s.shape
        s = cv2.resize(s, (max_patches[1]*patch_size[1] - overlap[1],\
                           max_patches[0]*patch_size[0] - overlap[0]))
        
        # Make patches
        downres_shape = s.shape
        steps = PM.get_stepsize(downres_shape, patch_size)
        s = PM.get_patches(s, patch_size = patch_size, steps = steps)
        
        # The dataset now has shape: (ny, nx, py, px). ny, nx are # of patches, and py, px is patch_shape.
        # Reshape this dataset into (n, py, px) where n = ny*nx. Trust numpy to preserve order. lol.
        dataset_shape = s.shape
        s = s.reshape((-1,) + patch_size)
        
        # Predict using the model.
        s = self.model.predict(s[...,np.newaxis])
        s = s[...,0]
        
        # Now, reshape the data back...
        s = s.reshape(dataset_shape)
        
        # Reconstruct from patches...
        s = PM.recon_patches(s, img_shape = downres_shape, steps = steps)
        
        # Finally, resize the images to the original shape of slices... This will result in some loss of resolution...
        s = cv2.resize(s, (orig_shape[1], orig_shape[0]))

        # outputs: segmented image of same shape as input image p
        return np.asarray(np.round(s)).astype(np.uint8)  
Exemplo n.º 3
0
def process_data(p,
                 patch_size=None,
                 n_patches=None,
                 skip_fac=None,
                 axis=None,
                 nprocs=None):

    if nprocs is None:
        nprocs = 4
    if p.ndim != 3:
        raise ValueError("Invalid dimensions for 3D data.")

    if type(patch_size) is not tuple:
        patch_size = (patch_size, patch_size)

    if type(n_patches) is not tuple:
        n_patches = (n_patches, n_patches)


#    orig_shape = p[0].shape
    p = np.copy(np.swapaxes(p, 0, axis))[::skip_fac]
    #     print("Input data will be resized...")
    #     print("\tCurrent d shape:" + str(np.shape(p)))

    p = np.asarray([
        cv2.resize(
            p[ii],
            (n_patches[1] * patch_size[1], n_patches[0] * patch_size[0]))
        for ii in range(p.shape[0])
    ])

    # Make patches
    #     print("Making patches...")
    #     print("\tCurrent d shape:" + str(np.shape(p)))
    downres_shape = p[0].shape
    steps = PM.get_stepsize(downres_shape, patch_size)
    p = Parallelize(p,
                    PM.get_patches,
                    procs=nprocs,
                    patch_size=patch_size,
                    steps=steps)
    p = np.asarray(p)

    # The dataset now has shape: (nslices, ny, nx, py, px). ny, nx are # of patches, and py, px is patch_shape.
    # Reshape this dataset into (n, py, px) where n = nslices*ny*nx. Trust numpy to preserve order. lol.
    #    dataset_shape = p.shape
    p = p.reshape((-1, ) + patch_size)
    #     print("Done...")
    #     print("\tCurrent d shape:" + str(np.shape(p)))

    return p
Exemplo n.º 4
0
    def seg_chunk(self, p, max_patches = None, overlap = None,\
                  nprocs = None, arr_split = 1):
        """Segment a volume of shape (nslices, ny, nx). The 2D keras model passes\
        along nslices, segmenting images (ny, nx) with a patch size defined by input \
        to the model  
        
        :param tuple max_patches: (my, mx) are # of patches along Y, X in image (ny, nx)

        :param overlap: number of overlapping pixels between patches  
        
        :type overlap: tuple or int  

        :param int nprocs: number of CPU processors for multiprocessing Pool  
        
        :param int arr_split: breakdown chunk into arr_split number of smaller chunks  
        
        """

        # Handle patching parameter inputs
        patch_size = self.model.output_shape[1:-1]
        if type(max_patches) is not tuple:
            max_patches = (max_patches, max_patches)

        if type(overlap) is not tuple:
            overlap = (overlap, overlap)
        overlap = (0 if max_patches[0] == 1 else overlap[0],\
                   0 if max_patches[1] == 1 else overlap[1])

        # Resize images
        orig_shape = p[0].shape
        p = np.asarray([cv2.resize(p[ii], (max_patches[1]*patch_size[1] - overlap[1],\
                                           max_patches[0]*patch_size[0] - overlap[0]))\
                        for ii in range(p.shape[0])])

        # Make patches
        message("Making patches...")
        message("\tCurrent d shape:" + str(np.shape(p)))
        downres_shape = p[0].shape
        steps = PM.get_stepsize(downres_shape, patch_size)
        p = Parallelize(p, PM.get_patches, procs = nprocs, \
                           patch_size = patch_size, steps = steps)
        p = np.asarray(p)

        # The dataset now has shape: (nslices, ny, nx, py, px),
        # where ny, nx are # of patches, and py, px is patch_shape.
        # Reshape this dataset into (n, py, px) where n = nslices*ny*nx.
        dataset_shape = p.shape
        p = p.reshape((-1, ) + patch_size)

        # Predict using the model.
        message("Running predictions using model...")
        message("\tCurrent d shape:" + str(np.shape(p)))
        p = self.model.predict(p[..., np.newaxis])
        p = p[..., 0]

        # Now, reshape the data back...
        p = p.reshape(dataset_shape)
        p = [p[ii] for ii in range(p.shape[0])]

        # Reconstruct from patches...
        message("Reconstructing from patches...")
        message("\tCurrent d shape:" + str(np.shape(p)))
        p = np.array_split(p, arr_split)

        p = [np.asarray(Parallelize(p[ii], PM.recon_patches,\
                                    img_shape = downres_shape,\
                                    steps = steps, procs = nprocs\
                                   )) for ii in range(arr_split)]
        p = np.concatenate(p, axis=0)

        # Finally, resize the images to the original shape of slices... This will result in some loss of resolution...
        message("Resizing images to original slice size...")
        message("\tCurrent d shape:" + str(np.shape(p)))
        p = np.asarray([cv2.resize(p[ii], (orig_shape[1], orig_shape[0]))\
                        for ii in range(p.shape[0])])
        return np.asarray(np.round(p)).astype(np.uint8)