使用python我们需要找到任何类型图像之间的相似之处。
在下面的代码片段中,我们正在读取文件夹中的所有图像并从中提取直方图,并将直方图存储在哈希表中以进行比较。
for imagePath in paths.list_images(imagesfolderpath):
image = cv2.imread(imagePath)
if image is None or len(image)<=112:
print()
else:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
winStride = (8,8)
padding = (8,8)
locations = ((10,20),)
hist = hog.compute(image,winStride,padding,locations)
labels.append(imagePath.split("/")[-2])
if len(hist)>0:
data.append(hist)
hashtable[imagePath] = hist
下面的代码片段用于直方图比较
for key, value in hashtable.iteritems():
count+=1
mydir = os.path.join(outpath, datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S-%f'))
os.makedirs(mydir)
for key1, value1 in hashtable.iteritems():
score = cv2.compareHist(np.array(value, dtype=np.int8),np.array(value1, dtype=np.int8), cv2.HISTCMP_BHATTACHARYYA)
直方图比较时遇到的错误如下:
OpenCV Error: Assertion failed (H1.type() == H2.type() && H1.depth() == CV_32F) in compareHist, file /home/administrator/Desktop/vijay/openface/opencv-3.0.0/modules/imgproc/src/histogram.cpp, line 2281
Traceback (most recent call last):
File "TestHOG.py", line 118, in <module>
score = cv2.compareHist(np.array(value, dtype=np.int8),np.array(value1, dtype=np.int8), cv2.HISTCMP_BHATTACHARYYA)
cv2.error: /home/administrator/Desktop/vijay/openface/opencv-3.0.0/modules/imgproc/src/histogram.cpp:2281: error: (-215) H1.type() == H2.type() && H1.depth() == CV_32F in function compareHist