L
L
Lord_kazu2021-03-15 18:58:02
Python
Lord_kazu, 2021-03-15 18:58:02

I have a folder with images in png format and I want to compare these images with the standard, I need a loop to run through all the images, how to implement?

#(image name *.png)

import cv2
import difflib
from matplotlib import pyplot as plt

# Hash calculation function
def CalcImageHash(FileName):
image = cv2.imread(FileName) # Read image
resized = cv2.resize(image, (8, 8), interpolation=cv2.INTER_AREA) # Reduce the image
gray_image = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) # Convert to black and white
avg = gray_image.mean() # Average pixel value
ret, threshold_image = cv2.threshold( gray_image, avg, 255, 0) # Threshold binarization

# Calculate hash
_hash = ""
for x in range(8):
for y in range(8):
val = threshold_image[x, y]
if val == 255:
_hash = _hash + "1"
else:
_hash = _hash + "0"

return _hash

def CompareHash(hash1, hash2):
l = len(hash1)
i = 0
count = 0
while i < l:
if hash1[i] != hash2[i]:
count = count + 1
i = i + 1

if(count <= 10):
print(count) # difference
return count

hash1 = CalcImageHash(' depth_data/etalon.png')
hash2 = CalcImageHash('depth_data/25.png') #I need to insert a print(hash1) loop here
print
(hash2)
print(CompareHash(hash1, hash2))

img = plt.imread('depth_data/etalon.png')
img2 = plt.imread('depth_data/25.png')
plt.imshow(img2)

plt.show()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy _, 2021-03-15
@GeneD88

1. Look at the imagehash
module 2. Get all pngs in a folder: glob.glob('/*.png')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question