Skip to main content
 首页 » 编程设计

python之计算在二维数组的子矩阵中找到一个值的概率

2024年09月07日16telwanggs

这可能是个愚蠢的问题,但我正在尝试用在像素附近找到该值的概率来替换图像的像素值。

假设我们有一张名为 img 的 100x100 图片。我想以 5x5 block (或其他奇数边的正方形)为单位对图像进行采样,确定样本中每个值的概率,然后用该值的概率替换像素值,根据公式:

其中 nk 是 zk 值出现的次数,NM 是我的样本边长(在本例中为 5x5)的乘积。所以对于 8 位图像,我们有 zi,i = 0, 1, ..., 255。 如果像素 j 的值为 zk,则应将其替换为 5x5 样本的 P(zk)。我还需要以某种方式处理边缘,但现在,我唯一的解决方案是一堆 for 循环,需要永远计算。任何帮助将不胜感激!

编辑

这是我当前的代码,计算 3.7 MPixel 8 位灰度图像大约需要一分半钟:

import numpy as np 
area = np.array([7, 7]) 
 
def probability(image, area): 
    dx, dy = area 
    dxx, dyy = area//2 
    imout = np.zeros_like(image, dtype=np.float64) 
    image = np.pad(image, dxx, 'edge') 
    shape = (image.shape[0] - dx + 1, image.shape[1] - dy +1, dx,dy) 
    strides = 2*image.strides 
    patches = stride_tricks.as_strided(image,shape=shape, strides=strides) 
    pa = patches.reshape(image.shape[0] - dx + 1, image.shape[1] - dy +1, dx*dy) 
    i = j = 0 
    for x in pa: 
        for y in x: 
            values, inverse, counts = np.unique(y, return_inverse=True, return_counts=True) 
            p = counts/y.size 
            idx = inverse[y.size//2] 
            imout[i,j] = p[idx] 
            j += 1 
        i += 1 
        j = 0 
    return imout 
 
p = probability(file[0, :20, :20], (10,10), area) 
pixel = file[0, 10, 10] 
print('image array:\n', file[0, 7:14, 7:14]) 
print('Probability for pixel value {} is: {:0.3f}'.format(pixel, p[1])) 
 
image array: 
 [[49 58 53 60 61 50 51] 
 [56 49 57 50 48 49 58] 
 [51 59 56 55 45 56 51] 
 [57 58 61 49 56 54 65] 
 [46 52 47 52 46 45 44] 
 [46 57 56 60 56 56 57] 
 [63 58 36 67 47 60 43]] 
Probability for pixel value 49 is: 0.082 

请您参考如下方法:

看看这是否运行得更快。仅查找您在数组中实际拥有的值意味着您应该通过不尝试计算不存在的值来节省时间。 np.bincount() 从零开始计数。 np.histogram 允许您指定 bin 值,在示例中,您给出的数组中的最低值为 36。

使用您展示的 7 x 7 像素阵列:

import numpy as np 
arr = np.array([[49, 58, 53, 60, 61, 50, 51], 
                [56, 49, 57, 50, 48, 49, 58], 
                [51, 59, 56, 55, 45, 56, 51], 
                [57, 58, 61, 49, 56, 54, 65], 
                [46, 52, 47, 52, 46, 45, 44], 
                [46, 57, 56, 60, 56, 56, 57], 
                [63, 58, 36, 67, 47, 60, 43]]) 
values = np.unique(arr) 
prob = np.histogram(arr, values)[0] / arr.size 
print(dict(zip(values, prob))) 

这是输出:

{36: 0.02040816326530612, 43: 0.02040816326530612, 44: 0.02040816326530612,  
45: 0.04081632653061224, 46: 0.061224489795918366, 47: 0.04081632653061224,  
48: 0.02040816326530612, 49: 0.08163265306122448, 50: 0.04081632653061224,  
51: 0.061224489795918366, 52: 0.04081632653061224, 53: 0.02040816326530612,  
54: 0.02040816326530612, 55: 0.02040816326530612, 56: 0.14285714285714285,  
57: 0.08163265306122448, 58: 0.08163265306122448, 59: 0.02040816326530612,  
60: 0.061224489795918366, 61: 0.04081632653061224, 63: 0.02040816326530612,  
65: 0.04081632653061224} 

编辑:至于您问题的子矩阵部分:使用 Python for 循环从数组构建列表 对我来说似乎很慢。如果你能保证子矩阵大小在两个维度上均分整个数组大小,你可以这样做:

row_div, col_div = 5, 5   # or whatever values you need 
for row in np.split(arr, row_div): 
    for rotated in np.split(row.T, col_div): 
        submatrix = rotated.T 
        # do stuff with submatrix 

您还可以使用二维切片:submatrix = arr[r1:r2, c1:c2] ,其中索引是通过 for 循环计算的,但您可以使用更大的步幅。