I am trying to compare an image I am taking to an image I already have stored on my computer and return True if they are similar enough. Here is a question that is similar to this.
I am using OpenCV, so using that would be good. My current work around is to use OpenCV to first open the images, then gray scale the images, then blur them, then write them back to files. Then I use Image from PIL and imagehash to compare the hashes of the images before deleting the files. Is there a better way of doing this?
Here's my current code:
def compareImg(imgWarpColor):
img = cv2.imread("data.jpg")
img = cv2.resize(img, (660, 880))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.GaussianBlur(img, (3, 3), 0)
cv2.imwrite("datagray.jpg", img)
grayImgWarped = cv2.cvtColor(imgWarpColor, cv2.COLOR_BGR2GRAY)
blurImg = cv2.GaussianBlur(grayImgWarped, (3, 3), 0)
cv2.imwrite("blurredImage.jpg", blurImg)
hash0 = imagehash.average_hash(Image.open('blurredImage.jpg'))
hash1 = imagehash.average_hash(Image.open('datagray.jpeg'))
cutoff = 5
hashDiff = hash0 - hash1
print(hashDiff)
if hashDiff < cutoff:
print('These images are similar!')
filepath = 'C:\Users\MY NAME\PycharmProjects\projectInQuestion'
os.remove(filepath, 'blurredImage.jpg')
os.remove(filepath, 'datagray.jpg')