Check if a set of images match the original one with Opencv and Python
Import the libraries and load Sift and Flann objects
From Line 1 to Line 3 we import the libraries.
We added a new library glob, which we need to read all the files from a specific folder. It’s by default installed in python, so you don’t need to do anything, just import it.
From Line 7 to Line 13 we load the objects Sift and Flann and we detect the Keypoints and descriptors of the original image.
import cv2 import numpy as np import glob original = cv2.imread("original_golden_bridge.jpg") # Sift and Flann sift = cv2.xfeatures2d.SIFT_create() kp_1, desc_1 = sift.detectAndCompute(original, None) index_params = dict(algorithm=0, trees=5) search_params = dict() flann = cv2.FlannBasedMatcher(index_params, search_params)
Load Images and Titles
On Line 16 we create an empty array where we’re going to load later all the images.
On Line 17 we create an empty array where we’re going to load the titles of the images.
On Line 18 we loop trough all the images inside the folder “images”.
On Line 19 we load each image and then we add title and image to the arrays we created before.
# Load all the images all_images_to_compare =  titles =  for f in glob.iglob("images\*"): image = cv2.imread(f) titles.append(f) all_images_to_compare.append(image)
Find similarities and print the result
On Line 23 We loop trough all the images loaded and the titles.
From Line 24 to Line 31 we check if the images are completely equal. If they are, we break the loop, as it’s pointless to check for similarity if we already know that they are equal, and we go to next image.
From Line 35 to Line 53 we check the similarity between the images and we print the result.
for image_to_compare, title in zip(all_images_to_compare, titles): # 1) Check if 2 images are equals if original.shape == image_to_compare.shape: print("The images have same size and channels") difference = cv2.subtract(original, image_to_compare) b, g, r = cv2.split(difference) if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0: print("Similarity: 100% (equal size and channels)") break # 2) Check for similarities between the 2 images kp_2, desc_2 = sift.detectAndCompute(image_to_compare, None) matches = flann.knnMatch(desc_1, desc_2, k=2) good_points =  for m, n in matches: if m.distance > 0.6*n.distance: good_points.append(m) number_keypoints = 0 if len(kp_1) >= len(kp_2): number_keypoints = len(kp_1) else: number_keypoints = len(kp_2) print("Title: " + title) percentage_similarity = len(good_points) / number_keypoints * 100 print("Similarity: " + str(int(percentage_similarity)) + "\n")
This site uses Akismet to reduce spam. Learn how your comment data is processed.
- Train YOLO to detect a custom object (online with free GPU)
- YOLO object detection using Opencv with Python
- Detecting colors (Hsv Color Space) – Opencv with Python
- How to install Python 3 and Opencv 4 on Windows
- How to install Dlib for Python 3 on Windows
- Check if two images are equal with Opencv and Python
- Feature detection (SIFT, SURF, ORB) – OpenCV 3.4 with python 3 Tutorial 25
- Eye motion tracking – Opencv with Python