Exemple #1
0
def _get_resized_image_stack(flist):
    """ Load all images in a list and resize to OPTIONS['size']

    When files are parsed, the variables are used in an index to provide
    a method to reference a specific file name by its dimensions. This
    function returns the variable index based on the input filename pattern.

    Inputs:ed th
        flist - Paths of list of images to load and resize
    Outputs:
        img_stack - A 3D stack of 2D images
        X - width of image
        Y - height of image
    """

    #Initialize the output
    br = BioReader(str(flist[0]))
    X = br.num_x()
    Y = br.num_y()
    C = len(flist)
    img_stack = np.zeros((OPTIONS['size'], OPTIONS['size'], C),
                         dtype=np.float32)

    # Load every image as a z-slice
    for ind, fname in zip(range(len(flist)), flist):
        br = BioReader(str(fname))
        I = np.squeeze(br.read_image())
        img_stack[:, :, ind] = cv2.resize(
            I, (OPTIONS['size'], OPTIONS['size']),
            interpolation=cv2.INTER_LINEAR).astype(np.float32)
    return img_stack, X, Y
Exemple #2
0
        {% endfor -%}
        {% for inp,val in cookiecutter._inputs|dictsort -%}
        {% for out,n in cookiecutter._outputs|dictsort -%}
        {% if val.type=="collection" and cookiecutter.use_bfio -%}
        # Loop through files in {{ inp }} image collection and process
        for i,f in enumerate({{ inp }}_files):
            # Load an image
            br = BioReader(Path({{ inp }}).joinpath(f))
            image = np.squeeze(br.read_image())

            # initialize the output
            out_image = np.zeros(image.shape,dtype=br._pix['type'])

            """ Do some math and science - you should replace this """
            logger.info('Processing image ({}/{}): {}'.format(i,len({{ inp }}_files),f))
            out_image = awesome_math_and_science_function(image)

            # Write the output
            bw = BioWriter(Path({{ out }}).joinpath(f),metadata=br.read_metadata())
            bw.write_image(np.reshape(out_image,(br.num_y(),br.num_x(),br.num_z(),1,1)))
        {%- endif %}{% endfor %}{% endfor %}
        
    finally:
        {%- if cookiecutter.use_bfio %}
        # Close the javabridge regardless of successful completion
        logger.info('Closing the javabridge')
        jutil.kill_vm()
        {%- endif %}
        
        # Exit the program
        sys.exit()
 read_workers = max([cpu_count()//3,1])
 write_workers = max([cpu_count()-1,2])
 loop_workers = max([3*cpu_count()//4,2])
 
 # extract filenames from registration_string and similar_transformation_string
 registration_set=registration_string.split()
 similar_transformation_set=similar_transformation_string.split()
 
 filename_len=len(template)
     
 # seperate the filename of the moving image from the complete path
 moving_image_name=registration_set[1][-1*filename_len:]
 
 # read and downscale reference image
 br_ref = BioReader(registration_set[0],max_workers=write_workers)
 scale_factor=get_scale_factor(br_ref.num_y(),br_ref.num_x())
 logger.info('Scale factor: {}'.format(scale_factor))
 
 # intialize the scale factor and scale matrix(to be used to upscale the transformation matrices)
 if method == 'Projective':
     scale_matrix = np.array([[1,1,scale_factor],[1,1,scale_factor],[1/scale_factor,1/scale_factor,1]])
 else:
     scale_matrix = np.array([[1/scale_factor,1/scale_factor,1],[1/scale_factor,1/scale_factor,1]])
 
 logger.info('Reading and downscaling reference image: {}'.format(Path(registration_set[0]).name))
 reference_image_downscaled,max_val,min_val = get_scaled_down_images(br_ref,scale_factor,get_max=True)
 br_ref.max_workers = read_workers
 
 # read moving image
 logger.info('Reading and downscaling moving image: {}'.format(Path(registration_set[1]).name))
 br_mov = BioReader(registration_set[1],max_workers=write_workers)
Exemple #4
0
            bw.num_c(len(paths))
            bw._metadata.image().Pixels.channel_count = bw.num_c()

            # Process the data in tiles
            threads = []
            count = 0
            total = bw.num_c() * bw.num_z() * \
                    (bw.num_x()//chunk_size + 1) * (bw.num_y()//chunk_size + 1)
            with ThreadPoolExecutor(cpu_count()) as executor:
                for c, file in enumerate(paths):
                    br = BioReader(file['file'])
                    C = [c]
                    first_image = True
                    for z in range(br.num_z()):
                        Z = [z, z + 1]
                        for x in range(0, br.num_x(), chunk_size):
                            X = [x, min([x + chunk_size, br.num_x()])]
                            for y in range(0, br.num_y(), chunk_size):
                                Y = [y, min([y + chunk_size, br.num_y()])]
                                threads.append(
                                    executor.submit(write_tile, br, bw, X, Y,
                                                    Z, C))

                                # Bioformats requires the first tile to be written
                                # before any other tile is written
                                if first_image or len(threads) == cpu_count():
                                    threads[0].result()
                                    del threads[0]
                                    first_image = False
                                    count += 1
                                    if count % 5 == 0: