1

I am trying to find a smaller image in a larger image. I have it done using PIL, but I am looking for ways to speed it up so I am currently looking for some numpy solutions. So far I have gotten to: loading both RGB images to np.arrays

rows, cols = np.where(np.all(haystack == needle[0,0], axis=-1))
for row, col in zip(rows, cols):
   if row+h > H or col+w > W: continue
   local_haystack = haystack[row:row+h, col:col+w].copy()

And this is where I am stuck, I have tried np.count_nonzeros, np.sum etc. but the problem seems to be, that it counts any color like it would be a flattened array instead of a tuple. So instead of counting 1 for (255, 255, 255) it counts 3 which makes me miss the needle

2
  • Does this question help?
    – orlp
    Commented Feb 8, 2021 at 0:27
  • I know opencv, but unfortunately its slower than my current method with PIL loading and comparing pixel by pixel. I've given up on the numpy method, all the ways I found to find the needle turned out to be 50-100% slower than my current method
    – emceef
    Commented Feb 8, 2021 at 21:48

0

Browse other questions tagged or ask your own question.