Commit 254de9e7 authored by Nicolas Elie's avatar Nicolas Elie
Browse files

pour model

parent 6fe2b1ab
Loading
Loading
Loading
Loading
+135 −0
Changes for .ipynb_checkpoints/JSON_Qupath_to_ImageLabel-checkpoint.ipynb: 135 added lines, 0 removed lines.
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
from __future__ import print_function
from collections import OrderedDict
from io import BytesIO
import random
import struct
import gzip
import geojson
from shapely.geometry import shape
from shapely.strtree import STRtree
from shapely.geometry import Point
from shapely.geometry import Polygon
from skimage.draw import polygon
import numpy
import multiprocessing as mp
import subprocess, os, skimage,mahotas, cv2
from skimage import io, filters, morphology, util, transform, segmentation, measure,io,feature,exposure
import math
#from skimage.segmentation import clear_border
import pandas as pd
import glob, scipy
import itertools
from skimage.filters.rank import entropy
import javabridge
import bioformats
from xml import etree as et
```

%% Cell type:code id: tags:

``` python
def drawnucleiFinal(SizeImage,allshape):
    """
    SizeImage : size image
    allshape : coordinates of objects
    """
    TotalUnique=numpy.zeros((SizeImage[3],SizeImage[2]),numpy.uint16())

    compteurU=1
    Dictionnaire={}
    for objet in allshape:
        ROI=numpy.zeros((SizeImage[3],SizeImage[2]),numpy.uint8())
        if objet ["geometry"]["type"]=="Polygon":
            # Create a Polygon from the coordinates
            poly = Polygon(objet["geometry"]["coordinates"][0])

            "conversion du polygon en numarray arrondi"
            contour=numpy.uint32(numpy.around(numpy.asarray(poly.exterior.coords)))

            "creation de la matrice de soustraction pour revenir à un depart à 0,0"
            try:
                bc_arr, bc_row_means = numpy.broadcast_arrays(contour, numpy.array([SizeImage[0],SizeImage[1]]))
            except:
                print('erreur')
            polygone=bc_arr-bc_row_means
            "dessin du polygon sur l'image pour faire un masque"
            c,r=numpy.split(polygone,2,axis=1)

            rr, cc = polygon(r.reshape(r.shape[0]), c.reshape(c.shape[0]))
            tab=numpy.where(rr>=SizeImage[3])
            rr[tab]=SizeImage[3]-1

            tab=numpy.where(cc>=SizeImage[2])
            cc[tab]=SizeImage[2]-1

            try:
                ROI[rr,cc]=255
                Tab=numpy.where(ROI==255)
                if objet["properties"]["classification"]["name"]=="sombre":
                    Dictionnaire[compteurU]=1
                    TotalUnique[Tab]=compteurU
                    compteurU+=1

                if objet["properties"]["classification"]["name"]=="clair":
                    Dictionnaire[compteurU]=2
                    TotalUnique[Tab]=compteurU
                    compteurU+=1
            except :
                print("erreur 2")
                pass

    return TotalUnique,Dictionnaire
```

%% Cell type:code id: tags:

``` python
"dossier contenant les contours dessinées sur qupath et exportés en json"
Liste=sorted(glob.glob("Examples/qupathProject/ExportJSON/*.json"))

"dossier des images d'origines"
dossierimage="Examples/"

"parcours des fichiers de contours"
for json_detections in Liste:
        namefile=json_detections[:-5]
        #namefiletif=json_detections[:-5].split("/")[-1]+".tif"
        namefiletif=json_detections[:-19].split("/")[-1]
        with open(json_detections) as g:
                alldetections = geojson.load(g)

        imagecouleur=io.imread(dossierimage+namefiletif)
        "Contour design, labeling and dictionary creation for classification"
        TotalUnique,my_dict=drawnucleiFinal([0,0,1544,1038],alldetections)

        "lecture de l'image couleur d'origine"
        imagecouleur=io.imread(dossierimage+namefiletif)
        nombre=0
        "decoupage de l'image en carré de 256x256"
        "on fait 24 images avec la taille de l'image "
        for x in range(0,6):
                for y in range(0,4):
                        Dictionnaire={}
                        imagcropC=imagecouleur[(7+y*256):(7+(y+1)*256),(4+x*256):(4+(x+1)*256)]
                        imagcropL=TotalUnique[(7+y*256):(7+(y+1)*256),(4+x*256):(4+(x+1)*256)]
                        labelunique=numpy.unique(imagcropL)
                        "sauvegarde du crop couleur et label associés"
                        io.imsave(namefile+"_"+str(nombre)+"_TU_C.tif",imagcropC,check_contrast=False)
                        io.imsave(namefile+"_"+str(nombre)+"_TU_L.tif",imagcropL,check_contrast=False)
                        for l in labelunique[1:]:
                                Dictionnaire[int(l)]=my_dict.get(l)
                        "sauvegarde du dico ave les labels indivs por les images 256*256"
                        tf1 = open(namefile+"_"+str(nombre)+"_TU.json", "w")
                        geojson.dump(Dictionnaire,tf1)
                        tf1.close()
                        nombre+=1

```

%% Cell type:code id: tags:

``` python
```
+110 −0
Changes for JSON_Qupath_to_ImageLabel.ipynb: 110 added lines, 0 removed lines.
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
import geojson
from shapely.geometry import Polygon
from skimage.draw import polygon
import numpy as np
from skimage import io
import glob
```

%% Cell type:code id: tags:

``` python
def drawnucleiFinal(SizeImage,allshape):
    """
    SizeImage : size image
    allshape : coordinates of objects
    """
    TotalUnique=np.zeros((SizeImage[3],SizeImage[2]),np.uint16())

    compteurU=1
    Dictionnaire={}
    for objet in allshape:
        ROI=np.zeros((SizeImage[3],SizeImage[2]),np.uint8())
        if objet ["geometry"]["type"]=="Polygon":
            # Create a Polygon from the coordinates
            poly = Polygon(objet["geometry"]["coordinates"][0])

            "conversion of the polygon into a rounded numarray"
            contour=np.uint32(np.around(np.asarray(poly.exterior.coords)))

            "creation of the subtraction matrix to return to a start at 0,0"
            try:
                bc_arr, bc_row_means = np.broadcast_arrays(contour, np.array([SizeImage[0],SizeImage[1]]))
            except:
                print('erreur')
            polygone=bc_arr-bc_row_means
            "drawing the polygon on the image to make a mask"
            c,r=np.split(polygone,2,axis=1)

            rr, cc = polygon(r.reshape(r.shape[0]), c.reshape(c.shape[0]))
            tab=np.where(rr>=SizeImage[3])
            rr[tab]=SizeImage[3]-1

            tab=np.where(cc>=SizeImage[2])
            cc[tab]=SizeImage[2]-1

            try:
                ROI[rr,cc]=255
                Tab=np.where(ROI==255)
                if objet["properties"]["classification"]["name"]=="sombre":
                    Dictionnaire[compteurU]=1
                    TotalUnique[Tab]=compteurU
                    compteurU+=1

                if objet["properties"]["classification"]["name"]=="clair":
                    Dictionnaire[compteurU]=2
                    TotalUnique[Tab]=compteurU
                    compteurU+=1
            except :
                print("erreur 2")
                pass

    return TotalUnique,Dictionnaire
```

%% Cell type:code id: tags:

``` python
"folder containing outlines drawn on qupath and exported in json"
Liste=sorted(glob.glob("Examples/qupathProject/ExportJSON/*.json"))

"folder of images associated with contours"
dossierimage="Examples/"

"Read JSON files"
for json_detections in Liste:
        namefile=json_detections[:-5]
        #namefiletif=json_detections[:-5].split("/")[-1]+".tif"
        namefiletif=json_detections[:-19].split("/")[-1]
        with open(json_detections) as g:
                alldetections = geojson.load(g)


        "Contour design, labeling and dictionary creation for classification"
        TotalUnique,my_dict=drawnucleiFinal([0,0,1544,1038],alldetections)

        "read the original color image"
        imagecouleur=io.imread(dossierimage+namefiletif)
        nombre=0
        "Split of the image in 24 images of 256x256 pixels for the size of our current image "
        for x in range(0,6):
                for y in range(0,4):
                        Dictionnaire={}
                        imagcropC=imagecouleur[(7+y*256):(7+(y+1)*256),(4+x*256):(4+(x+1)*256)]
                        imagcropL=TotalUnique[(7+y*256):(7+(y+1)*256),(4+x*256):(4+(x+1)*256)]
                        labelunique=np.unique(imagcropL)
                        "saving the color crop image and associated label"
                        io.imsave(namefile+"_"+str(nombre)+"_TU_C.tif",imagcropC,check_contrast=False)
                        io.imsave(namefile+"_"+str(nombre)+"_TU_L.tif",imagcropL,check_contrast=False)
                        for l in labelunique[1:]:
                                Dictionnaire[int(l)]=my_dict.get(l)
                        "save dictionary with individual labels for images of size 256x256"
                        tf1 = open(namefile+"_"+str(nombre)+"_TU.json", "w")
                        geojson.dump(Dictionnaire,tf1)
                        tf1.close()
                        nombre+=1

```
+5 −9
Changes for Model_Build.ipynb: 5 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -742,9 +742,7 @@
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": false
   },
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
@@ -2034,9 +2032,7 @@
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "scrolled": false
   },
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
@@ -2369,7 +2365,7 @@
   "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
@@ -2383,9 +2379,9 @@
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
 "nbformat_minor": 4
}