2

空間分析の目的で、特定の近隣のピクセルに対して、その近隣のこのピクセルのパーセンタイル (構造化要素によって定義される)を与えるフィルターを設定しようとしています。

以下は、これまでの私のベストショットです。

import numpy as np
import scipy.ndimage as ndimage
import scipy.stats as sp

def get_percentile(values, radius=3):
  # Retrieve central pixel and neighbours values
  cur_value = values[4]
  other_values = np.delete(values, 4)

  return sp.percentileofscore(other_values, cur_value)/100

def percentiles(image):

  # definition of the neighbourhood (structuring element)
  footprint = np.array([[1,1,1],
                  [1,1,1],
                  [1,1,1]])

  # Using generic_filter to apply sequentially a my own user-defined
  # function (`get_percentile`) in the filter
  results = ndimage.generic_filter(
    image, 
    get_percentile, 
    footprint=footprint, 
    mode='constant', 
    cval=np.nan)

  return results

# Pick dimensions for a dummy example
dims = [12,15]
# Generate dummy example
df = np.random.randn(np.product(dims)).reshape(dims[0], dims[1])

percentiles(df)

1. コードが実際には最適化されておらず、より高速に実行できると確信している 2. 近隣の次元がハードコードされている。私が望むのは、footprintこのフィルターに従って、フィルターを適用している中央のピクセル ( ) を隣接ピクセルからより適切に識別することです。

4

0 に答える 0