计算 64x64 图像的一致性

2024-06-26 10:38:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python和genral编程相当陌生。我一直在尝试使用python及其库计算图像的一致性。因为探测器的尺寸比“图像”小,所以图像是在一个用零填充的64×64矩阵中。在计算之前,我必须使用加权核来平滑图像:([[1,2,1],[2,4,2],[1,2,1]])。在

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

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

  2. 如果像素的四个直接赋值邻域中至少有一个包含零,则应将其设置为零。

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

我用过戴面具的女士排除填充区域。但我在应用上述条件时遇到了困难。更不用说只将它们应用于有用视野的边缘。在

由于英语不是我的第一语言,我希望我的解释是可以理解的。否则我会重新编程。在

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

Tags: oftheimgminmaxintshapepixel