Quick start : example 0

Code (graph building from labelling):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np
import skgtimage as skgti

#Initial image
image= np.array([[0, 0, 0, 0, 0, 0, 0],
                  [0, 4, 4, 4, 4, 4, 0],
                  [0, 4, 6, 6, 6, 4, 0],
                  [0, 4, 4, 4, 4, 4, 0],
                  [0, 4, 1, 1, 1, 4, 0],
                  [0, 4, 1, 7, 1, 4, 0],
                  [0, 4, 1, 1, 1, 4, 0],
                  [0, 4, 4, 4, 4, 4, 0],
                  [0, 0, 0, 0, 0, 0, 0]])
#Labelling: several regions depict several connected components
#Region with label 0 correspond to image intensities 0 and 1
#Region with label 2 correspond to image intensities 6 and 7
labelling = np.array([[0, 0, 0, 0, 0, 0, 0],
                  [0, 1, 1, 1, 1, 1, 0],
                  [0, 1, 2, 2, 2, 1, 0],
                  [0, 1, 1, 1, 1, 1, 0],
                  [0, 1, 0, 0, 0, 1, 0],
                  [0, 1, 0, 2, 0, 1, 0],
                  [0, 1, 0, 0, 0, 1, 0],
                  [0, 1, 1, 1, 1, 1, 0],
                  [0, 0, 0, 0, 0, 0, 0]])

#Building both inclusion and photometry graphs
inclusion_graph,photometry_graph=skgti.core.from_labelled_image(image,labelling)
#Retrieval of the new labelling
new_labelling=inclusion_graph.get_labelled()


import matplotlib.pyplot as plt
plt.subplot(131)
plt.title("Image")
plt.imshow(image,interpolation='nearest')
plt.axis('off')
plt.subplot(132)
plt.title("Initial labelling")
plt.imshow(labelling,interpolation='nearest')
plt.axis('off')
plt.subplot(133)
plt.title("Labelling after the discovery \nof inclusion relationships")
plt.imshow(new_labelling,interpolation='nearest')
plt.axis('off')
plt.show()


Output (using matplotlib):

Produced result

Resulting graphs (requires pygraphviz):

Produced result

Quick start : example 1

Code (mean shift segmentation):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import scipy as sp;from scipy.misc import imread,imsave
import skgtimage as skgti;from skgtimage.utils import meanshift,recognize
from skimage.segmentation import mark_boundaries



#A PRIORI KNOWLEDGE
inclusion="text<paper<file" #text is included within paper, itself included into file
photometry="text<file<paper" #text is darker than file, itself darker than paper

#INITIAL IMAGE
image=imread("image_gray.png")

#MEANSHIFT-BASED SEGMENTATION
print("Start segmentation...")
segmentation=meanshift(image, 10,verbose=True)
print("Segmentation finished")

#INTERPRETATION (PROPOSED METHOD)
print("Start recognition...")
id2region,r = recognize(image, segmentation, inclusion, photometry,bg=True)
print("Recognition finished")

#skgti.utils.save_recognizer_details(r,"save/")

#DISPLAY
import matplotlib.pyplot as plt
idplot=141
plt.subplot(idplot)
plt.imshow(mark_boundaries(image, segmentation));plt.title("Initial");plt.axis('off')
for id in id2region:
    idplot+=1
    plt.subplot(idplot);plt.imshow(id2region[id],"gray");plt.title(id);plt.axis('off')
plt.show()

Output (using matplotlib):

Produced result

Inexact graph matching (inclusion - requires pygraphviz):

Produced result

Inexact graph matching (photometry - requires pygraphviz):

Produced result

Quick start : example 2

Code (quick shift segmentation):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import scipy as sp;from scipy.misc import imread,imsave
import skgtimage as skgti;from skgtimage.utils import quickshift,recognize
from skimage.segmentation import mark_boundaries

#A PRIORI KNOWLEDGE
inclusion="text<paper<file" #text is included within paper, itself included into file
photometry="text<file<paper" #text is darker than file, itself darker than paper

#INITIAL IMAGE
image=imread("image_gray.png")

#MEANSHIFT-BASED SEGMENTATION
print("Start segmentation...")
segmentation=quickshift(image,0.7)
print("Segmentation finished")

#INTERPRETATION (PROPOSED METHOD)
print("Start recognition...")
id2region,r = recognize(image, segmentation, inclusion, photometry, bg=True, rag=20)
print("Recognition finished")

#skgti.utils.save_recognizer_details(r,"save/")

#DISPLAY
import matplotlib.pyplot as plt
idplot=141
plt.subplot(idplot)
plt.imshow(mark_boundaries(image, segmentation));plt.title("Initial");plt.axis('off')
for id in id2region:
    idplot+=1
    plt.subplot(idplot);plt.imshow(id2region[id],"gray");plt.title(id);plt.axis('off')
plt.show()

Output (using matplotlib):

Produced result

Inexact graph matching (inclusion - requires pygraphviz):

Produced result

Inexact graph matching (photometry - requires pygraphviz):

Produced result

Quick start : example 3

Code (quick shift segmentation):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import scipy as sp;from scipy.misc import imread,imsave
import skgtimage as skgti;from skgtimage.utils import meanshift,recognize
from skimage.segmentation import mark_boundaries

#A PRIORI KNOWLEDGE
inclusion="2E<2D;2G<F;2D,F,2H,2I<C<B<A;C,1D<B<A;1E,1G<1D;1H<1E"
photometry="B=F=2D=2H=1E=1G<2I=2E=1H=1D<C=2G=A"

#INITIAL IMAGE
image=imread("image_color.png")

#MEANSHIFT-BASED SEGMENTATION
print("Start segmentation...")
segmentation=meanshift(image, 0.1, mc=True, sigma=0.5, rgb_convert=True)
print("Segmentation finished")

#INTERPRETATION (PROPOSED METHOD)
print("Start recognition...")
id2region,r = recognize(image, segmentation, inclusion, photometry,mc=True,bg=True)
print("Recognition finished")

#skgti.utils.save_recognizer_details(r,"save/")

#DISPLAY
import matplotlib.pyplot as plt
idplot=141
plt.subplot(idplot)
plt.imshow(mark_boundaries(image, segmentation));plt.title("Initial");plt.axis('off')
for id in ["1E","2E","F"]:
    idplot+=1
    plt.subplot(idplot);plt.imshow(id2region[id],"gray");plt.title(id);plt.axis('off')
plt.show()

Output (using matplotlib):

Produced result

Inexact graph matching (inclusion - requires pygraphviz):

Produced result

Quick start : example 4

Code (quick shift segmentation):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import scipy as sp;from scipy.misc import imread,imsave
import skgtimage as skgti;from skgtimage.utils import quickshift,recognize
from skimage.segmentation import mark_boundaries

#A PRIORI KNOWLEDGE
inclusion="2E<2D;2G<F;2D,F,2H,2I<C<B<A;C,1D<B<A;1E,1G<1D;1H<1E"
photometry="B=F=2D=2H=1E=1G<2I=2E=1H=1D<C=2G=A"

#INITIAL IMAGE
image=imread("image_color.png")

#MEANSHIFT-BASED SEGMENTATION
print("Start segmentation...")
segmentation=quickshift(image,ratio=0.7,mc=True,verbose=True)
print("Segmentation finished")

#INTERPRETATION (PROPOSED METHOD)
print("Start recognition...")
id2region,r = recognize(image, segmentation, inclusion, photometry, mc=True, bg=True, rag=50, merge=10)
print("Recognition finished")

#skgti.utils.save_recognizer_details(r,"save/")

#DISPLAY
import matplotlib.pyplot as plt
idplot=141
plt.subplot(idplot)
plt.imshow(mark_boundaries(image, segmentation));plt.title("Initial");plt.axis('off')
for id in ["1E","2E","F"]:
    idplot+=1
    plt.subplot(idplot);plt.imshow(id2region[id],"gray");plt.title(id);plt.axis('off')
plt.show()

Output (using matplotlib):

Produced result