Giter VIP home page Giter VIP logo

Comments (2)

YoniSchirris avatar YoniSchirris commented on September 18, 2024

@lester0866 I had this happen, too.

It happened for an image that was very "white". This led staintools to see each pixel as a 'background pixel'. It then creates a mask, since it only transforms non-background pixels.

This way, you can end up with 0 or 1 pixel that's left as non-background.

If it then tries to compute the eigenvalues of this 'matrix', this is not possible.

My solution was to change macenko_stain_extractor.MacenkoStainExtractor to

tissue_mask = LuminosityThresholdTissueLocator.get_tissue_mask(I, luminosity_threshold=luminosity_threshold).reshape((-1,))
        OD = convert_RGB_to_OD(I).reshape((-1, 3))
        OD = OD[tissue_mask]


        # Yoni's edit!!
        if tissue_mask.sum() <= 1: # If the tissue mask has 0 or 1 pixel the linalg does not work. We will return an untransformed image
            return False

        # Eigenvectors of cov in OD space (orthogonal as cov symmetric)
        _, V = np.linalg.eigh(np.cov(OD, rowvar=False))

And then, in stain_normalizer.StainNormalizer:

def transform(self, I, filename=''):
        """
        Transform an image.

        :param I: Image RGB uint8.
        :return:
        """
        stain_matrix_source = self.extractor.get_stain_matrix(I)

        # Yoni's edit: get_strain_matrix returns false if the linalg doesn't work out. If that doesn't work out, we return an untransformed image
        if isinstance(stain_matrix_source, np.ndarray):
            source_concentrations = get_concentrations(I, stain_matrix_source, multiThread=False)
            maxC_source = np.percentile(source_concentrations, 99, axis=0).reshape((1, 2))
            source_concentrations *= (self.maxC_target / maxC_source)
            tmp = 255 * np.exp(-1 * np.dot(source_concentrations, self.stain_matrix_target))
            return tmp.reshape(I.shape).astype(np.uint8)
        else:
            print(f"=== This image was seen as background by Macenko method, and is therefore not transformed: {filename}")
            return I.astype(np.uint8)

I hope this helps out anyone with these issues

from staintools.

adelabdelli avatar adelabdelli commented on September 18, 2024

@YoniSchirris I did what you suggested but i am having another error, i don't know why !
The error:

Epoch 1/100 /content/src/staintools/staintools/stain_normalizer.py:43: RuntimeWarning: divide by zero encountered in true_divide source_concentrations *= (self.maxC_target / maxC_source) /content/src/staintools/staintools/stain_normalizer.py:43: RuntimeWarning: invalid value encountered in multiply source_concentrations *= (self.maxC_target / maxC_source)

from staintools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.