dispcompare.py 5.35 KB
Newer Older
sarthou's avatar
sarthou committed
1
2
import numpy as np
import matplotlib.pyplot as plt
3
4
from skimage import io

sarthou's avatar
sarthou committed
5
6
7
MatlabPath = "Data/matlab_debug/"
CppPath = "Data/debug/"

8
9
np.set_printoptions(threshold=np.nan)

sarthou's avatar
sarthou committed
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Attention cette partie est à remplir à la main
########Paramètres#####################
# On choisit ici la frame à observer, attention Matlab est en +1
frame = 27

# On choisit le niveau à observer en indiquant les indices d'itérations
iterLevel = 2 # 0,1 or 2

# On indique quelle direction on veut observer
XYZ = "Z"


def compare(frame,XYZ,MatlabPath,CppPath,iterLevel):
    # Gérer le niveau de la pyramide à observer (attention, valeurs en dur)
    if iterLevel==2:
        indexMatlab = np.arange(101,108)
        indexCpp = np.arange(100,108)
    elif iterLevel==1:
        indexMatlab = np.arange(200,209)
        indexCpp = np.arange(200,217)
    elif iterLevel==0:
        indexMatlab = np.arange(300,314)
        indexCpp = np.arange(300,313)
    else:
        print("Wrong iteration level")


    #Loading Matlab and Cpp Disp files for a frame and a level
    list_matlab = []
    for i in indexMatlab:
        fullpath = MatlabPath+"frame_"+str(frame+1)+"/M_Disp"+XYZ+"_"+str(frame+1)+"_"+str(i)+".csv"
        list_matlab.append(np.genfromtxt(fullpath,delimiter=','))

    list_cpp = []
    for i in indexCpp:
        fullpath = CppPath+"frame_"+str(frame)+"/SaveDisp"+XYZ+"_"+str(frame)+"_"+str(i)+".csv"
        #print(fullpath)
        temp = np.genfromtxt(fullpath,delimiter=';')
        end = np.shape(temp)[1]
        # Get the matrix without NaN
        list_cpp.append(temp[:,0:end-1])

    # Visual comparison
    n,m = len(list_cpp),len(list_matlab)
    p = max(n,m) # Normally, len_matlab = len_cpp

    fig = plt.figure()
    plt.title('Comparaison of '+XYZ+' Displacement matrix in frame '+str(frame) +' over inpainting iterations on level '+str(iterLevel))
    i=0
    while(i<p-1):
        ax0 = fig.add_subplot(n,2,2*i+1)
        if(i<m):
            ax0.imshow(list_matlab[i])
            plt.xlabel('Matlab displacement matrix')
        ax1 = fig.add_subplot(n,2,2*(i+1),sharey=ax0)
        if(i<n):
            ax1.imshow(list_cpp[i])
            plt.xlabel('C++ displacement matrix')
        i+=1

    plt.show()
    return 0


def evolution(path,MorC,nbFrame,XYZ,idIter):
    list_frame = []

    if(MorC=="Cpp"):
        for i in range(nbFrame):
            temp = np.genfromtxt(path+"frame_"+str(i)+"/SaveDisp"+XYZ+"_"+str(i)+"_"+str(idIter)+".csv",delimiter=';')
            end = np.shape(temp)[1]
            # Get the matrix without NaN
            list_frame.append(temp[:,0:end-1])
    elif(MorC=="Matlab"):
        for i in range(nbFrame):
            temp = np.genfromtxt(path+"frame_"+str(i+1)+"/M_Disp"+XYZ+"_"+str(i+1)+"_"+str(idIter)+".csv",delimiter=',')
            list_frame.append(temp)
    else:
        print("langage non reconnu")

    for i in range(nbFrame):
        plt.imsave("Data/debug/imDisp/ImDisp_"+MorC+"_"+XYZ+"_"+str(idIter)+"_F"+str(i),list_frame[i])

    return 0

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def upsample_compare(Mpath,Cpath,nbFrame,XYZ,level,OccMask):

    results=[]
    for i in range(nbFrame):
        temp_matlab = np.genfromtxt(Mpath+"Disp"+XYZ+"_L"+str(level+1)+"_F"+str(i+1)+".csv",delimiter=',')

        temp_cpp = np.genfromtxt(Cpath+"Disp"+XYZ+"_L"+str(level)+"_F"+str(i)+".csv",delimiter=';')
        end = np.shape(temp_cpp)[1]

        diff = temp_matlab-temp_cpp[:,0:end-1]
        # print(diff)
        plt.imsave("Data/upsample/results/Diff_"+XYZ+"_F"+str(i)+".png",diff,cmap="seismic")

        diff[diff!=0]=1
        diff_sum = 0
        diff_total = 0
        for i in range(np.shape(diff)[0]):
            for j in range(np.shape(diff)[1]):
                if(OccMask[i][j]==255):
                    diff_total+=1
                    if(diff[i][j]==1):
                        diff_sum+=1

        # print("diff_sum",diff_sum,"diff_total",diff_total)
        results.append((diff_sum*100)/diff_total)

    plt.plot(results[3:97])
    plt.axhline(np.mean(results[3:97]),color='red')
    plt.xlabel("Frames")
    plt.ylabel("Pourcentage de différence sur le masque")
    plt.title("Evolution de la différence entre les cartes de déplacement établis par les algorithmes d'augmentation \n \
    Direction: "+XYZ+" Avg:"+str(np.mean(results[3:97]))+" Ecart-type: "+str(np.std(results[3:97])))
    plt.show()
    return results



sarthou's avatar
sarthou committed
132
if __name__=="__main__":
133
134
135
136
137
138
139

    OccMediumPath = "Data/divergence/full_div/M_OccMask_F32_L2_I_1.png"

    OccMediumMask = io.imread(OccMediumPath,as_grey=True)
    OccMediumMask[OccMediumMask<0.1]=0
    OccMediumMask[OccMediumMask>=0.1]=255

sarthou's avatar
sarthou committed
140
141
142
    # compare(frame,XYZ,MatlabPath,CppPath,iterLevel)
    # evolution(CppPath,"Cpp",98,"Y",302)

143
144
145
146
147
148
149
150
151
152
153
154
155
156
    # ###Quick Load of occlusion
    # VolOnion = np.zeros((4,17,67))
    # VolOnion[0,:,:]=np.genfromtxt("Data/onion_debug/OccF0_"+str(0)+".csv",delimiter=";")
    # VolOnion[1,:,:]=np.genfromtxt("Data/onion_debug/OccF0_"+str(1)+".csv",delimiter=";")
    # VolOnion[2,:,:]=np.genfromtxt("Data/onion_debug/OccF0_"+str(2)+".csv",delimiter=";")
    # VolOnion[3,:,:]=np.genfromtxt("Data/onion_debug/OccF0_last.csv",delimiter=";")
    # plt.figure()
    # plt.subplot(2,2,1)
    # plt.imshow(VolOnion[1,:,:,])
    # plt.subplot(2,2,2)
    # plt.imshow(VolOnion[2,:,:,])
    # plt.subplot(2,2,3)
    # plt.imshow(VolOnion[3,:,:,])
    # plt.show()
157
    upsample_compare("Data/upsample/matlab_upsample/M_upsampleAfter_","Data/upsample/CppOut_",98,"A",1,OccMediumMask)