dispcompare.py 9.84 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
np.set_printoptions(threshold=np.nan)
sarthou's avatar
sarthou committed
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
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
# 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

94
95
def upsample_compare(Mpath,Cpath,nbFrame,XYZ,level,OccMask,outPath,inputMorC,levelBool):
    "This function is to compare two set of data, in .csv matrices, and produce a visual output (in .png)"
96
97

    results=[]
98
99
100
101
    for i in range(1,nbFrame):
        # Add level in the pathway
        if(levelBool==True):
            endPath = "_L"+str(level)+"_F"
102
        else:
103
104
105
106
            endPath = "_F"

        # Load matrices from the first path
        if(inputMorC=='Matlab'):
107
            temp_matlab = np.genfromtxt(Mpath+XYZ+endPath+str(i)+".csv",delimiter=',')
108
109
        else:
            temp_matlab = np.genfromtxt(Mpath+XYZ+endPath+str(i)+".csv",delimiter=';')
110
111
112
            end_m = np.shape(temp_matlab)[1]
            temp_matlab = temp_matlab[:,0:end_m-1]

113
114
115
        # Load matrices for the second path
        endPath = endPath + str(i) + ".csv"
        temp_cpp = np.genfromtxt(Cpath+XYZ+endPath,delimiter=';')
116
117
118
        end = np.shape(temp_cpp)[1]

        diff = temp_matlab-temp_cpp[:,0:end-1]
119

120
121
122
        # if(np.sum(diff)!=0):
        #     print("i:",i)
        #     print(100*diff)
123

124
        plt.imsave(outPath+XYZ+"_F"+str(i)+".png",diff,cmap="seismic")
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

        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")
143
144
    plt.title("Evolution de la différence entre les cartes de déplacement établis \
    par les algorithmes d'augmentation -Nouvelle implémentation \n \
145
146
147
148
149
150
    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
151
if __name__=="__main__":
152
153
154
155
156
157
158

    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
159
160
161
    # compare(frame,XYZ,MatlabPath,CppPath,iterLevel)
    # evolution(CppPath,"Cpp",98,"Y",302)

162
163
164
165
166
167
168
169
170
171
172
173
174
175
    # ###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()
176

177
    # # upsample compare between Matlab and C++ with new implementation
178
    # upsample_compare("Data/upsample/matlab_upsample/M_upsampleAfter_",\
179
    #  "Data/upsample/CppOut_",98,"DispX",1,OccMediumMask,"Data/upsample/results/DiffNewFunc_",'Matlab',True)
180

181
    # # Upsample compare between C++ old and new implementation
182
183
    # upsample_compare("Data/upsample/CppOut_",\
    #  "Data/upsample/test_methodCpp/CppOut_",98,"DispX",1,OccMediumMask,"Data/upsample/results/DiffMethod_",1)
184
    # # Result: Implementation successful, same result in output, the problem is somewhere else.
185

186
    # # Transformer .csv en image
187
188
189
    # for i in range(98):
    #     img = np.genfromtxt("Data/upsample/test_methodCpp/CppOut_DispX_L1_F"+str(i)+".csv",delimiter=";")
    #     plt.imsave("Data/upsample/test_methodCpp/test_DispX_L1_F"+str(i)+".png",img,cmap='seismic')
190

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    # # Displaying Disp and Grad differences for a SR iteration
    # upsample_compare("Data/search_reconstruct/SR_matlab/M_SR_Post_",\
    #  "Data/search_reconstruct/CppOut_SR_",98,"GradY",3,\
    #  OccMediumMask,"Data/search_reconstruct/SR_result/Diff_SR_",0)
    # # Result: Huge difference...

    # # Comparing before and after clamp matrices in Matlab algorithm
    # im1 = np.genfromtxt("Data/upsample_ant/M_DispX_BeforeClamp_F32.csv",delimiter=',')
    # im2 = np.genfromtxt("Data/upsample_ant/M_DispX_AfterClamp_F32.csv",delimiter=',')
    #
    # plt.imsave("Data/upsample_ant/M_DispX_DiffClamp_F32.png",im1-im2)
    # # Result: Exactly the same, clamp unusued

    # #Compare the output of data out of C++ algo, after and before
    # upsample_compare("Data/upsample_ant/M_UA_Post_","Data/upsample_ant/Cpp_UA_",\
    #  98,"DispY",3,OccMediumMask,"Data/upsample_ant/DiffOut_UA_",'Matlab',False)
    # # Result: Exactly the same, there is a problem in the analysis

    # # Compare just two frames of DispA for more details
    # im1 = np.genfromtxt("Data/upsample/matlab_upsample/M_upsampleAfter_DispA_L1_F47.csv",delimiter=',')
    # im2 = np.genfromtxt("Data/upsample/CppOut_DispA_L1_F48.csv",delimiter=';')
    # end = np.shape(im2)[1]
    # plt.imsave("Data/upsample/results/DiffM_NewC_DispA_L1_F48.png",im1-im2[:,0:end-1],cmap='seismic')
    # # Result: Ok there is a problem in the indexation of data...

    # # Upsample compare of DispA to test the indexation problem
    # upsample_compare("Data/upsample/matlab_upsample/M_upsampleAfter_",\
    #  "Data/upsample/test_methodCpp/CppOut_",98,"DispZ",1,OccMediumMask,"Data/upsample/results/NewIndNewImp_",'Matlab',True)
    # # Result: There was indeed an index problem... X,Y and A exactly the same, but problem with the Z, with small peak of differences.

    # # Looking differences between coords of upsample (M vs Cpp) to see the problem on Z
    # for i in range(98):
    #     im1 = np.genfromtxt("Data/upsample_ant/coord/M_CoordZ_F"+str(i)+".csv",delimiter=',')
    #     im2 = np.genfromtxt("Data/upsample_ant/coord/Cpp_UA_CoordZ_F"+str(i)+".csv",delimiter=';')
    #     end = np.shape(im2)[1]
    #     print(np.sum(im1-im2[:,0:end-1]-np.ones(np.shape(im1))))
    #     plt.imsave("Data/upsample_ant/coord/Diff_UA_CoordZ_F"+str(i)+".png",im1-im2[:,0:end-1])
    # # Result: Don't seem to have a difference in the coord matrices between matlab and C++, maybe during clamp ?

230
231
232
233
234
235
    # # Comparing before and after clamp matrices in Matlab algorithm
    # im1 = np.genfromtxt("Data/upsample_ant/M_DispZ_BeforeClamp_F68.csv",delimiter=',')
    # im2 = np.genfromtxt("Data/upsample_ant/M_DispZ_AfterClamp_F68.csv",delimiter=',')
    #
    # plt.imsave("Data/upsample_ant/M_DispZ_DiffClamp_F68.png",im1-im2)
    # # Result: On Z, there is a difference, so clamp might be useful
236

237
238
239
240
    #Compare the output of data out of C++ algo, after and before on DispZ to see if clamp is effective
    upsample_compare("Data/upsample_ant/M_UA_Post_","Data/upsample_ant/Cpp_UA_",\
     98,"DispZ",3,OccMediumMask,"Data/upsample_ant/DiffOut_UA_",'Matlab',False)
    # Result: Implementation succesful but the difference is still here ... WTF