def run(self, imagename=None):
        if imagename is not None:
            self.load_image(imagename)

        if self.image is None:
            raise ImportError("Image cannot be loaded")

        # Create ilastik dataMgr
        dataMgr = DataMgr()

        di = DataItemImage("")
        di.setDataVol(DataAccessor(self.image))
        dataMgr.append(di, alreadyLoaded=True)
        dataMgr.module["Classification"]["classificationMgr"].classifiers = self.classifiers

        # Create FeatureMgr
        fm = FeatureMgr(dataMgr, self.features)
        fm.prepareCompute(dataMgr)
        fm.triggerCompute()
        fm.joinCompute(dataMgr)

        # Predict with loaded classifier
        classificationPredict = ClassifierPredictThread(dataMgr)
        classificationPredict.start()
        classificationPredict.wait()

        # del dataMgr

        return classificationPredict._prediction[0]
    def run(self, workspace):
        # get input image
        image = workspace.image_set.get_image(self.image_name.value, must_be_color=False)

        # recover raw image domain
        image_ = image.pixel_data
        if image.get_scale() is not None:
            image_ = image_ * image.get_scale()
        else:
            # Best guess for derived images
            image_ = image_ * 255.0
        #
        # Apply a rescaling that's done similarly in ilastik's dataImpex
        #
        image_max = np.max(image_)
        if (image_max > 255) and (image_max < 4096):
            image_ = image_ / 4095.0 * 255.0

        # Create ilastik dataMgr
        dataMgr = DataMgr()

        # Transform input image to ilastik convention s
        # 3D = (time,x,y,z,channel)
        # 2D = (time,1,x,y,channel)
        # Note, this work for 2D images right now. Is there a need for 3D
        image_.shape = (1, 1) + image_.shape

        # Check if image_ has channels, if not add singelton dimension
        if len(image_.shape) == 4:
            image_.shape = image_.shape + (1,)

        # Add data item di to dataMgr
        di = DataItemImage("")
        di.setDataVol(DataAccessor(image_))
        dataMgr.append(di, alreadyLoaded=True)
        dataMgr.module["Classification"]["classificationMgr"].classifiers = self.get_classifiers(workspace)

        # Create FeatureMgr
        fm = FeatureMgr(dataMgr, self.get_feature_items(workspace))

        # Compute features

        fm.prepareCompute(dataMgr)
        fm.triggerCompute()
        fm.joinCompute(dataMgr)

        # Predict with loaded classifier

        classificationPredict = ClassifierPredictThread(dataMgr)
        classificationPredict.start()
        classificationPredict.wait()

        workspace.display_data.source_image = image.pixel_data
        workspace.display_data.dest_images = []
        for group in self.probability_maps:
            # Produce output image and select the probability map
            probMap = classificationPredict._prediction[0][0, 0, :, :, int(group.class_sel.value)]
            temp_image = cpi.Image(probMap, parent_image=image)
            workspace.image_set.add(group.output_image.value, temp_image)
            workspace.display_data.dest_images.append(probMap)
Beispiel #3
0
    def run(self, imagename=None):
        if imagename is not None:
            self.load_image(imagename)

        if self.image is None:
            raise ImportError('Image cannot be loaded')

        # Create ilastik dataMgr
        dataMgr = DataMgr()

        di = DataItemImage('')
        di.setDataVol(DataAccessor(self.image))
        dataMgr.append(di, alreadyLoaded=True)
        dataMgr.module["Classification"][
            "classificationMgr"].classifiers = self.classifiers

        # Create FeatureMgr
        fm = FeatureMgr(dataMgr, self.features)
        fm.prepareCompute(dataMgr)
        fm.triggerCompute()
        fm.joinCompute(dataMgr)

        # Predict with loaded classifier
        classificationPredict = ClassifierPredictThread(dataMgr)
        classificationPredict.start()
        classificationPredict.wait()

        #del dataMgr

        return classificationPredict._prediction[0]
Beispiel #4
0
    def run(self, workspace):
        # get input image
        image = workspace.image_set.get_image(self.image_name.value,
                                              must_be_color=False)

        # recover raw image domain
        image_ = image.pixel_data * image.get_scale()
        #
        # Apply a rescaling that's done similarly in ilastik's dataImpex
        #
        image_max = np.max(image_)
        if (image_max > 255) and (image_max < 4096):
            image_ = image_ / 4095. * 255.0

        # Create ilastik dataMgr
        dataMgr = DataMgr()

        # Transform input image to ilastik convention s
        # 3D = (time,x,y,z,channel)
        # 2D = (time,1,x,y,channel)
        # Note, this work for 2D images right now. Is there a need for 3D
        image_.shape = (1, 1) + image_.shape

        # Check if image_ has channels, if not add singelton dimension
        if len(image_.shape) == 4:
            image_.shape = image_.shape + (1, )

        # Add data item di to dataMgr
        di = DataItemImage('')
        di.setDataVol(DataAccessor(image_))
        dataMgr.append(di, alreadyLoaded=True)
        dataMgr.module["Classification"]["classificationMgr"].classifiers =\
            self.get_classifiers(workspace)

        # Create FeatureMgr
        fm = FeatureMgr(dataMgr, self.get_feature_items(workspace))

        # Compute features

        fm.prepareCompute(dataMgr)
        fm.triggerCompute()
        fm.joinCompute(dataMgr)

        # Predict with loaded classifier

        classificationPredict = ClassifierPredictThread(dataMgr)
        classificationPredict.start()
        classificationPredict.wait()

        workspace.display_data.source_image = image.pixel_data
        workspace.display_data.dest_images = []
        for group in self.probability_maps:
            # Produce output image and select the probability map
            probMap = classificationPredict._prediction[0][
                0, 0, :, :, int(group.class_sel.value)]
            temp_image = cpi.Image(probMap, parent_image=image)
            workspace.image_set.add(group.output_image.value, temp_image)
            workspace.display_data.dest_images.append(probMap)
Beispiel #5
0
    def _predict_image_with_ilastik(self, image_):
        import ilastik
        from ilastik.core.dataMgr import DataMgr, DataItemImage
        from ilastik.modules.classification.core.featureMgr import FeatureMgr
        from ilastik.modules.classification.core.classificationMgr import ClassificationMgr
        from ilastik.modules.classification.core.features.featureBase import FeatureBase
        from ilastik.modules.classification.core.classifiers.classifierRandomForest import ClassifierRandomForest
        from ilastik.modules.classification.core.classificationMgr import ClassifierPredictThread
        from ilastik.core.volume import DataAccessor
        import numpy, h5py

        dataMgr = DataMgr()

        # Transform input image to ilastik convention s
        # 3D = (time,x,y,z,channel)
        # 2D = (time,1,x,y,channel)
        # Note, this work for 2D images right now. Is there a need for 3D
        image_.shape = (1,1) + image_.shape

        # Check if image_ has channels, if not add singelton dimension
        if len(image_.shape) == 4:
            image_.shape = image_.shape + (1,)

        # Add data item di to dataMgr
        di = DataItemImage('')
        di.setDataVol(DataAccessor(image_))
        dataMgr.append(di, alreadyLoaded=True)

        fileName = self.params["ilastik_classifier"]
        ilastik_class = self.params["ilastik_class_selector"]

        hf = h5py.File(fileName,'r')
        temp = hf['classifiers'].keys()
        # If hf is not closed this leads to an error in win64 and mac os x
        hf.close()
        del hf

        classifiers = []
        for cid in temp:
            cidpath = 'classifiers/' + cid
            classifiers.append(ClassifierRandomForest.loadRFfromFile(fileName, str(cidpath)))

        dataMgr.module["Classification"]["classificationMgr"].classifiers = classifiers

        # Restore user selection of feature items from hdf5
        featureItems = []
        f = h5py.File(fileName,'r')
        for fgrp in f['features'].values():
            featureItems.append(FeatureBase.deserialize(fgrp))
        f.close()
        del f
        fm = FeatureMgr(dataMgr, featureItems)



        # Create FeatureMgr


        # Compute features

        fm.prepareCompute(dataMgr)
        fm.triggerCompute()
        fm.joinCompute(dataMgr)

        # Predict with loaded classifier

        classificationPredict = ClassifierPredictThread(dataMgr)
        classificationPredict.start()
        classificationPredict.wait()

        if ilastik_class >= classificationPredict._prediction[0].shape[-1]:
            raise RuntimeError('ilastik output class not valid...')

        # Produce output image and select the probability map
        probMap = (classificationPredict._prediction[0][0,0,:,:, ilastik_class] * 255).astype(numpy.uint8)
        img_out = ccore.numpy_to_image(probMap, True)
        return img_out