Beispiel #1
0
def folderimportdeprecated(FolderLoc, extension):
    '''

	'''
    imfilenames = glob.glob(FolderLoc + "/*" + extension)
    numFiles = len(imfilenames)
    testimage = imread2(imfilenames[0])
    dim = testimage.shape
    image_list = np.zeros([numFiles, dim[0], dim[1]])
    for i in range(numFiles):
        image_list[i] = imread2(imfilenames)
def overlay_images(dir1,
                   dir2,
                   out_dir,
                   ftype1='.tif',
                   ftype2='.tif',
                   tint_color=(127 / 255., 255 / 255., 212 / 255.)):
    '''
    Given two directories full of images, load one image from each directory and 
    overlay it on the other with the specified tint and color

    Parameters
    ----------

    dir1 : str
        Path to the directory of images that will be used as the background
    dir2 : str
        Path to the directory of images that will be tinted and overlaid
    out_dir : str
        Path to the directory at which output will be saved
    tint_color : 3-list
        Three values between 0 and 1 specifying a color in RGB

    '''

    # rvals, gvals, bvals = stack2*fullcmap[:, 0], stack2*fullcmap[:, 1], stack2*fullcmap[:, 2]
    #                 stack2 = concatenate([rvals[...,newaxis],gvals[...,newaxis],bvals[...,newaxis]],axis=-1)

    bg_ims = glob.glob(dir1 + '/*' + ftype1)
    fg_ims = glob.glob(dir2 + '/*' + ftype2)

    if len(bg_ims) != len(bg_ims):
        warnings.warn(
            "The two image directories contain different numbers of images.")

    for ind, bg_im in enumerate(bg_ims):

        fg_im = fg_ims[ind]
        im1 = imread2(bg_im)
        im2 = imread2(fg_im)

        # threshold image from one side
        # ones just pass through, anything less than one filters
        im2[im2 < .5] = 1.0

        finim = im1 * im2

        bg_name = os.path.split(bg_im)[-1][:-4]
        fg_name = os.path.split(fg_im)[-1][:-4]
        savestr = out_dir + '/' + bg_name + '_times_' + fg_name + '.png'
        toimage(finim, cmin=0.0, cmax=max(ravel(finim))).save(savestr)
Beispiel #3
0
def overlay_images_dir(dir1, dir2, out_dir='', ftype1='.png',ftype2='.png',alpha_threshold=1.0):
    '''
    Given two directories full of images, load one image from each directory and 
    overlay it on the other, respecting the alpha value for transparency

    Parameters
    ----------

    dir1 : str
        Path to the directory of images that will be used as the background

    dir2 : str
        Path to the directory of images that will be tinted and overlaid

    out_dir : str
        Path to the directory at which output will be saved
        
    alpha_threshold : float
        Determines how much the foreground image takes precedence.
        + If alpha_threshold = 1.0, then the foreground image overlays with
        the expected transparency.
        + If alpha_threshold = 0.0, then all nonzero values of alpha get sent
        to one (fully opaque)

    '''
    
    if not out_dir:
        out_dir = os.path.split(dir1)[0]
    
    bg_ims = glob.glob(os.path.join(dir1,'*'+ftype1))
    bg_ims = sorted(bg_ims , key=sort_by_numberstr)

    fg_ims = glob.glob(os.path.join(dir2,'*'+ftype2))
    fg_ims = sorted(fg_ims , key=sort_by_numberstr)

    if len(bg_ims) != len(fg_ims):
        warnings.warn("The two image directories contain different numbers of images.")

        
    for fg_im, bg_im in zip(fg_ims, bg_ims):

        im1 = imread2(bg_im)
        im2 = imread2(fg_im)
        overlay_im = overlay_images(im1, im2, alpha_threshold=alpha_threshold)
    
        bg_name = os.path.split(bg_im)[-1][:-4]
        fg_name = os.path.split(fg_im)[-1][:-4]
        savestr = os.path.join(out_dir, bg_name+'_times_'+fg_name+'.png')
        overlay_im.save(savestr)
Beispiel #4
0
def omestackimport(FolderPath):
    '''
	Just renaming the imread from skimage to import an image stack
	preferred method, need to include the filename.tif of the first image
	'''
    imfilenames = sorted(glob.glob(FolderPath + "/*.tif"))
    return imread2(imfilenames[0])
def overlay_images(dir1,
                   dir2,
                   out_dir,
                   ftype1='.png',
                   ftype2='.png',
                   bg_color=(153 / 255., 204 / 255., 255 / 255.),
                   fg_color=(255 / 255., 204 / 255., 102 / 255.)):
    '''
    Given two directories full of images, load one image from each directory and 
    overlay it on the other with the specified tint and color

    Parameters
    ----------

    dir1 : str
        Path to the directory of images that will be used as the background
    dir2 : str
        Path to the directory of images that will be tinted and overlaid
    out_dir : str
        Path to the directory at which output will be saved
    bg_color : 3-list
        Three values between 0 and 1 specifying a color in RGB
    fg_color : 3-list
        Three values between 0 and 1 specifying a color in RGB

    '''

    bg_ims = glob.glob(dir1 + '/*' + ftype1).sort()
    fg_ims = glob.glob(dir2 + '/*' + ftype2).sort()

    if len(bg_ims) != len(fg_ims):
        warnings.warn(
            "The two image directories contain different numbers of images.")

    for ind, bg_im in enumerate(bg_ims):

        fg_im = fg_ims[ind]
        im1 = imread2(bg_im)
        if len(im1.shape) == 3:
            im1 = np.sum(im1, axis=2) / 3.

        im2 = imread2(fg_im)
        if len(im2.shape) == 3:
            im2 = np.sum(im2, axis=2) / 3.

        im2_norm = im2.astype(double) / 255.
        im2_mask = im2_norm < .2

        im2_norm[im2_mask] = 0.0
        just_bigger_sizes = im2_norm * im1
        just_bigger_sizes = grey_dilation(just_bigger_sizes, size=(2, 2))

        im2_norm = im2.astype(double) / 255.
        # im2_mask = im2_norm > .2
        im2_norm[~im2_mask] = 0.0
        just_smaller_sizes = im2_norm * im1

        just_smaller_sizes = grey_dilation(just_smaller_sizes, size=(2, 2))

        if ind == 0:
            norm_factor1 = np.max(np.ravel(just_smaller_sizes))
            norm_factor2 = np.max(np.ravel(just_bigger_sizes))
        just_smaller_sizes = (just_smaller_sizes.astype(double) /
                              norm_factor1) * 255
        just_bigger_sizes = (just_bigger_sizes.astype(double) /
                             norm_factor2) * 255

        rgb_bg = np.concatenate([(just_smaller_sizes * chan)[..., np.newaxis]
                                 for chan in bg_color],
                                axis=-1)
        rgb_img = np.concatenate([(just_bigger_sizes * chan)[..., np.newaxis]
                                  for chan in fg_color],
                                 axis=-1)
        finim = rgb_bg + rgb_img

        bg_name = os.path.split(bg_im)[-1][:-4]
        fg_name = os.path.split(fg_im)[-1][:-4]
        savestr = out_dir + '/' + bg_name + '_times_' + fg_name + '.png'

        if ind == 0:
            cmax0 = np.max(np.ravel(finim))
        toimage(finim, cmin=0.0, cmax=cmax0).save(savestr)
def sliding_zproj_internal(frames_list, frames_to_merge, out_dir, **kwargs):
    '''
    This code is optimized for computers that DO NOT have enough 
    RAM to store all the images in memory, but which DO have enough 
    RAM to store an entire z-projection stack in memory
    
    Args:
        frames_list : list of str
            list of image files
        frames_to_merge : int
            The number of frames to merge to form a single streamline image
        out_dir : str
            path to directory where streamline files are saved
        take_diff : bool
            whether to take the difference of consecutive frames 
        diff_order : int
            the order of the difference in frames when take_diff is used
        subtract_median : bool
            For each substack, subtract the median before taking the 
            z projection
        subtract_first : bool
            For each substack, subtract the first frame before taking the 
            z projection
        add_first_frame : bool
            Add the unaltered first frame of the stack back to the stack
            before taking z projection. Makes it possible to view sharp structures
            in median transformed data
        invert_color : bool
            Set to True if working with dark particles on a light background
        color_series : bool
            Color the time traces
    '''

    if 'diff_order' in kwargs:
        diff_order = kwargs['diff_order']
    else:
        diff_order = 1

    image_files = frames_list
    im_path = os.path.split(image_files[0])[:-1][0]

    frame0 = imread2(image_files[0])

    if len(frame0.shape) == 3:
        rgb_flag = True
        frame0 = np.sum(frame0, axis=2) / 3.
    else:
        rgb_flag = False

    # preallocate storage array
    if rgb_flag:
        stack_shape = frame0.shape + (3, ) + (frames_to_merge, )
    else:
        stack_shape = frame0.shape + (frames_to_merge, )
    stack = zeros(stack_shape)

    for ii in range(len(image_files) - frames_to_merge):

        if ii == 0:
            for jj in range(frames_to_merge):
                im = imread2(image_files[jj])
                stack[..., jj] = im
        else:
            if rgb_flag:
                stack = np.roll(stack, -1, axis=3)
            else:
                stack = np.roll(stack, -1, axis=2)
            im = imread2(image_files[ii + frames_to_merge])
            stack[..., -1] = im
        stack2 = stack.copy()

        if 'subtract_first' in kwargs:
            if kwargs['subtract_first']:
                front_im = stack2[..., 0]
                stack2 = stack2 - front_im[..., np.newaxis]

        if 'subtract_median' in kwargs:
            if kwargs['subtract_median']:
                med_im = median(stack2, axis=-1)
                stack2 = stack2 - med_im[..., np.newaxis]

        if 'take_diff' in kwargs:
            if kwargs['take_diff']:
                stack2 = diff(stack2, n=diff_order, axis=-1)

        if 'add_first_frame' in kwargs:
            if kwargs['add_first_frame']:
                # stack2 = dstack([stack2, stack[...,0]])
                stack2 = np.concatenate(
                    (stack2, np.expand_dims(stack[..., 0], axis=-1)), axis=-1)

        if 'color_series' in kwargs:
            if kwargs['color_series']:

                # probably want to preallocate this upstream (255, 204, 153)
                # fullcmap = array(cmap1D((0,250,154),(255,20,147), stack2.shape[-1]))/255.
                fullcmap = np.array(
                    cmap1D((90, 10, 250),
                           (255, 153, 0), stack2.shape[-1])) / 255.

                rvals, gvals, bvals = stack2 * fullcmap[:,
                                                        0], stack2 * fullcmap[:,
                                                                              1], stack2 * fullcmap[:,
                                                                                                    2]
                stack2 = np.concatenate([
                    rvals[..., np.newaxis], gvals[..., np.newaxis],
                    bvals[..., np.newaxis]
                ],
                                        axis=-1)
                stack2 = np.swapaxes(stack2, -1, -2)

        if 'invert_color' in kwargs:
            if 'color_series' in kwargs:
                warnings.warn(
                    "Be careful when enabling time series coloring with images with light backgrounds"
                )
            if kwargs['invert_color']:
                max_proj = stack2.min(axis=-1)
                max_proj = 255 - max_proj

            else:
                max_proj = stack2.max(axis=-1)
        else:
            max_proj = stack2.max(axis=-1)

        im_name = os.path.split(image_files[ii])[-1][:-4]

        savestr = out_dir + '/' + im_name + '_streamlines' + '_frames' + str(
            frames_to_merge) + '.png'

        # This makes it difficult to save with a light background, and it also might cause flickering
        # due to re-normalization
        toimage(max_proj, cmin=0.0,
                cmax=np.max(np.ravel(max_proj))).save(savestr)
Beispiel #7
0
def stackimport(FilePath):
    '''
	Just renaming the imread from skimage to import an image stack
	preferred method, need to include the filename.tif
	'''
    return imread2(FilePath)