Exemplo n.º 1
0
    def updateMasks(self):
        """Binarization of image. """

        self.gaussSigma = np.float(self.main.sigmaEdit.text()) / self.pxSize
        thr = np.float(self.main.intThresEdit.text())

        if self.main.testData:
            self.blocksInputS = [
                ndi.gaussian_filter(b, self.gaussSigma)
                for b in self.blocksInput
            ]
            self.blocksInputS = np.array(self.blocksInputS)
            self.meanS = np.mean(self.blocksInputS, (1, 2))
            self.stdS = np.std(self.blocksInputS, (1, 2))
            thresholds = self.meanS + thr * self.stdS
            thresholds = thresholds.reshape(np.prod(self.n), 1, 1)
            mask = self.blocksInputS < thresholds
            self.blocksMask = np.array([
                bI < np.mean(bI) + thr * np.std(bI) for bI in self.blocksInputS
            ])

            self.mask = tools.unblockshaped(mask, *self.inputData.shape)
            self.inputDataS = tools.unblockshaped(self.blocksInputS,
                                                  *self.shape)
        else:
            self.inputDataS = ndi.gaussian_filter(self.inputData,
                                                  self.gaussSigma)
            self.blocksInputS = tools.blockshaped(self.inputDataS,
                                                  *self.nblocks)
            self.meanS = np.mean(self.inputDataS)
            self.stdS = np.std(self.inputDataS)
            self.mask = self.inputDataS < self.meanS + thr * self.stdS
            self.blocksMask = tools.blockshaped(self.mask, *self.nblocks)

        self.showImS = np.fliplr(np.transpose(self.inputDataS))
        self.showMask = np.fliplr(np.transpose(self.mask))

        self.selectedMask = self.roi.getArrayRegion(self.showMask,
                                                    self.inputImg).astype(bool)
Exemplo n.º 2
0
def buildData(technique=None, pxSize=None, mag=None):

    print("Test image creation script started...")

    if technique is None:
        te = input('STED or STORM? STED=1, STORM=2 ')
        if te == '1':
            technique = 'STED'

        elif te == '2':
            technique = 'STORM'
            mag = float(input('Enter super-resolved image magnification '))

        else:
            print('Invalid answer')

    pxSize = float(input('Enter px size in nm '))
    subimgPxSize = int(np.round(1000/pxSize))
    folder = os.getcwd()
    nRings = 0
    nNoRings = 0

    try:
        fig, ax = plt.subplots()
        fig.set_size_inches(12, 16, forward=True)
        loadOutput = loadData(os.getcwd(), ax, subimgPxSize, technique, mag)
        folder, blocks, dataShape, fileShape, n = loadOutput
        plt.show(block=False)

        # this array will be the output
        testData = np.zeros(blocks.shape, dtype=blocks.dtype)
        maxRings = int(0.5*len(blocks))
        maxNoRings = maxRings

        listRings, listNoRings = selectBlocks(maxRings, maxRings)
        lR = len(listRings)
        testData[nRings:nRings + lR] = blocks[listRings]
        nRings += lR
        lNR = len(listNoRings)
        nRBlocks = blocks[listNoRings]
        testData[maxRings + nNoRings:maxRings + nNoRings + lNR] = nRBlocks
        nNoRings += lNR

        plt.close()

        ans = ''
        while ans not in ['y', 'n']:
            ans = input('Keep working? [y/n] ')

        keepWorking = ans == 'y'

    except OSError:
        keepWorking = False

    while keepWorking:

        try:
            fig, ax = plt.subplots()
            fig.set_size_inches(12, 18, forward=True)
            folder, blocks, dd, ee, ff = loadData(folder, ax, subimgPxSize,
                                              technique, mag)
            plt.show(block=False)

            needRings = maxRings - nRings
            needNoRings = maxNoRings - nNoRings
            listRings, listNoRings = selectBlocks(needRings, needNoRings)
            listRings = listRings[:needRings]
            listNoRings = listNoRings[:needNoRings]

            lR = len(listRings)
            testData[nRings:nRings + lR] = blocks[listRings]
            nRings += lR
            lNR = len(listNoRings)
            nRBlocks = blocks[listNoRings]
            testData[maxRings + nNoRings:maxRings + nNoRings + lNR] = nRBlocks
            nNoRings += lNR

            plt.close()
            ans = ''
            while ans not in ['y', 'n']:
                ans = input('Keep working? [y/n] ')

            keepWorking = ans == 'y'

        except OSError:
            print('No file selected!')

    testData = tools.unblockshaped(testData, *dataShape)

    if technique == 'STORM':
        crop = int(3*mag)
        newTestData = np.zeros(fileShape, testData.dtype)
        newTestData[crop:crop + n[0]*subimgPxSize,
                    crop:crop + n[1]*subimgPxSize] = testData
        plotWithGrid(testData, ax, subimgPxSize)
        plt.show()
        tiff.imsave('testdata.tif', newTestData)

    else:
        plotWithGrid(testData, ax, subimgPxSize)
        plt.show()
        tiff.imsave('testdata.tif', testData)