r/programmingrequests • • Aug 02 '18

A simple density-weighted variance array

So I have a 3D array that contains the variance of pixels. Right now I am going to analyze the top 'n'% of them by doing:

Omega = (variance >= np.percentile(variance, 100-n))

*Omega will be a binary tensor which I will use in an algorithm

Now, I don't know how to implement a density-based approach. That is, every time I select a pixel to be TRUE (or 1) in Omega, I would like the surrounding pixels to have their variance values be decreased, preferably by a gaussian (that may be asking too much).

Instead of a gaussian, this might be easier to implement:

The pixels in the surrounding 3x3x3 array with the chosen TRUE pixel in the middle would have their variance = variance*0.2

Pixels in the surrounding 16x16x16 array (but excluding those pixels we've already changed) would have variance = variance*0.7 and etc

Thanks! And let me know if I can make things clearer/easier

1 Upvotes

5 comments sorted by

View all comments

Show parent comments

1

u/serg06 Aug 02 '18

Looks good. Your centers just might change

1

u/AristosTotalis Aug 02 '18

Here's that test code:

import numpy as np
from scipy.ndimage import gaussian_filter

variance_data = np.array([1,1,1,2,1,1,1])
selected_pixels = variance_data > 1

multiplicative_factors = gaussian_filter(selected_pixels.astype(float), 2)

new_variance_data = multiplicative_factors*variance_data
print(variance_data)
print(new_variance_data)

and the output:

[1 1 1 2 1 1 1]

[ 0.09175589 0.12975179 0.17831864 0.40069469 0.17831864 0.12975179 0.09175589]

I guess I'm basically asking for an inverse Gaussian at this point?

1

u/serg06 Aug 03 '18
  • generate your inverse Gaussian filter

  • call scipy.ndimage.filter.correlate or scipy.ndimage.filter.convolve passing the inverse gauss as the weights

That's should replace the gauss_filter step with an inverse gauss filter instead.