Python Forum
A more efficient way of comparing two images in a python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A more efficient way of comparing two images in a python
#1
I have a task where i need to specify the upper left coordinate of the smaller image in the larger image. I implemented this code, however it is too slow since I have a time limit of 20 seconds, and in some datasets I have 3000 images. How can this be implemented more effectively? I can use numpy, scipy and all packages from the standard python library.

import numpy as np
from PIL import Image

map_image_path = input()
map_image = Image.open(map_image_path)
map_ar = np.asarray(map_image)
map_ar_y, map_ar_x  = map_ar.shape[:2]

i = int(input())
dimensions = input()
patches=list()

for k in range(i):
  patch_image_path = input()
  patches.append(Image.open(patch_image_path))

for j in range(i):
  patch_ar = np.asarray(patches[j])
  patch_ar_y, patch_ar_x = patch_ar.shape[:2]
  stop_x = map_ar_x - patch_ar_x + 1
  stop_y = map_ar_y - patch_ar_y + 1

  for x in range(0, stop_x):
    for y in range(0, stop_y):
      x2 = x + patch_ar_x
      y2 = y + patch_ar_y
      picture = map_ar[y:y2, x:x2]
      if np.array_equal(picture, patch_ar):
        print(str(x) + "," + str(y))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A more efficient code titanif 2 492 Oct-17-2023, 02:07 PM
Last Post: deanhystad
  Cleaning my code to make it more efficient BSDevo 13 1,360 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  Making a function more efficient CatorCanulis 9 1,828 Oct-06-2022, 07:47 AM
Last Post: DPaul
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,799 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  how to read multispectral images on python noorasim 0 1,776 Feb-28-2021, 03:54 PM
Last Post: noorasim
  I there a more efficient way of printing ? Capitaine_Flam 7 3,490 Dec-01-2020, 10:37 AM
Last Post: buran
  How do I insert images in Python using gspread (or any other package)? ivansing23 0 2,263 Jul-27-2020, 01:26 PM
Last Post: ivansing23
  My journey with attaching file instead of images using Python numbnumb54 1 1,774 Jul-24-2020, 02:37 AM
Last Post: scidam
  how to compare two different size images in python and find corresponding pixel value squidsirymchenry 1 4,570 Feb-03-2020, 06:48 AM
Last Post: michael1789
  how to make iterative search more efficient renergy 2 2,266 Jan-03-2020, 03:43 PM
Last Post: stullis

Forum Jump:

User Panel Messages

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