compare.py 13.8 KB
Newer Older
sarthou's avatar
sarthou committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
from skimage import measure
import cv2

import scipy.signal

np.set_printoptions(threshold=np.nan)

CppPath = "Data/out_cpp_random/D:\\temp\\finalReconstructedImgF"
MatlabPath = "Data/Out_matlab_random/beach_umbrellainpainted_frame_0"
OccPath = "Data/resources/beach_umbrella_occlusion.png"
OriFilePath = "Data/resources/beach_umbrella_f"
Rand1Path = "Data/random_compare/OneRandImgF"
Rand2Path = "Data/random_compare/ThreeRandImgF"
17
18
Cpp10 = "Data/onion_debug/CppOutF"
Cpp30 = "Data/onion_debug/CppOut30F"
19
20
21
PeelMatlab = "Data/onion_debug/peel_compare/M_EndPeel_F_frame_"
PeelCpp = "Data/onion_debug/peel_compare/Cpp_EndPeel_F"
OccSmallPath = "Data/onion_debug/Occ/OccF0.png"
22
CppLoadingPath = "Data/onion_debug/CppOut_WithMatlabInput_F"
sarthou's avatar
sarthou committed
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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# print(OccMask)
threshold_nbPixDiff = 0.1

list_result = []
list_max = []
# test_cpp = io.imread(CppPath+str(0)+".png")
# test_matlab = io.imread(MatlabPath+"01"+".png")
# print(test_cpp.shape)
# print(test_matlab.shape)
#plt.imshow(test_cpp)
#plt.imshow(test_matlab)
#plt.show()
def rgb2gray(rgb):

    r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
    gray = 0.2989 * r + 0.5870 * g + 0.1140 * b

    return gray

def PixCompare(im1,im2,occMask,threshold):
    #Calculs des différences de canaux
    n,m=im1.shape[0],im1.shape[1]
    assert(im2.shape[0]==occMask.shape[0]==n and im2.shape[1]==occMask.shape[1]==m)

    Rdiff= abs(im1[:,:,0]-im2[:,:,0])/im1[:,:,0]
    Gdiff= abs(im1[:,:,1]-im2[:,:,1])/im1[:,:,1]
    Bdiff= abs(im1[:,:,2]-im2[:,:,2])/im1[:,:,2]

    Rcount,Gcount,Bcount=0,0,0
    MaskCount = 0
    for i in range(n):
        for j in range(m):
            if(occMask[i,j]==255):
                MaskCount = MaskCount +1
                if(Rdiff[i,j]>threshold):
                    Rcount = Rcount + 1
                if(Gdiff[i,j]>threshold):
                    Gcount = Gcount + 1
                if(Bdiff[i,j]>threshold):
                    Bcount = Bcount + 1
    return [Rcount/MaskCount,Gcount/MaskCount,Bcount/MaskCount]

def PSNR(im1,im2,occMask):

    n,m=im1.shape[0],im1.shape[1]
    assert(im2.shape[0]==occMask.shape[0]==n and im2.shape[1]==occMask.shape[1]==m)

    R_PSNR,G_PSNR,B_PSNR = 0,0,0
    MaskCount = 0
    for i in range(n):
        for j in range(m):
            if(occMask[i,j]==255):
                MaskCount = MaskCount +1
                R_PSNR += ((im1[i,j,0]-im2[i,j,0])**2)/255.0
                G_PSNR += ((im1[i,j,1]-im2[i,j,1])**2)/255.0
                B_PSNR += ((im1[i,j,2]-im2[i,j,2])**2)/255.0

    R_PSNR,G_PSNR,B_PSNR = 10*np.log10(R_PSNR/MaskCount),10*np.log10(G_PSNR/MaskCount),10*np.log10(B_PSNR/MaskCount)

    Mean_PSNR = (R_PSNR + G_PSNR + B_PSNR)/3

    return [R_PSNR,G_PSNR,B_PSNR,Mean_PSNR]

def SSIM(imX,imY,C1,C2):
    meanX = np.mean(np.mean(imX,axis=1),axis=0)
    meanY = np.mean(np.mean(imY,axis=1),axis=0)

    stdX = np.std(np.std(imX,axis=1),axis=0)
    stdY = np.std(np.std(imY,axis=1),axis=0)

    stdXY = [np.sum((imX[:,:,0]-meanX[0])*(imY[:,:,0]-meanY[0]))/(np.size(imX[:,:,0]-1)),\
    np.sum((imX[:,:,1]-meanX[1])*(imY[:,:,1]-meanY[1]))/(np.size(imX[:,:,0]-1)),\
    np.sum((imX[:,:,2]-meanX[2])*(imY[:,:,2]-meanY[2]))/(np.size(imX[:,:,0]-1))]

    stdXY = np.array(stdXY)

    return (2*meanX*meanY+C1)*(2*stdXY + C2)/((meanX**2 + meanY**2 + C1)*(stdX**2 + stdY**2 + C2))

def MSSIM(VolX,VolY,C1,C2,WinNb):
    assert(np.shape(VolX)==np.shape(VolY))

    (p,q) = WinNb
    subIm = np.zeros((p,q,98))

    for k in range(np.shape(VolX)[0]):

        imX = VolX[k,:,:,:]
        imY = VolY[k,:,:,:]
        # print(k)
        (n,m) = (np.shape(imX)[0],np.shape(imX)[1])

        r,s = int(np.floor(n/p)),int(np.floor(m/q))


        for i in range(p):
            if(i<p-1):
                for j in range(q-1):
                    temp = SSIM(imX[i*r:(i+1)*r,j*s:(j+1)*s,:],imY[i*r:(i+1)*r,j*s:(j+1)*s,:],C1,C2)
                    subIm[i,j,k] = np.mean(temp)
                    # subImX.append(imX[i*r:(i+1)*r,j*s:(j+1)*s,:])
                    # subImY.append(imY[i*r:(i+1)*r,j*s:(j+1)*s,:])

                subIm[i,q-1,k] = np.mean(SSIM(imX[i*r:(i+1)*r,(q-1)*s:m,:],imY[i*r:(i+1)*r,(q-1)*s:m,:],C1,C2))
                # subImX.append(imX[i*r:(i+1)*r,(q-1)*s:m,:])
                # subImY.append(imY[i*r:(i+1)*r,(q-1)*s:m,:])
            else:
                for j in range(q-1):
                    subIm[i,j,k] = np.mean(SSIM(imX[i*r:(i+1)*r,j*s:(j+1)*s,:],imY[i*r:(i+1)*r,j*s:(j+1)*s,:],C1,C2))
                    # subImX.append(imX[i*r:n,j*s:(j+1)*s,:])
                    # subImY.append(imY[i*r:n,j*s:(j+1)*s,:])

                subIm[i,q-1,k] = np.mean(SSIM(imX[i*r:n,(q-1)*s:m,:],imY[i*r:n,(q-1)*s:m,:],C1,C2))
                # subImX.append(imX[i*r:n,(q-1)*s:m,:])
                # subImY.append(imY[i*r:n,(q-1)*s:m,:])

    fig = plt.figure()
    # fig.title("local evolution of MSSIM Matlab/C++ in time")
    for i in range(p):
        ax0 = fig.add_subplot(p,q,i*q+1)
        ax0.plot(subIm[i,0,:])
        # fig.ylabel("MSSIM Matlab/C++ with random search")
        for j in range(1,q):
            ax1 = fig.add_subplot(p,q,i*q+j+1,sharey=ax0)
            ax1.plot(subIm[i,j,:])
            # fig.xlabel("Time (number of frames)")

    plt.show()
    return 0

def visual_compare(Vol1,Vol2,OutputPath):
153
    VolOutput = np.zeros(np.shape(Vol1))
sarthou's avatar
sarthou committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167

    for i in range(98):
        VolOutput[i,:,:,0] = np.absolute(Vol1[i,:,:,0] - Vol2[i,:,:,0])
        VolOutput[i,:,:,1] = np.absolute(Vol1[i,:,:,1] - Vol2[i,:,:,1])
        VolOutput[i,:,:,2] = np.absolute(Vol1[i,:,:,2] - Vol2[i,:,:,2])

        plt.imsave(OutputPath+str(i),rgb2gray(VolOutput[i,:,:,:]),cmap="binary")

    return VolOutput


if __name__ == '__main__':

    OccMask = io.imread(OccPath)
168
169
170
171
172

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

sarthou's avatar
sarthou committed
173
174
175
176
177
    VolMatlab = np.zeros((98,68,264,3))
    VolCpp = np.zeros((98,68,264,3))
    VolOri = np.zeros((98,68,264,3))
    VolRand1 = np.zeros((98,68,264,3))
    VolRand2 = np.zeros((98,68,264,3))
178
179
    VolCpp10 = np.zeros((98,68,264,3))
    VolCpp30 = np.zeros((98,68,264,3))
180
181
    VolPeelM = np.zeros((98,17,66,3))
    VolPeelC = np.zeros((98,17,66,3))
182
    VolCppLoad = np.zeros((98,68,264,3))
sarthou's avatar
sarthou committed
183
184
185
186
187
188
189
190
    # Chargement des images dans un volume
    for i in range(98):
        if(i<9):
            temp_str= "0"+str(i+1)
        else:
            temp_str= str(i+1)

        img_cpp = io.imread(CppPath+str(i)+".png")
191
        img_matlab = io.imread(MatlabPath+str(i)+".png")
sarthou's avatar
sarthou committed
192
        img_ori = io.imread(OriFilePath+str(i)+".png")
193
194
195
196
197
198
        # img_rand1 = io.imread(Rand1Path+str(i)+".png")
        # img_rand2 = io.imread(Rand2Path+str(i)+".png")
        # img_cpp10 = io.imread(Cpp10+str(i)+".png")
        # img_cpp30 = io.imread(Cpp30+str(i)+".png")
        img_peelM = io.imread(PeelMatlab+str(i)+".png")
        img_peelC = io.imread(PeelCpp+str(i)+".png")
199
        img_load = io.imread(CppLoadingPath+str(i)+".png")
sarthou's avatar
sarthou committed
200
201
202
203
204
205

        # print(img_cpp.shape)

        VolMatlab[i,:,:,:]=img_matlab
        VolCpp[i,:,:,:]=img_cpp
        VolOri[i:,:,:,:]=img_ori
206
207
208
209
210
211
        # VolRand1[i,:,:,:]=img_rand1
        # VolRand2[i,:,:,:]=img_rand2
        # VolCpp10[i,:,:,:]=img_cpp10
        # VolCpp30[i,:,:,:]=img_cpp30
        VolPeelC[i,:,:,:]=img_peelC
        VolPeelM[i,:,:,:]=img_peelM
212
        VolCppLoad[i,:,:,:]=img_load
sarthou's avatar
sarthou committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    # # Code for video
    # cap = cv2.VideoCapture(OriFilePath)
    # i=0
    # while(True):
    #     # Capture frame-by-frame
    #     ret, frame = cap.read()
    #     if ret==True:
    #         cv2.imwrite("MATLAB Inpainting/Content/beach_umbrella_f"+str(i)+".png",frame)
    #     i+=1
    # # When everything done, release the capture
    #
    # cap.release()
    # cv2.destroyAllWindows()


    # #Code de test pour savoir si les images ont bien chargé en uint8
    # imX = VolMatlab[97,:,:,:].astype(np.uint8)
    # imY = io.imread(MatlabPath+"98"+".png")
    # imZ = VolOri[97,:,:,:].astype(np.uint8)

    #MSSIM(VolMatlab,VolOri,0.01,0.03,(3,4))

    # print(SSIM(imX,imY,0.01,0.03))
    # print(test[11,23])
    # print(strange[11,23])
    # plt.subplot(1,2,1)
    # plt.imshow(test)
    # plt.subplot(1,2,2)
    # plt.imshow(imZ)
    # plt.show()

244
    # # Affichage résultat de la comparaison des pixels dans la zone du masque
sarthou's avatar
sarthou committed
245
246
    # pixCountRGB = np.zeros((3,98))
    # for i in range(98):
247
    #     pixCountRGB[:,i]=PixCompare(VolCpp[i,:,:,:],VolCppLoad[i,:,:,:],OccMask,0.1)
sarthou's avatar
sarthou committed
248
249
250
251
252
    #
    # plt.plot(pixCountRGB[0,:],'r', label='Red pixel ratio')
    # plt.plot(pixCountRGB[1,:],'g',label='Green pixel ratio')
    # plt.plot(pixCountRGB[2,:],'b',label='Blue pixel ratio')
    # plt.plot((pixCountRGB[0,:]+pixCountRGB[1,:]+pixCountRGB[2,:])/3,'black',label='Mean pixel ratio')
253
    # # plt.axhline(0.05)
sarthou's avatar
sarthou committed
254
255
256
    # plt.xlabel("number of frame (time)")
    # plt.ylabel("Percentage of pixels different in each image on the mask")
    # plt.legend()
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    # plt.title("Evolution de la différence de pixel entre le code C++ avec et sans initialisation en pelure d'onion")
    # plt.show()

    # # Affichage résultat de la comparaison des pixels dans la zone du masque
    # pixCountCpp = np.zeros((3,98))
    # pixCountLoad = np.zeros((3,98))
    # for i in range(98):
    #     pixCountCpp[:,i]=PixCompare(VolCpp[i,:,:,:],VolMatlab[i,:,:,:],OccMask,0.1)
    #     pixCountLoad[:,i]=PixCompare(VolCppLoad[i,:,:,:],VolMatlab[i,:,:,:],OccMask,0.1)
    #
    # plt.plot((pixCountCpp[0,:]+pixCountCpp[1,:]+pixCountCpp[2,:])/3,'black',label='Cpp with peel init')
    # plt.plot((pixCountLoad[0,:]+pixCountLoad[1,:]+pixCountLoad[2,:])/3,'blue',label='Cpp with matlab data loading')
    # # plt.axhline(0.05)
    # plt.xlabel("number of frame (time)")
    # plt.ylabel("Percentage of pixels different in each image on the mask")
    # plt.legend()
    # plt.title("Evolution de la différence de pixel entre le code C++ avec et sans initialisation en pelure d'onion")
sarthou's avatar
sarthou committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    # plt.show()

    # ## Evolution du PSNR dans la vidéo
    # PSNR_vol = np.zeros((4,98))
    # for i in range(98):
    #     PSNR_vol[:,i]=PSNR(VolRand1[i,:,:,:],VolRand2[i,:,:,:],OccMask)
    #
    # plt.plot(PSNR_vol[0,:],'r', label='Red pixel PSNR')
    # plt.plot(PSNR_vol[1,:],'g',label='Green pixel PSNR')
    # plt.plot(PSNR_vol[2,:],'b',label='Blue pixel PSNR')
    # plt.plot(PSNR_vol[3,:],'black',label='Mean pixel PSNR')
    # plt.ylabel("PSNR on the mask during time")
    # plt.xlabel("number of frame (time)")
    # plt.legend()
    # plt.show()

290
291
292
293
294
295
296
297
298
299
300
301
302
303
    # ## PSNR entre Cpp avec 10,20 et 30 iterations (reference avec Matlab)
    # PSNR_vol = np.zeros((3,98))
    # for i in range(98):
    #     [_,_,_,PSNR_vol[0,i]] = PSNR(VolMatlab[i,:,:,:],VolCpp10[i,:,:,:],OccMask)
    #     [_,_,_,PSNR_vol[1,i]] = PSNR(VolMatlab[i,:,:,:],VolCpp[i,:,:,:],OccMask)
    #     [_,_,_,PSNR_vol[2,i]] = PSNR(VolMatlab[i,:,:,:],VolCpp30[i,:,:,:],OccMask)
    # plt.plot(PSNR_vol[0,:],'r', label='Cpp with 10 search iterations')
    # plt.plot(PSNR_vol[1,:],'g',label='Cpp with 20 search iterations')
    # plt.plot(PSNR_vol[2,:],'b',label='Cpp with 30 search iterations')
    # plt.ylabel("PSNR on the mask during time")
    # plt.xlabel("number of frame (time)")
    # plt.legend()
    # plt.title("Comparaison of mean PSNR over RGB during time for differents search iterations in the C++ algorithm")
    # plt.show()
304

305
306
307
308
309
310
311
312
313
314
315
316
317
    ## PSNR entre Cpp avec init_peel ou en chargeant les données matlab (reference avec Matlab)
    # PSNR_vol = np.zeros((2,98))
    # for i in range(98):
    #     [_,_,_,PSNR_vol[0,i]] = PSNR(VolMatlab[i,:,:,:],VolCpp[i,:,:,:],OccMask)
    #     [_,_,_,PSNR_vol[1,i]] = PSNR(VolMatlab[i,:,:,:],VolCppLoad[i,:,:,:],OccMask)
    # plt.plot(PSNR_vol[0,:],'r', label='Cpp with onion peel init')
    # plt.plot(PSNR_vol[1,:],'g',label='Cpp with loading matlab data')
    # plt.ylabel("PSNR on the mask during time")
    # plt.xlabel("number of frame (time)")
    # plt.legend()
    # plt.title("Comparaison of mean PSNR over RGB during time for C++ algorithm with or without self onion peel initialisation")
    # plt.show()

318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
    # ## Evolution du SSIM dans la vidéo en comparaison CPP 10/20/30 search iter
    # SSIM_cpp10 = np.zeros((3,98))
    # SSIM_cpp =np.zeros((3,98))
    # SSIM_cpp30 = np.zeros((3,98))
    # for i in range(98):
    #     SSIM_cpp10[:,i]=SSIM(VolCpp10[i,:,:,:],VolOri[i,:,:,:],0.01,0.03)
    #     SSIM_cpp[:,i]=SSIM(VolCpp[i,:,:,:],VolOri[i,:,:,:],0.01,0.03)
    #     SSIM_cpp30[:,i]=SSIM(VolCpp30[i,:,:,:],VolOri[i,:,:,:],0.01,0.03)
    #
    #
    # plt.plot((SSIM_cpp10[0,:]+SSIM_cpp10[1,:]+SSIM_cpp10[2,:])/3,'r', label='SSIM Cpp 10 iter')
    # plt.plot((SSIM_cpp[0,:]+SSIM_cpp[1,:]+SSIM_cpp[2,:])/3,'g', label='SSIM Cpp 20 iter')
    # plt.plot((SSIM_cpp30[0,:]+SSIM_cpp30[1,:]+SSIM_cpp30[2,:])/3,'b', label='SSIM Cpp 30 iter')
    #
    #
    #
    # plt.ylabel("SSIM of picture during time")
    # plt.xlabel("number of frame (time)")
    # plt.legend()
    # plt.title("Comparaison of mean MSSIM over RGB during time for differents search iterations in the C++ algorithm")
    # plt.show()


sarthou's avatar
sarthou committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
    # ## Evolution du SSIM dans la vidéo en comparaison MATLAB/CPP
    # SSIM_matlab = np.zeros((3,98))
    # SSIM_cpp =np.zeros((3,98))
    # for i in range(98):
    #     SSIM_matlab[:,i]=SSIM(VolMatlab[i,:,:,:],VolOri[i,:,:,:],0.01,0.03)
    #     SSIM_cpp[:,i]=SSIM(VolCpp[i,:,:,:],VolOri[i,:,:,:],0.01,0.03)
    #
    #
    # plt.plot((SSIM_matlab[0,:]+SSIM_matlab[1,:]+SSIM_matlab[2,:])/3,'r', label='SSIM with Matlab')
    # plt.plot((SSIM_cpp[0,:]+SSIM_cpp[1,:]+SSIM_cpp[2,:])/3,'b', label='SSIM with Cpp')
    # plt.plot(np.abs(((SSIM_matlab[0,:]+SSIM_matlab[1,:]+SSIM_matlab[2,:])/3) - (SSIM_cpp[0,:]+SSIM_cpp[1,:]+SSIM_cpp[2,:])/3),'g',label='Difference of SSIM')
    #
    #
    # plt.ylabel("SSIM of picture during time")
    # plt.xlabel("number of frame (time)")
    # plt.legend()
    # plt.show()

359
    # VolOutComp = visual_compare(VolRand1,VolRand2,"Data/random_compare/RComp")
360
    visual_compare(VolCpp,VolCppLoad,"Data/onion_debug/peel_compare/visual_comp_cpp_matlab_loading/CompLoadF")