0
我有一个图像数据集,我创建每个图像的直方图,然后我想将它们存储(写入)到一个文件中,这样对于每个用作输入的新图像,我比较这个图像的直方图和我已经在文件中的直方图,并找出它们是否相同。到目前为止的代码是这样的:比较文件中的多个直方图OpenCV
import numpy as np
import cv2
import os.path
import glob
import matplotlib.pyplot as plt
import pickle
index = {}
#output dic
out = {
1: {},
2: {},
3: {},
}
for t in [1]:
#load_files
files = glob.glob(os.path.join("..", "data", "train", "Type_{}".format(t), "*.jpg"))
no_files = len(files)
#iterate and read
for n, file in enumerate(files):
try:
image = cv2.imread(file)
img = cv2.resize(image, None, fx=0.1, fy=0.1, interpolation=cv2.INTER_AREA)
# features : histograms
plt.hist(img.flatten(), 256, [0, 256], color='r')
plt.xlim([0,256])
plt.legend('histogram', loc='upper left')
plt.show()
# index[file] = hist
# write histograms into file
#compare them and find similarity score
# result_dist = compareHist(index[0], index[1], cv2.cv.CV_COMP_CORREL)
print(file, t, "-files left", no_files - n)
except Exception as e:
print(e)
print(file)
有人可以指导我通过这个吗?谢谢!
感谢您的帮助。它在'filenames = glob.glob(os.path.join(root,obj.folder,extension))'时给了我一个错误',那个名字'obj'没有被定义 – joasa
我编辑了我的答案来修复那个错误 – Tonechas
现在我获取这些警告_UserWarning:可能损坏的EXIF数据。期待读取524288字节,但只有0跳过标记3 “跳过标记%s”%(大小,len(数据),标记))_和此错误消息:** OSError:图像文件被截断(54字节不是处理)**。你知道这是什么意思吗? – joasa