def example():
    """Usage example of the main functionalities within this file. """
    from dermoscopic_symmetry.utils import load_dermoscopic, load_segmentation, display_symmetry_axes
    dermoscopic = load_dermoscopic("IMD400")
    segmentation = load_segmentation("IMD400")
    symmetry_info = shape_symmetry(segmentation, angle_step=9)
    print(f'Symmetry info: {symmetry_info}')
    display_symmetry_axes(dermoscopic,
                          segmentation,
                          symmetry_info,
                          title='Shape symmetry')
Ejemplo n.º 2
0
def texture_symmetry_scores(stepAngle=9, filename_to_save='TextureScores'):
    """Create a "textureScores.csv" file containing all texture symmetry scores over angles for each image of the PH2
           Dataset.

    # Arguments :
        stepAngle: Int. The step used to go from 0 to 180 degrees. Each angle permits to score symmetry in the
                    corresponding orientation.
        filename_to_save: String or None.

    # Outputs :
        Scores as a pandas dataframe
    """

    ims, asymCoefs = load_PH2_asymmetry_GT()

    labels = []
    for coef in asymCoefs:
        if coef == 2:
            labels.append(2)
        elif coef == 1:
            labels.append(1)
        else:
            labels.append(0)

    lists = []
    for k in range(int(180 / stepAngle) + 1):
        lists.append([])
    c = 0
    for im in ims:
        print(im, " : ", c + 1, "/200")
        segIm = load_segmentation(im)
        im = load_dermoscopic(im)
        res, ratios = texture_symmetry(im, segIm, stepAngle)
        for k in range(len(ratios)):
            lists[k].append(ratios[k])
        c += 1

    df = pd.DataFrame({"Labels": labels})
    for k in range(int(180 / stepAngle) + 1):
        df["Texture score " + str(k)] = lists[k]

    if filename_to_save:
        df.to_csv(f"{package_path()}/data/{filename_to_save}.csv")

    return df
def example(retrain_model=False, sample_name='IMD400'):
    """Usage example of the main functionalities within this file. """
    img = load_dermoscopic(sample_name)
    segm = load_segmentation(sample_name)

    if retrain_model:
        dataExtractorForTraining(patchesPerImage=10, nbImages=199, nbBins=4)
        clf, acc = classifierTrainer(200)

    else:
        clf = load_model('PatchClassifierModel')

    symmetry_info, ratios = texture_symmetry(img,
                                             segm,
                                             stepAngle=20,
                                             classifier=clf)
    display_symmetry_axes(img, segm, symmetry_info, title='Texture symmetry')

    display_similarity_matches(img,
                               segm,
                               patchSize=32,
                               nbBins=4,
                               classifier=clf,
                               axis_in_degrees=symmetry_info[1][0])
def example():
    im = load_dermoscopic("IMD400")
    segIm = load_segmentation("IMD400")
    patchesUsed, points, reference, data = texture_symmetry_features(
        im, segIm, 32, 4)
Ejemplo n.º 5
0
def datasetCreator(patchesPerImage, patchSize, overlap):
    """Create a dataset of patches. This dataset is composed of similar and non similar pairs of "a" and "b" patches.

            # Arguments :
                patchesPerImage: The amount of patches wanted for each image.
                patchSize:       Int. The size of the patches taken. For example, if `patchSize` = 32, the function takes
                                 32*32 patches.
                overlap:         Int. Similar pairs of patched are created by shifting "a" patch from `overlap` pixels
                                 to have the "b" patch.

            # Outputs :
                Only save the patches into a folder named "patchesDataSet". Then order the patches created into two new
                folders (within the "patchesDataSet" folder) : "Similar" and "nonSimilar".
            """
    os.makedirs(f'{package_path()}/data/patchesDataSet/',
                exist_ok=True)  # Make sure dirs exist.
    df = pd.read_excel(f"{package_path()}/symtab.xlsx")
    ims = df["Image Name"]
    ims = list(ims)

    # ---------------Creation of the "a" patches-----------------
    images = ims
    index = 0
    allPoints = []
    allInorOut = []

    for img in images:
        segIm = load_segmentation(img)

        contour = find_contours(segIm, 0)
        cnt = contour[0]
        minx = min(cnt[:, 1])
        maxx = max(cnt[:, 1])
        maxy = min(cnt[:, 0])
        miny = max(cnt[:, 0])
        segIm = segIm[int(maxy):int(miny), int(minx):int(maxx)]

        im = load_dermoscopic(img)

        imCrop = im[int(maxy):int(miny), int(minx):int(maxx)]

        points, inOrOut = randomPatchForDataset(imCrop, segIm, patchSize,
                                                patchesPerImage, index)
        allPoints += [points]
        allInorOut += [inOrOut]

        index += patchesPerImage

    #---------------Creation of the "b" patches (to have pairs of patches "a" and "b")-----------------
    for countIndex in range(len(images)):

        segIm = load_segmentation(images[countIndex])

        contour = find_contours(segIm, 0)
        cnt = contour[0]
        minx = min(cnt[:, 1])
        maxx = max(cnt[:, 1])
        maxy = min(cnt[:, 0])
        miny = max(cnt[:, 0])
        segIm = segIm[int(maxy):int(miny), int(minx):int(maxx)]

        im = load_dermoscopic(images[countIndex])

        imCrop = im[int(maxy):int(miny), int(minx):int(maxx)]

        pts = allPoints[countIndex]
        ioo = allInorOut[countIndex]

        histoSeg = histogram(segIm)
        numPix = histoSeg[0][-1]

        blk00 = np.zeros(np.shape(segIm))
        blk01 = np.zeros(np.shape(segIm))
        blk10 = np.zeros(np.shape(segIm))
        blk11 = np.zeros(np.shape(segIm))

        k = 0
        for c in range(int(len(pts) / 2)):

            start00 = (pts[c][0] + overlap, pts[c][1])
            start01 = (pts[c][0] - overlap, pts[c][1])
            start10 = (pts[c][0], pts[c][1] + overlap)
            start11 = (pts[c][0], pts[c][1] - overlap)
            extent = (patchSize, patchSize)

            if pts[c][0] + overlap + patchSize < np.shape(imCrop)[0]:
                rr, cc = rectangle(start00, extent=extent)
                blk00[rr, cc] = 1
            if pts[c][0] - overlap > 0:
                rr, cc = rectangle(start01, extent=extent)
                blk01[rr, cc] = 1
            if pts[c][1] + overlap + patchSize < np.shape(imCrop)[1]:
                rr, cc = rectangle(start10, extent=extent)
                blk10[rr, cc] = 1
            if pts[c][1] - overlap > 0:
                rr, cc = rectangle(start11, extent=extent)
                blk11[rr, cc] = 1

            join00 = join_segmentations(blk00, segIm)
            histoJoin00 = histogram(join00)
            join01 = join_segmentations(blk01, segIm)
            histoJoin01 = histogram(join01)
            join10 = join_segmentations(blk10, segIm)
            histoJoin10 = histogram(join10)
            join11 = join_segmentations(blk11, segIm)
            histoJoin11 = histogram(join11)

            if histoJoin00[0][-1] == patchSize * patchSize:
                patch = img_as_ubyte(
                    imCrop[pts[c][0] + overlap:pts[c][0] + overlap + patchSize,
                           pts[c][1]:pts[c][1] + patchSize])
                imsave(
                    f"{package_path()}/data/patchesDataSet/patch" +
                    str(k + countIndex * patchesPerImage) + "b.bmp", patch)
            elif histoJoin01[0][-1] == patchSize * patchSize:
                patch = img_as_ubyte(
                    imCrop[pts[c][0] - overlap:pts[c][0] - overlap + patchSize,
                           pts[c][1]:pts[c][1] + patchSize])
                imsave(
                    f"{package_path()}/data/patchesDataSet/patch" +
                    str(k + countIndex * patchesPerImage) + "b.bmp", patch)
            elif histoJoin10[0][-1] == patchSize * patchSize:
                patch = img_as_ubyte(imCrop[pts[c][0]:pts[c][0] + patchSize,
                                            pts[c][1] + overlap:pts[c][1] +
                                            overlap + patchSize])
                imsave(
                    f"{package_path()}/data/patchesDataSet/patch" +
                    str(k + countIndex * patchesPerImage) + "b.bmp", patch)
            elif histoJoin11[0][-1] == patchSize * patchSize:
                patch = img_as_ubyte(imCrop[pts[c][0]:pts[c][0] + patchSize,
                                            pts[c][1] - overlap:pts[c][1] -
                                            overlap + patchSize])
                imsave(
                    f"{package_path()}/data/patchesDataSet/patch" +
                    str(k + countIndex * patchesPerImage) + "b.bmp", patch)
            else:
                patch = img_as_ubyte(imCrop[pts[c][0]:pts[c][0] + patchSize,
                                            pts[c][1]:pts[c][1] + patchSize])
                imsave(
                    f"{package_path()}/data/patchesDataSet/patch" +
                    str(k + countIndex * patchesPerImage) + "b.bmp", patch)

            blk00[rr, cc] = 0
            blk01[rr, cc] = 0
            blk10[rr, cc] = 0
            blk11[rr, cc] = 0

            k += 1

        for idx in range(int(patchesPerImage / 2), int(len(ioo))):

            indexesZero = []
            indexesOne = []
            coeff = ioo[idx]

            ind = 0

            for digit in ioo:

                if digit == 0:
                    indexesZero.append(ind)

                else:
                    indexesOne.append(ind)
                ind += 1

            # If the "a" patch treated is within the lesion, then the corresponding "b" patch is chosen out of it. If there
            # are only in or only out patches, then take randomly a patch from another image
            #TODO : take care about the random choice in lines 236 and 249 (must have enough patches to make choice, eg : if
            # patchesPerImage=10 and have to make a choice for the 6th patch then you make a random choice in (0;-4) which
            # is impossible.
            if coeff == 1:
                if indexesZero != []:
                    rdInd = choice(indexesZero)
                    rdPt = pts[rdInd]
                    patch = img_as_ubyte(imCrop[rdPt[0]:rdPt[0] + patchSize,
                                                rdPt[1]:rdPt[1] + patchSize])
                    imsave(
                        f"{package_path()}/data/patchesDataSet/patch" +
                        str(idx + countIndex * patchesPerImage) + "b.bmp",
                        patch)
                else:
                    actual = idx + countIndex * patchesPerImage
                    rdIdx = randint(0, actual - patchesPerImage)
                    shutil.copyfile(
                        f"{package_path()}/data/patchesDataSet/patch" +
                        str(rdIdx) + "a.bmp",
                        f"{package_path()}/data/patchesDataSet/patch" +
                        str(actual) + "b.bmp")

            else:
                if indexesOne != []:
                    rdInd = choice(indexesOne)
                    rdPt = pts[rdInd]
                    patch = img_as_ubyte(imCrop[rdPt[0]:rdPt[0] + patchSize,
                                                rdPt[1]:rdPt[1] + patchSize])
                    imsave(
                        f"{package_path()}/data/patchesDataSet/patch" +
                        str(idx + countIndex * patchesPerImage) + "b.bmp",
                        patch)
                else:
                    actual = idx + countIndex * patchesPerImage
                    rdIdx = randint(0, actual - patchesPerImage)
                    shutil.copyfile(
                        f"{package_path()}/data/patchesDataSet/patch" +
                        str(rdIdx) + "a.bmp",
                        f"{package_path()}/data/patchesDataSet/patch" +
                        str(actual) + "b.bmp")

    #---------------Move created pairs of patches to the Similar or nonSimilar folder-----------------
    for compteur in range(0, (len(ims) * patchesPerImage)):
        crit = compteur // int(patchesPerImage / 2)
        if (crit % 2 == 0):
            shutil.move(
                f"{package_path()}/data/patchesDataSet/patch" + str(compteur) +
                "a.bmp",
                f"{package_path()}/data/patchesDataSet/Similar/patch" +
                str(compteur) + "a.bmp")
            shutil.move(
                f"{package_path()}/data/patchesDataSet/patch" + str(compteur) +
                "b.bmp",
                f"{package_path()}/data/patchesDataSet/Similar/patch" +
                str(compteur) + "b.bmp")

        else:
            shutil.move(
                f"{package_path()}/data/patchesDataSet/patch" + str(compteur) +
                "a.bmp",
                f"{package_path()}/data/patchesDataSet/nonSimilar/patch" +
                str(compteur) + "a.bmp")
            shutil.move(
                f"{package_path()}/data/patchesDataSet/patch" + str(compteur) +
                "b.bmp",
                f"{package_path()}/data/patchesDataSet/nonSimilar/patch" +
                str(compteur) + "b.bmp")