Commit b3e18325 authored by sarthou's avatar sarthou
Browse files

first commit

parents
.idea/
MATLAB Inpainting/Output/
*.asv
*.mexw64
*.gch
Data/
*.png
*.csv
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"
# 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):
VolOutput = np.zeros((98,68,264,3))
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)
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))
# 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")
img_matlab = io.imread(MatlabPath+temp_str+".png")
img_ori = io.imread(OriFilePath+str(i)+".png")
img_rand1 = io.imread(Rand1Path+str(i)+".png")
img_rand2 = io.imread(Rand2Path+str(i)+".png")
# print(img_cpp.shape)
VolMatlab[i,:,:,:]=img_matlab
VolCpp[i,:,:,:]=img_cpp
VolOri[i:,:,:,:]=img_ori
VolRand1[i,:,:,:]=img_rand1
VolRand2[i,:,:,:]=img_rand2
# # 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()
## Affichage résultat de la comparaison des pixels dans la zone du masque
# pixCountRGB = np.zeros((3,98))
# for i in range(98):
# pixCountRGB[:,i]=PixCompare(VolRand1[i,:,:,:],VolRand2[i,:,:,:],OccMask,0.1)
#
# 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')
# 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 rand() appelée une fois ou 3 fois")
# 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()
# ## 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()
VolOutComp = visual_compare(VolRand1,VolRand2,"Data/random_compare/RComp")
import numpy as np
import matplotlib.pyplot as plt
MatlabPath = "Data/matlab_debug/"
CppPath = "Data/debug/"
# 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
if __name__=="__main__":
# compare(frame,XYZ,MatlabPath,CppPath,iterLevel)
# evolution(CppPath,"Cpp",98,"Y",302)
# Quick Load
VolOnion = np.zeros((3,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=";")
plt.figure()
plt.subplot(2,2,1)
plt.imshow(VolOnion[0,:,:,])
plt.subplot(2,2,2)
plt.imshow(VolOnion[1,:,:,])
plt.subplot(2,2,3)
plt.imshow(VolOnion[2,:,:,])
plt.show()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment