计算64乘64图像的均匀性

时间:2016-04-13 17:31:25

标签: python image numpy

我对python和genral编程都很新。我一直在尝试使用python及其库来计算图像的一致性。由于探测器尺寸小于“图像”,因此图像采用填充零的64乘64矩阵。在计算之前,我必须使用加权内核来平滑图像:([[1,2,1],[2,4,2],[1,2,1]])。

但在平滑之前,我必须相应地设置边框像素的值:

  1. 有用视野(ufov)边缘的任何像素,包含中心视野中平均值小于75%(ufov的75%),应设置为零。

  2. 其四个直接支配的邻居中至少有一个包含零的像素应设置为零。

  3. 9点滤波器中分析区域外像素的加权系数应为零。

  4. 我使用了ma.masked_equal来排除填充区域。但是我在上述条件下遇到麻烦。更不用说仅将它们应用于有用视野的边缘。

    由于英语不是我的第一语言,我希望我能解释清楚。否则我会aplogize。

    import numpy as np
    import numpy.ma as ma
    import scipy
    from scipy import ndimage
    
    #ufov = useful field of view - non-zero elements
    #cfov = central field of view - in this example 50% of ufov
    
    img = ([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 2, 0, 2, 0, 0, 1, 0, 8, 0, 7, 0, 0, 0],
            [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0],
            [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 0, 0],
            [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0],
            [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0],
            [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0],
            [0, 0, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0],
            [0, 0, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0],
            [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0],
            [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0],
            [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
    
    img = ma.masked_equal(img, 0) #mask zeros
    
    axis_0 = ma.notmasked_edges(img,0) #start/end masked pixel value along axis=0
    axis_1 = ma.notmasked_edges(img,1) #start/end masked pixel value along axis=1
    
    xstart = min(axis_1[0][1]) #start along x axis -rows (last masked pixel)
    xend = max(axis_1[1][1]) #end along x axis -rows (last masked pixel)
    
    ystart = min(axis_0[0][0]) #start along y axis -columns (last masked pixel)
    yend = max(axis_0[1][0]) #end along y axis -columns (last masked pixel)
    
    ufov = img[ystart:yend+1, xstart:xend+1] #ufov sliced along the masked array edge
    
    #cfov is 50% of ufov(sliced from ufov)
    
    cfovystart = int((ufov.shape[0] - ufov.shape[0] * 0.5)/2)
    cfovyend = int(ufov.shape[0] - cfovystart)
    cfovxstart = int((ufov.shape[1] - ufov.shape[1] * 0.5)/2)
    cfovxend = int(ufov.shape[1] - cfovxstart)
    
    cfov = ufov[cfovystart:cfovyend, cfovxstart:cfovxend]
    
    #1. condition - applied over the whole ufov (want edges)
    #pixels containing less than 75% of cfov mean value are replace with 0
    mean = int(cfov.mean()*0.75)
    
    ufov[ufov < mean]=0
    
    #2. condition
    #pixels which have at least one of their four directly abbuted neighbors containing zero
    #shall be set to zero ???
    
    #3.condition
    #the weighting factor for a pixel outside the analysed are in 9 point filter
    #shall be zero ???
    
    
    #smoothing befor calculating (but not including the 3.condition
    weights = np.array([[1,2,1],
                       [2,4,2],
                       [1,2,1]])
    
    kernel = weights*1/16
    
    img_smth = ndimage.convolve(ufov, kernel, mode="constant", cval=0)
    
    #then calculate the uniformity of ufov - useful field of view as follows
    
    #max_pix = int(ufov.max())
    #min_pix = int(ufov.min())
    
    #unif_ufov = (max_pix-min_pix) / (max_pix+min_pix) * 100
    

0 个答案:

没有答案