0

Is there a way to optimize this snippet of code? With my current im value, it is taking me ~28 seconds. Wondering if I could reduce this time.

im = output_image[-min_xy[1]:-min_xy[1] + image_2.shape[0], -min_xy[0]:-min_xy[0] + image_2.shape[1]]
    for idx, rk in enumerate(im):
        for ix, k in enumerate(rk):
            image_2[idx][ix] = avg(im[idx][ix], image_2[idx][ix])

type(image_2) and type(im) is <type 'numpy.ndarray'>

im.shape and image_2.shape is (2386, 3200, 3)

what my avg() does is

def avg(a1, a2):
    if [0., 0., 0.] in a1:
        return a2
    else:
        return (a1 + a2) / 2

NOTE: a1 is an array of size 3 ex: array([ 0.68627451, 0.5372549 , 0.4745098])

4
  • Try to see if your algorithm behaves like any matrix transformations or operations? Commented Oct 18, 2017 at 22:29
  • With if [0., 0., 0.] in a1 its checking if anyone in a1 is zero. Are you sure you want that? Commented Oct 18, 2017 at 22:45
  • @Liondancer That doesn't answer my question, which was - With if [0., 0., 0.] in a1:, are you trying to check if ALL elements in a1 are zeros OR if ANY one element in a1 is zero? Commented Oct 19, 2017 at 4:16
  • @Divakar if ALL elements in a1 are zeros sorry Commented Oct 19, 2017 at 8:46

1 Answer 1

1

The only obstacle to vectorization seemed to be that IF conditional in avg. To get past it, simply use the choosing capability of np.where and thus have our solution like so -

avgs = (im + image_2)/2.0
image_2_out = np.where((im == 0).any(-1,keepdims=1), image_2, avgs)

Note that this assumes with if [0., 0., 0.] in a1, you meant to check for ANY one match. If you meant to check for ALL zeros, simply use .all instead of .any.

Alternatively, to do in-situ edits in image_2, use a mask for boolean-indexing -

mask = ~(im == 0).any(-1)
image_2[mask] = avgs[mask]
Sign up to request clarification or add additional context in comments.

Comments

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.