spectralpython / spectral

Python module for hyperspectral image processing
MIT License
573 stars 139 forks source link

Possible to append classes to existing TrainingClassSet? #51

Closed shaystrong closed 8 years ago

shaystrong commented 8 years ago

curious about functionality to support appending classes to an previously generated TrainingClassSet. Is this possible in the current master branch? I haven't yet had luck appending anything.

tboggs commented 8 years ago

I haven't done it explicitly myself but....

TrainingClassSet has an add_class method that takes a TrainingClass object as it's argument (TrainingClass can be imported from spectral.algorithms.algorithms). Here is the doc string for its constructor:

    def __init__(self, image, mask, index=0, class_prob=1.0):
        '''Creates a new training class defined by applying `mask` to `image`.

        Arguments:

            `image` (:class:`spectral.Image` or :class:`numpy.ndarray`):

                The `MxNxB` image over which the training class is defined.

            `mask` (:class:`numpy.ndarray`):

                An `MxN` array of integers that specifies which pixels in
                `image` are associated with the class.

            `index` (int) [default 0]:

                if `index` == 0, all nonzero elements of `mask` are associated
                with the class.  If `index` is nonzero, all elements of `mask`
                equal to `index` are associated with the class.

            `class_prob` (float) [default 1.0]:

                Defines the prior probability associated with the class, which
                is used in maximum likelihood classification.  If `classProb`
                is 1.0, prior probabilities are ignored by classifiers, giving
                all class equal weighting.
        '''

So suppose you updated the ground truth mask ("gt") of the image ("image") with a new class having ID of 33. Then you should be able to add the new class to your TrainingClassSet ("training_classes") as follows:

>>> from spectral.algorithms.algorithms import TrainingClass
>>> new_class = TrainingClass(image, gt, 33)
>>> training_classes.add_class(new_class)

Note that if you have already applied a linear transform to "training_classes", you will need to apply the same transform to the new TrainingClass before adding it (TrainingClass has a transform method that accepts a linear transform).

Would that work for you?

shaystrong commented 8 years ago

thanks. This could work. I'll let you know!

tboggs commented 8 years ago

I'll assume this is answered. If not, let me know and I'll re-open.