Home > Software engineering >  Why do these two numpy.divide operations give such different results?
Why do these two numpy.divide operations give such different results?

Time:03-05

I would like to correct the values in hyperspectral readings from a cameara using the formula described over left image right image

CodePudding user response:

Your two methods don't agree because in the first method you used

   white_nparr_rep = white_nparr.repeat(43, axis=0)

but the second method corresponds to using

   white_nparr_rep = np.tile(white_nparr, (43, 1, 1))

If the first method is correct, you'll have to adjust the second method to act accordingly. Perhaps

for i in range(int(data_scale / ref_scale)):
    data_nparr[i*ref_scale:(i 1)*ref_scale] = 
        np.divide
        ( 
        np.subtract(data_nparr[i*ref_scale:(i 1)*ref_scale], dark_nparr[i]),
        np.subtract(white_nparr[i], dark_nparr[i])
        )

A simple example with 2-d arrays that shows the difference between repeat and tile:

In [146]: z
Out[146]: 
array([[ 1,  2,  3,  4,  5],
       [11, 12, 13, 14, 15]])

In [147]: np.repeat(z, 3, axis=0)
Out[147]: 
array([[ 1,  2,  3,  4,  5],
       [ 1,  2,  3,  4,  5],
       [ 1,  2,  3,  4,  5],
       [11, 12, 13, 14, 15],
       [11, 12, 13, 14, 15],
       [11, 12, 13, 14, 15]])

In [148]: np.tile(z, (3, 1))
Out[148]: 
array([[ 1,  2,  3,  4,  5],
       [11, 12, 13, 14, 15],
       [ 1,  2,  3,  4,  5],
       [11, 12, 13, 14, 15],
       [ 1,  2,  3,  4,  5],
       [11, 12, 13, 14, 15]])

Off topic postscript: I don't know why the author of the page that you linked to writes NumPy expressions as (for example):

corrected_nparr = np.divide(
    np.subtract(data_nparr, dark_nparr),
    np.subtract(white_nparr, dark_nparr))

NumPy allows you to write that as

corrected_nparr = (data_nparr - dark_nparr) / (white_nparr - dark_nparr)

whick looks much nicer to me.

  • Related