【发布时间】:2021-06-11 13:13:48
【问题描述】:
def SSIM_compute(files,WorkingFolder,DestinationAlikeFolder,DestinationUniqueFolder,results, start_time):
NumberAlike=0
loop=1
while True:
files=os.listdir(WorkingFolder)
if files==[]:
break
IsAlike=False
CountAlike=1
print("Loop : "+str(loop)+" --- starttime : "+str(time.time()-start_time))
for i in range (1,len(files)):
#print("\ti= "+str(i)+" : "+str(time.time()-start_time))
img1=cv2.imread(WorkingFolder+"/"+files[0])
img2=cv2.imread(WorkingFolder+"/"+files[i])
x1,y1=img1.shape[:2]
x2,y2=img2.shape[:2]
x=min(x1,x2)
y=min(y1,y2)
img1=cv2.resize(img1,(x,y),1)
img2=cv2.resize(img2,(x,y),1)
threshold=ssim(img1,img2,multichannel=True)
if threshold>0.8:
IsAlike=True
if os.path.exists((WorkingFolder+"/"+files[i])):
shutil.move((WorkingFolder+"/"+files[i]),DestinationAlikeFolder+"/alike"+str(NumberAlike)+"_"+str(CountAlike)+".jpg")
CountAlike+=1
#results.write("ALIKE : " +files[0] +" --- " +files[i]+"\n")
results.write("ALIKE : /alike"+str(NumberAlike)+"_0"+".jpg --- /alike"+str(NumberAlike)+"_"+str(CountAlike)+".jpg -> "+str(threshold))
if IsAlike:
if os.path.exists((WorkingFolder+"/"+files[0])):
shutil.move((WorkingFolder+"/"+files[0]),DestinationAlikeFolder+"/alike"+str(NumberAlike)+"_0"+".jpg")
NumberAlike+=1
else :
if os.path.exists((WorkingFolder+"/"+files[0])):
shutil.move((WorkingFolder+"/"+files[0]),DestinationUniqueFolder)
loop+=1
我有这段代码,它必须比较图像以确定它们是否相同或其中一些是否被修改(压缩、伪像等)。
因此,为了检查两个图像是否严格相似,我只需计算并比较它们各自的哈希值(在此处未显示的另一个函数中),并检查它们是否相似,我计算这两个文件的 SSIM。
下一部分是麻烦开始的地方:当我在一组安静的小图片(大约 50 张)上测试这段代码时,执行时间还不错,但是如果我将图片集更大(大约 200 张图片),考虑到我有两个重叠的 for 循环,执行时间变得像预期的那样太长(几个小时)。
由于我不是很有创意,有没有人想在更大的数据集上减少执行时间?也许是一种避免那些重叠循环的方法?
感谢您提供的任何帮助:)
【问题讨论】:
标签: python optimization execution-time