Example #1
0
def loadims(afolder):
    folderi = f"Ground truth/{afolder}/image"
    folderm = f"Ground truth/{afolder}/mask"

    images = []
    masks = []
    names = []

    for mask in os.listdir((folderm)):
        names.append(mask)
        maskfile = np.asarray(Image.open(os.path.join(folderm, mask)))
        if afolder == 'AuGe TEM':
            image = mask[:-7] + ".tiff"

            thing = np.zeros_like(maskfile)
            thing[maskfile == 255] = 1
            thing = thing + 1
        else:
            image = mask[:-7] + ".dm4"
            thing = ps.toggle_channels(maskfile[:, :, :3],
                                       colors=['#0000ff', '#ff0000'])

        #warnings.filterwarnings("ignore")
        imagefile = hs.load(os.path.join(folderi, image))
        if afolder == 'PdC TEM' or afolder == 'AuGe TEM':
            im = imagefile.data
            normed = (im - im.min()) * (1000 / (im.max() - im.min()))
            images.append(normed)
        else:
            images.append(imagefile.data)
        masks.append(thing)
    return (images[0:11], masks[0:11])
Example #2
0
def test_man_seg(afolder):

    folderm = f"Ground truth/{afolder}/mask"
    folderm2 = f"Ground truth/{afolder}/mask2 EB"

    masks = []
    masks2 = []
    names = []

    for mask in os.listdir((folderm2)):

        maskfile2 = np.asarray(Image.open(os.path.join(folderm2, mask)))
        if afolder == 'AuGe TEM':
            maskfile1 = np.asarray(
                Image.open(os.path.join(folderm, mask)[:-3] + 'tif'))
            thing = np.zeros_like(maskfile1)
            thing[maskfile1 == 255] = 1
            maskfile1 = thing

        else:
            maskfile1 = np.asarray(Image.open(os.path.join(folderm, mask)))
            maskfile1 = ps.toggle_channels(maskfile1[:, :, :3],
                                           colors=['#0000ff', '#ff0000'])
            maskfile1 = (maskfile1 - 1).astype(np.bool_).astype(np.int)

        maskfile2 = ps.toggle_channels(maskfile2[:, :, :3],
                                       colors=['#0000ff', '#ff0000'])
        maskfile2 = maskfile2 - 1
        masks.append(maskfile1)
        masks2.append(maskfile2)

    data = open(f"Results/manual seg{afolder}.csv", 'w')
    data.write("pixel accuracy,dice01,dice11,dice00,dice10\n")

    for i in range(len(masks)):

        pixacc = ac.check_ground(masks[i], masks2[i])
        dice = ac.dice(masks[i], masks2[i])
        data.write("{},{},{},{},{}\n".format(pixacc, dice[0, 1], dice[1, 1],
                                             dice[0, 0], dice[1, 0]))

    data.close()
    return
Example #3
0
def ClusterPercent(image, labels, classifier, number, params=None):
    #just does one image trained on a random percentage of the image pixels

    if params == None:
        params = ps.trainableParameters()

    if len(labels.shape) != 2:
        labels = ps.toggle_channels(labels)

    #makes sure labels aren't empty
    if (labels != 0).any() == True:
        thin_mask = labels.astype(np.float64)
        image = image.data

        features = ps.CreateFeatures(image, parameters=params)

        area = image.shape[0] * image.shape[1]
        indexes = np.random.randint(0, high=area, size=number)
        frac_array = np.zeros(area)
        frac_array[indexes] = 1
        frac_array = np.reshape(
            frac_array, (image.shape[0], image.shape[1])).astype(np.bool)

        features = np.rot90(np.rot90(features, axes=(2, 0)), axes=(1, 2))
        #features are num/x/ymini

        training_data = features[:, frac_array].T
        #training data is number of labeled pixels by number of features
        training_labels = thin_mask[frac_array].ravel()
        training_labels = training_labels.astype('int')
        #training labels is labelled pixels in 1D array

        tic = time.perf_counter()
        classifier.fit(training_data, training_labels)
        #will crash for one image with no labels
        toc = time.perf_counter() - tic

        thin_mask = labels.astype(np.float64)
        output = np.copy(thin_mask)
        if (labels == 0).any() == True:
            #train classifier on  labelled data
            data = features[:, thin_mask == 0].T
            #unlabelled data
            pred_labels = classifier.predict(data)
            #predict labels for rest of image

            output[thin_mask == 0] = pred_labels
            #adds predicted labels to unlabelled data

    return output, classifier, toc
Example #4
0
def create_smol_features(afolder, num, params):
    #creates set of featues using CS and opens the right folders
    folderi = "Ground truth/{}/image".format(afolder)
    folderm = "Ground truth/{}/mask".format(afolder)

    images = []
    masks = []
    names = []

    for mask in os.listdir((folderm)):
        names.append(mask)
        maskfile = np.asarray(Image.open(os.path.join(folderm, mask)))
        if afolder == 'AuGe TEM':
            image = mask[:-7] + ".tiff"

            thing = np.zeros_like(maskfile)
            thing[maskfile == 255] = 1
            thing = thing + 1
        else:
            image = mask[:-7] + ".dm4"

            thing = ps.toggle_channels(maskfile[:, :, :3],
                                       colors=['#0000ff', '#ff0000'])

        imagefile = hs.load(os.path.join(folderi, image))
        images.append(imagefile.data)
        masks.append(thing)
    mask = masks[0]
    image = images[0]
    features = ps.CreateFeatures(image, params)

    area = image.shape[0] * image.shape[1]
    indexes = np.random.randint(0, high=area, size=num)
    frac_array = np.zeros(area)
    frac_array[indexes] = 1
    frac_array = np.reshape(frac_array,
                            (image.shape[0], image.shape[1])).astype(np.bool)
    features = np.rot90(np.rot90(features, axes=(2, 0)), axes=(1, 2))

    training_data = features[:, frac_array].T

    mask = mask[frac_array]
    mask = mask.ravel()

    labelled_features = np.concatenate(
        (np.reshape(mask, (mask.shape[0], 1)), training_data), axis=1)

    return labelled_features
Example #5
0
def test_classifier_mult(afolder, clf, n=1, parameters=None):

    folderi = f"Ground truth/{afolder}/image"
    folderm = f"Ground truth/{afolder}/mask"

    if parameters == None:
        params = ps.trainableParameters()

    images = []
    masks = []
    names = []

    for mask in os.listdir((folderm)):
        names.append(mask)
        maskfile = np.asarray(Image.open(os.path.join(folderm, mask)))
        if afolder == 'AuGe TEM':
            image = mask[:-7] + ".tiff"

            thing = np.zeros_like(maskfile)
            thing[maskfile == 255] = 1
            thing = thing + 1
        else:
            image = mask[:-7] + ".dm4"

            thing = ps.toggle_channels(maskfile[:, :, :3],
                                       colors=['#0000ff', '#ff0000'])

        imagefile = hs.load(os.path.join(folderi, image))
        import matplotlib.pyplot as plt
        im = imagefile.data
        normed = (im - im.min()) * (1000 / (im.max() - im.min()))

        images.append(normed)
        masks.append(thing)

    data = open(f"results/dicedata{afolder}{str(clf)}.csv", 'w')
    data.write("pixel accuracy,dice01,dice11,dice00,dice10\n")

    tic = time.perf_counter()
    output, clf = ps.ClusterTrained(images[:n],
                                    masks[:n],
                                    clf,
                                    parameters=parameters)
    #output, clf = ps.ClusterTrained(images[:n], masks[:n], clf, membrane = [1,1,1,1,1,1], texture= True, minimum = True,sigma = p[0], high_sigma = p[1], disk_size = p[2])
    toc = time.perf_counter() - tic
    print('trained classifier in {} seconds'.format(toc))
    data.write('{}\n'.format(toc))
    tic = time.perf_counter()

    if n == 1:
        output = [output]
    for i in range(n, len(masks)):
        output.append(
            ps.ClassifierSegment(clf, images[i], parameters=parameters))
        #output.append(ps.ClassifierSegment(clf, images[i], membrane = [1,1,1,1,1,1], texture= True, minimum = True, sigma = p[0], high_sigma = p[1], disk_size = p[2]))

    toc = time.perf_counter() - tic
    data.write('{}\n'.format(toc))
    print('classified images in {} seconds'.format(toc))

    for i in range(len(masks)):

        output[i] = 2 - output[i]
        masks[i] = 2 - masks[i]

        tempp = ps.toggle_channels(2 - output[i])
        maskim = Image.fromarray(tempp)
        maskim.save(f'results/{names[i]}')

        pixacc = ac.check_ground(output[i], masks[i])
        print("accuracy: {}".format(pixacc))
        dice = ac.dice(output[i], masks[i])
        print("dice {} {}\n     {} {}".format(dice[0, 1], dice[1, 1],
                                              dice[0, 0], dice[1, 0]))
        data.write("{},{},{},{},{}\n".format(pixacc, dice[0, 1], dice[1, 1],
                                             dice[0, 0], dice[1, 0]))

    data.close()
    return
Example #6
0
def test_classifier(afolder, clf):

    folderi = f"Ground truth/{afolder}/image"
    folderm = f"Ground truth/{afolder}/mask"

    images = []
    masks = []
    names = []

    for mask in os.listdir((folderm)):
        names.append(mask)
        maskfile = np.asarray(Image.open(os.path.join(folderm, mask)))
        if afolder == 'AuGe TEM':
            image = mask[:-7] + ".tiff"
            imagefile = hs.load(os.path.join(folderi, image))
            images.append(imagefile.data)
            thing = np.zeros_like(maskfile)
            thing[maskfile == 255] = 1
            thing = thing + 1
        else:
            image = mask[:-5] + ".dm4"
            imagefile = hs.load(os.path.join(folderi, image))
            images.append(imagefile.data)
            thing = ps.toggle_channels(maskfile[:, :, :3],
                                       colors=['#0000ff', '#ff0000'])

        masks.append(thing)

    data = open(f"results/dicedata{afolder}{str(clf)}.csv", 'w')

    data.write("pixel accuracy,dice01,dice11,dice00,dice10\n")

    for i in range(len(masks)):

        mk = np.copy(masks[i])
        im = np.copy(images[i])

        if i == 0:
            tic = time.perf_counter()
            _, clf = ps.ClusterTrained(
                im,
                mk,
                clf,
                sigma=1,
                high_sigma=16,
                disk_size=20,
            )
            toc = time.perf_counter() - tic
            print('trained classifier in {} seconds'.format(toc))
            data.write('{}\n'.format(toc))
            tic = time.perf_counter()
        output = ps.ClassifierSegment(
            clf,
            im,
            sigma=10,
            high_sigma=16,
            disk_size=20,
        )
        im = ps.toggle_channels(output)
        mk = 2 - mk
        output = 2 - output

        maskim = Image.fromarray(im)
        maskim.save("ims/{}".format(names[i]))

        print("accuracy: {}".format(ac.check_ground(output, mk)))
        dice = ac.dice(output, mk)
        print("dice {} {}\n     {} {}".format(dice[0, 1], dice[1, 1],
                                              dice[0, 0], dice[1, 0]))
        data.write("{},{},{},{},{}\n".format(ac.check_ground(output, mk),
                                             dice[0, 1], dice[1, 1],
                                             dice[0, 0], dice[1, 0]))
    toc = time.perf_counter() - tic
    data.write('{}\n'.format(toc))
    print('classified images in {} seconds'.format(toc))
    data.close()
    return