-1

Wanna know if two numpy arrays are close to each other... like if we had 2 (2-d) numpy arrays of 2 images I wanna know if they close to each other (are they similar or not) not equal also, there is anyway to know if the probability of similarity are close to 1? like any classifier would do but I just wanted it without machine learning.
let's assume we have 2 pixel:

   a = [255, 250, 3]
   b = [255, 255, 3]
   err = [(bb-aa)/max(aa,bb) for aa, bb in a, b]

then the probability

   p = 1 - np.mean(np.array(err))
Community
  • 1
  • 1
Muhammed Saeed
  • 101
  • 1
  • 10
  • How do you propose to measure similarity? – kevinkayaks Sep 11 '18 at 20:48
  • @kevinkayaks like if we have these a = [255, 255,0], b = [250,255,1] these are similar, i don't know how to get prob of these two...? – Muhammed Saeed Sep 11 '18 at 20:55
  • but you said images: how do you propose to measure similarity? L2 distance between pixels, summed over all pixels? There are many ways – kevinkayaks Sep 11 '18 at 21:01
  • i think 1 - a[0] - b[0]/ a[0] for this specific example the prob. of closeness would be 0.98... @kevinkayaks – Muhammed Saeed Sep 11 '18 at 21:10
  • how would you generalize this to the entire image, instead of just one pixel? – kevinkayaks Sep 11 '18 at 21:12
  • it's easy with nested for loops, but i'm looking for something with fair time to go... because i have this data to check so it would take me long time to do – Muhammed Saeed Sep 11 '18 at 21:14
  • it's easy how? How do you combine the operation per pixel to all pixels? multiplication? addition? And how does ` 1 - a[0] - b[0]/ a[0]` define a closeness? you're ignoring the other two channels? You need to post a complete example data and anticipated result. 2x2 images is fine. If you ask a proper question like this with a numpy tag you'll have people literally racing to answer you – kevinkayaks Sep 11 '18 at 21:15
  • 1
    i don't ignoring the other two channels... but it's okay... thanks you i'm editing the question – Muhammed Saeed Sep 11 '18 at 21:24
  • so how do you combine the measure from all pixels? Do you multiply them across all pixels? Is this probability you want a product of the probabilities from every pixel? – kevinkayaks Sep 11 '18 at 21:34
  • by mean of the pixel the mean of mean all pixels (image) then i would know if it's similar or not – Muhammed Saeed Sep 11 '18 at 21:36

1 Answers1

1

Ok, this should do what you describe.

import numpy as np
im1 = np.random.randint(0,255,size=(2,2,3)).astype('float')
im2 = np.random.randint(0,255,size=(2,2,3)).astype('float')
p = 1 - np.mean(np.abs(im1-im2)/np.stack((im1,im2),-1).max(-1))

To calculate p, this code subtracts pixel by pixel, then takes the absolute value, before dividing by the maximum value between the images at each channel of each pixel. This is a little confusing but it's what you described. You can dismantle the one liner to see what each piece is doing.

A key point: your images have to be float datatype for this to work.

kevinkayaks
  • 2,636
  • 1
  • 14
  • 30