Python Forum
If function is false search next file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If function is false search next file
#1
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()
Yoriz write Sep-03-2022, 03:43 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Excel File with a list of values huzzug 4 1,148 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,257 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  search file by regex SamLiu 1 860 Feb-23-2023, 01:19 PM
Last Post: deanhystad
  fuzzywuzzy search string in text file marfer 9 4,434 Aug-03-2021, 02:41 AM
Last Post: deanhystad
  Cloning a directory and using a .CSV file as a reference to search and replace bg25lam 2 2,101 May-31-2021, 07:00 AM
Last Post: bowlofred
  search of a curve fitting function bluffy5 2 2,377 Dec-13-2020, 09:53 AM
Last Post: ndc85430
  PyAD search function adquery SpongeB0B 2 8,705 Sep-21-2020, 04:29 AM
Last Post: SpongeB0B
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,187 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  search binary file and list all founded keyword offset Pyguys 4 2,701 Mar-17-2020, 06:46 AM
Last Post: Pyguys
  Is there a way to search for function call? mtran 2 2,222 Jan-14-2020, 02:07 AM
Last Post: mtran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020