1

Orginal and Mutated are images. I need to get the difference of each r,g,b separately. I got this code to work, but it is to slow. Any help on making this fast would be nice! :)

Orginal = np.asarray(Orginal).copy()
Mutated = np.asarray(Mutated).copy()    

Fittnes = 0

for x in range(0, 299):

    for y in range(0, 299):

        DeltaRed   = (Orginal[x][y][0] - Mutated[x][y][0])
        DeltaGreen = (Orginal[x][y][1] - Mutated[x][y][1])
        DeltaBlue  = (Orginal[x][y][2] - Mutated[x][y][2])

        Fittnes += (DeltaRed * DeltaRed + DeltaGreen * DeltaGreen + DeltaBlue * DeltaBlue)

return Fittnes
3
  • You want to redo python part? Because there is a way to speed-up numpy itself, using intel MKL software.intel.com/en-us/articles/numpyscipy-with-intel-mkl Commented Nov 15, 2015 at 2:09
  • Got a better code. Any better solution would be helpfull Commented Nov 15, 2015 at 2:20
  • @SeverinPappadeux MKL would only be relevant for linear algebra ops, e.g. matrix products. Simple vectorized subtraction and summation are handled by numpy's internals rather than external libraries. Commented Nov 15, 2015 at 11:59

3 Answers 3

3

It should be a whole lot faster if you didn't go the extra mile of zipping and then summing up each dimension instead of using numpy's sum function:

DeltaRed   = np.sum(OR) - np.sum(MR)
DeltaGreen = np.sum(OG) - np.sum(MG)
DeltaBlue = np.sum(OB) - np.sum(MB)
Sign up to request clarification or add additional context in comments.

2 Comments

Any way to improve it more?
On my computer, this gives you at least a 100x speed improvement. For anything beyond I'd have to know abot your particular use case. If you're summing up the whole image, all additional improvements will be small.
1

Here's one approach to do all those in one summation with ndarray.sum -

DeltaRed, DeltaGreen, DeltaBlue = Orginal.sum((0,1)) - Mutated.sum((0,1))

Here's another with np.einsum and hopefully faster one, when working with uint8 images -

org_diff = np.einsum('ijk->k',Orginal.astype('uint64'))
mut_diff = np.einsum('ijk->k',Mutated.astype('uint64'))
DeltaRed, DeltaGreen, DeltaBlue = org_diff - mut_diff

17 Comments

This gets me the total RGB changes, I need them to calculate the RGB difference for each pixel, and get the total RGB changes for all pixels combined.
@Funktiona ` RGB difference for each pixel` would be Orginal - Mutated, right? And total RGB changes for all pixels combined would be as listed in the solution?
Look my code bellow. They does not do the same as your solutions
@Funktiona What's the datatype of Orginal and Mutated?
they are numpy.asarray
|
0

This was the code from beginning that worked.

    Fittnes = 0

    for x in range(0, 299):

        for y in range(0, 299):

            DeltaRed   = (Orginal[x][y][0] - Mutated[x][y][0])
            DeltaGreen = (Orginal[x][y][1] - Mutated[x][y][1])
            DeltaBlue  = (Orginal[x][y][2] - Mutated[x][y][2])

            Fittnes += (DeltaRed * DeltaRed + DeltaGreen * DeltaGreen + DeltaBlue * DeltaBlue)

   return Fittnes

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.