1

I'm trying to compare two 3-dimensional arrays and count how many inner arrays are equal.

I'm comparing 2 patches of a picture and want to know how many pixels are equal and not how many color values are equal. And it would be nice if it's efficient so I'm using numpy. I know how to make the comparison with for loops but it's too slow.

But I'm only able to count it element wise, here's my snippet:

import numpy as np

a = np.array([[[255, 255, 255],
           [255, 255, 255],
           [255, 255, 255],
           [255, 255, 255]],

          [[255, 255, 255],
           [255, 255, 255],
           [255, 255, 255],
           [255, 255, 255]],

          [[255, 255, 255],
           [255, 255, 255],
           [255, 255, 255],
           [255, 255, 255]],

          [[255, 255, 255],
           [255, 255, 255],
           [255, 255, 255],
           [255, 255, 255]]])

b = np.array([[[255, 255, 255],
           [255, 255, 255],
           [0, 0, 0],
           [0, 0, 0]],

          [[255, 255, 255],
           [255, 255, 255],
           [0, 0, 0],
           [0, 0, 0]],

          [[0, 0, 0],
           [0, 0, 0],
           [0, 0, 0],
           [0, 0, 0]],

          [[0, 0, 0],
           [0, 0, 0],
           [0, 0, 0],
           [0, 0, 0]]])
print(np.sum(a[:, :] == b[:, :])) 
# prints 12 and i would like to have a 4 in this example
2
  • Shouldn't the desired answer be 4? I see 4 sub arrays equal Commented Dec 16, 2018 at 19:42
  • Yes of course :) Commented Dec 16, 2018 at 19:54

1 Answer 1

2

Check whether all three color values are equal for each pixel first and then count the pixels that are equal by summing up the trues:

(a == b).all(axis=-1).sum()
# 4
Sign up to request clarification or add additional context in comments.

1 Comment

i have another question, maybe you can answer it. I want to check if there are equal with a small difference. In other words there are equal if the value is for example 10% off.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.