When comparing arrays, why is "in1d" much slower than "a == b"

I need to be able to compare two images and extract any unique pixels to create a third image. To do this, I did the following:

import cv2
import numpy as np

img = cv2.imread("old.jpg")
img2 = cv2.imread("new.jpg")

image2 = cv2.cvtColor(img2, cv2.COLOR_RGB2RGBA)

for (x,y,z), value in np.ndenumerate(img):
    dif = img[x,y,0] == img2[x,y,0] #only checking one color for speed
    diff = str(dif)
    if "True" in diff:
        image2[x,y,3] = 0
cv2.imwrite("result.png", image2)        

      

This worked pretty well, but it took about 10 seconds for a 640 x 480 image and I was hoping to get close to it about half the time. So I changed this line:

dif = img[x,y,0] == img2[x,y,0]

      

to

dif = np.in1d(img[x,y,0], img2[x,y,0])

      

The results are identical, but instead of speeding up the process, it takes about 3 minutes. I don't understand why at all.

I understand that iterating over elements in large arrays will be time consuming in python, but why is in1d so much slower?

(As a side note, I would just use the " palette " method , but I couldn't figure out how to implement it for this purpose due to my limited knowledge of numpy arrays.)

+3


source to share


1 answer


np.in1d

checks every element of its first argument against every element of its second element in the worst case. For each element i,j

in, img

it checks if there is an element k,l

in img2

with the same value. This means 640x480

you can do comparisons for your image (640x480)^2

.

On the other hand, ==

only checks cell, it checks to see if the element i,j

of img

an element i,j

of img2

. He will always compare 640x480

.



np.in1d

will work if you have images of different sizes, ==

will only work for images of the same size.

+5


source







All Articles