Пример #1
0
def read_downsample_equalize_mask_uri(impath,
                                      scale,
                                      CLAHE_grid=None,
                                      CLAHE_clip=None):
    # im = cv2.imread(impath[0], 0)
    im = cv2.imdecode(
        np.fromstring(uri_utils.uri_readbytes(impath[0]), np.uint8), 0)

    im = cv2.resize(im, (0, 0),
                    fx=scale,
                    fy=scale,
                    interpolation=cv2.INTER_CUBIC)

    if (CLAHE_grid is not None) & (CLAHE_clip is not None):
        clahe = cv2.createCLAHE(clipLimit=CLAHE_clip,
                                tileGridSize=(CLAHE_grid, CLAHE_grid))
        im = clahe.apply(im)
    else:
        im = cv2.equalizeHist(im)

    if impath[1] is not None:
        mask = cv2.imdecode(
            np.fromstring(uri_utils.uri_readbytes(impath[1]), np.uint8), 0)

        # mask = cv2.imread(impath[1], 0)
        mask = cv2.resize(mask, (0, 0),
                          fx=scale,
                          fy=scale,
                          interpolation=cv2.INTER_CUBIC)
        im = cv2.bitwise_and(im, im, mask=mask)

    return im
Пример #2
0
    def run(self):
        # with open(self.args['metafile'], 'r') as f:
        #     meta = json.load(f)
        meta = json.loads(uri_utils.uri_readbytes(self.args['metafile_uri']))
        roidata = meta[0]['metadata']
        imgdata = meta[1]['data']
        img_coords = {
            img['img_path']: self.image_coords_from_stage(
                img['img_meta']['stage_pos'],
                img['img_meta']['pixel_size_x_move'],
                img['img_meta']['pixel_size_y_move'],
                numpy.radians(img['img_meta']['angle']))
            for img in imgdata
        }

        if not imgdata:
            raise RenderModuleException(
                "No relevant image metadata found for metadata at {}".format(
                    self.args['metafile_uri']))

        minX, minY = numpy.min(numpy.array(list(img_coords.values())), axis=0)
        # assume isotropic pixels
        pixelsize = roidata['calibration']['highmag']['x_nm_per_pix']

        imgdir = self.args.get('image_prefix',
                               uri_utils.uri_prefix(self.args['metafile_uri']))

        tspecs = [
            self.ts_from_imgdata(img,
                                 imgdir,
                                 img_coords[img['img_path']][0] - minX,
                                 img_coords[img['img_path']][1] - minY,
                                 minint=self.args['minimum_intensity'],
                                 maxint=self.args['maximum_intensity'],
                                 width=roidata['camera_info']['width'],
                                 height=roidata['camera_info']['height'],
                                 z=self.zValues[0],
                                 sectionId=self.args.get('sectionId'),
                                 scopeId=roidata['temca_id'],
                                 cameraId=roidata['camera_info']['camera_id'],
                                 pixelsize=pixelsize,
                                 maskUrl=self.args['maskUrl_uri'])
            for img in imgdata
        ]

        self.output_tilespecs_to_stack(tspecs)

        try:
            self.output({'stack': self.output_stack})
        except AttributeError as e:
            self.logger.error(e)
    def run(self):
        self.args['sectionId'] = self.get_sectionId_from_metafile_uri(
            self.args['metafile_uri'])

        if self.args['output_dir'] is None:
            self.args['output_dir'] = tempfile.mkdtemp()

        if self.args['outfile'] is None:
            outfile = tempfile.NamedTemporaryFile(suffix=".json",
                                                  delete=False,
                                                  dir=self.args['output_dir'])
            outfile.close()
            self.args['outfile'] = outfile.name

        out_file = tempfile.NamedTemporaryFile(suffix=".json", delete=False)
        out_file.close()

        args_for_input = dict(self.args)

        metafile = json.loads(
            uri_utils.uri_readbytes(self.args['metafile_uri']))

        self.maskUrl = make_mask(
            self.args['mask_dir'],
            metafile[0]['metadata']['camera_info']['width'],
            metafile[0]['metadata']['camera_info']['height'],
            self.args['mask_coords'],
            mask_file=self.args['mask_file'],
            basename=uri_utils.uri_basename(self.args['metafile_uri']) +
            '.png')

        # argschema doesn't like the NumpyArray after processing it once
        # we don't need it after mask creation
        self.args['mask_coords'] = None
        args_for_input['mask_coords'] = None

        # create a stack with the lens correction tiles
        ts_example = self.generate_ts_example()
        mod = GenerateEMTileSpecsModule(input_data=ts_example,
                                        args=['--output_json', out_file.name])
        mod.run()

        if self.args['rerun_pointmatch']:
            # generate tile pairs for this section in the input stack
            tp_example = self.generate_tilepair_example()
            tp_mod = TilePairClientModule(
                input_data=tp_example, args=['--output_json', out_file.name])
            tp_mod.run()

            with open(tp_mod.args['output_json'], 'r') as f:
                js = json.load(f)
            self.args['pairJson'] = js['tile_pair_file']

            self.logger.setLevel(self.args['log_level'])
            delete_matches_if_exist(self.render, self.args['render']['owner'],
                                    self.args['match_collection'],
                                    self.args['sectionId'])

            args_for_pm = self.get_pm_args()
            pmgen = GeneratePointMatchesOpenCV(
                input_data=args_for_pm, args=['--output_json', out_file.name])
            pmgen.run()

        # load these up in memory to pass to actual solver
        rawtilespecs = renderapi.tilespec.get_tile_specs_from_z(
            ts_example['output_stack'],
            ts_example['z'],
            render=renderapi.connect(**ts_example['render']))
        rawtdict = [t.to_dict() for t in rawtilespecs]
        matches = renderapi.pointmatch.get_matches_within_group(
            self.args['match_collection'],
            self.args['sectionId'],
            render=renderapi.connect(**self.args['render']))

        self.logger.setLevel(self.args['log_level'])

        solver_args = {
            'nvertex': self.args['nvertex'],
            'regularization': self.args['regularization'],
            'good_solve': self.args['good_solve'],
            'tilespecs': rawtdict,
            'matches': matches,
            'output_dir': self.args['output_dir'],
            'outfile': 'resolvedtiles.json.gz',
            'compress_output': False,
            'log_level': self.args['log_level'],
            'timestamp': True
        }

        meshclass = MeshAndSolveTransform(input_data=solver_args, args=[])
        # find the lens correction, write out to new stack
        meshclass.run()
        with open(meshclass.args['output_json'], 'r') as f:
            jout = json.load(f)
        resolved = renderapi.resolvedtiles.ResolvedTiles(
            json=jsongz.load(jout['resolved_tiles']))

        tform_out = os.path.join(self.args['output_dir'],
                                 'lens_correction_out.json')
        with open(tform_out, 'w') as f:
            json.dump(resolved.transforms[0].to_dict(), f)

        try:
            self.output({'output_json': tform_out, 'maskUrl': self.maskUrl})
        except AttributeError as e:
            self.logger.error(e)
Пример #4
0
def create_mipmaps_uri(inputImage,
                       outputDirectory=None,
                       method="block_reduce",
                       mipmaplevels=[1, 2, 3],
                       outputformat='tif',
                       convertTo8bit=True,
                       force_redo=True,
                       **kwargs):
    """function to create downsampled images from an input image

    Parameters
    ==========
    inputImage: str
        path to input image
    outputDirectory: str
        path to save output images (default to current directory)
    mipmaplevels: list or tuple
        list or tuple of integers (default to (1,2,3))
    outputformat: str
        string representation of extension of image format (default tif)
    convertTo8bit: boolean
        whether to convert the image to 8 bit, dividing each value by 255
    force_redo: boolean
        whether to recreate mip map images if they already exist
    method: str
        string corresponding to downsampling method
    block_func: str
        string corresponding to function used by block_reduce
    ds_filter: str
        string corresponding to PIL downsample mode

    Returns
    =======
    list
        list of output images created in order of levels

    Raises
    ======
    MipMapException
        if an image cannot be created for some reason
    """
    # Need to check if the level 0 image exists
    # TODO this is for uri implementation
    inputImagepath = urllib.parse.urlparse(inputImage).path
    im = Image.open(io.BytesIO(uri_utils.uri_readbytes(inputImage)))
    # im = Image.open(inputImage)

    # TODO: proper type checks and then convert
    if convertTo8bit:
        table = [i // 256 for i in range(65536)]
        im = im.convert('I')
        im = im.point(table, 'L')

    levels_uri_map = {
        int(level): uri_utils.uri_join(
            outputDirectory, str(level), '{basename}.{fmt}'.format(
                basename=os.path.basename(inputImagepath), fmt=outputformat))
        for level in mipmaplevels
    }
    # levels_file_map = {int(level): os.path.join(
    #     outputDirectory, str(level), '{basename}.{fmt}'.format(
    #         basename=inputImage.lstrip(os.sep), fmt=outputformat))
    #                    for level in mipmaplevels}
    # levels_uri_map = levels_file_map

    try:
        method_funcs[method](im,
                             levels_uri_map,
                             force_redo=force_redo,
                             **kwargs)
    except KeyError as e:
        raise CreateMipMapException("invalid method {}".format(e))

    return levels_uri_map
 def get_sectionId_from_metafile_uri(metafile_uri):
     j = json.loads(uri_utils.uri_readbytes(metafile_uri))
     sectionId = j[0]['metadata']['grid']
     return sectionId