In [24]:
import re
#from skimage.measure import structural_similarity as ssim
import subprocess
import cv2
from os import system
from os import walk
import numpy as np
def tryint(s):
try:
return int(s)
except:
return s
def alphanum_key(s):
""" Turn a string into a list of string and number chunks.
"z23a" -> ["z", 23, "a"]
"""
return [ tryint(c) for c in re.split('([0-9]+)', s) ]
def sort_nicely(l):
""" Sort the given list in the way that humans expect.
"""
l.sort(key=alphanum_key)
def mse(imageA, imageB):
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
return err
folder = "01DP"
brdfs = ["nickel", "blue-metallic", "plastic"]
for brdf in brdfs:
f = []
path = folder+"/exr/"+brdf+"/"
path2 = folder+"/diff3/"+brdf+"/"
original = "originals/"+brdf+".exr"
for (dirpath, dirnames, filenames) in walk(path):
f.extend(filenames)
break
sort_nicely(f)
diffFile = folder+"/results/"+brdf+"/compareFileOriginalCV2.txt"
with open(diffFile, "w") as fout:
for fname in f:
out = path2 + str(fname[:-3]) + "jpg"
orgImg = cv2.imread(original)
othImg = cv2.imread(path+fname)
outImg = abs(orgImg - othImg)
cv2.imwrite(out, outImg)
m = mse(orgImg, othImg)
# s = ssim(imageA, imageB)
#output = "mse:" + "{0:.5f}".format(mPDist)
output = "mse:" + str(m) + "\n"
#output += "ssim:" + str(s) + "\n"
fout.write(fname+"\n")
fout.write(output+"\n\n")
print 'Done'
In [ ]:
In [ ]: