Python Forum

Full Version: If function is false search next file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am comparing images from a master image to a 100s of others. If its not a match what is best practice to then search the next image. All images are .png.
Manually changing the file name gives me the intended result but I need to check, if match then do something if not match try next image until match found. Things are a little all over while I attempt to do this.
Bare in mind I am no programmer and normally get things working by excessive googling but this one has stumped me.
from skimage.metrics import structural_similarity
import cv2
import numpy

filenames = 'images/a.png'

img00 = cv2.imread('Output.png', 0)
img01 = cv2.imread(filenames, 0)

def orb_sim(img1, img2):
  orb = cv2.ORB_create()
  kp_a, desc_a = orb.detectAndCompute(img1, None)
  kp_b, desc_b = orb.detectAndCompute(img2, None)
  bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
  matches = bf.match(desc_a, desc_b)
  similar_regions = [i for i in matches if i.distance < 80]  
  if len(matches) == 0:
    return 0
  return len(similar_regions) / len(matches)

def structural_sim(img1, img2):
  sim, diff = structural_similarity(img1, img2, full=True)
  return sim

def run():
    ssim = structural_sim(img01, img00)
    print("{0:.2f}".format(ssim))
    if ssim >= 0.50:
        print("Match")
    else:
        print("Fail")
              
run()

Below is a better example
from skimage.metrics import structural_similarity
import cv2

ToCompare = cv2.imread('a.png', 0)
MasterImage = cv2.imread('Output.png', 0)

def structural_sim(imageonem, imagetwoc):
  sim, diff = structural_similarity(imageonem, imagetwoc, full=True)
  return sim

def run():
  ssim = structural_sim(MasterImage, ToCompare)
  print("{0:.2f}".format(ssim))
  if ssim >= 0.50:
    print("Match")
    #do something else
  else:
    print("Fail")
    #if no match the try b.png from images folder
       
run()
I'm not sure that I fully understand the objective, but if I do, then why not simply create a hash value for the images and then compare those?

I say "simply", but then again, I've not tried this, so it could be that doing that, would be more trouble than what you're already doing, or trying to do; it's a thought more than a solution.
I think your question is "How do I compare a bunch of image FILES to see if they match a master image". You aren't asking about how to compare images, but are really wondering how you can do walk through a folder of directory tree and find all the .png files.

The easiest way to do this is glob:

https://docs.python.org/3/library/glob.html

With very few changes you can make your current program check all the png files in the current working directory.
from glob import glob  # Import the glob function

# Your existing image comparison code

# Loop through all image files in folder, printing out names of matching images.
# Should at least print "Output.png", which is a good check to make sure the code works.
original = cv2.imread('Output.png', 0)
for image_file in glob("*.png"):
    if structural_sim(original, cv2.imread(image_file, 0)) >= 0.50:
        print(image_file)