图像分割的后处理

2024-10-05 11:07:16 发布

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

我有一个3D NumPy数据集[images number,x,y],其中像素属于某个类的概率存储为浮点(0-1)。我想纠正错误的分割像素(高性能)

概率是电影的一部分,在电影中,物体从右向左移动,可能再次移动。基本思想是,我用高斯函数或类似函数拟合像素,然后查看大约15-30幅图像。如果前面的5个像素和后面的5个像素被分类在这个类别中,那么这个像素也很可能属于这个类别

如果我用循环来解决这个问题,它会花费太多的时间(尽管我使用多处理和多线程)。NumPy阵列的大小为[300600600]或更大,但图像始终为方形

下面是我的示例代码和注释:

from scipy.optimize import curve_fit
from scipy import exp
import numpy as np


def gaus(x, a, x0, sigma):
    return a*exp(-(x-x0)**2/(2*sigma**2))

if __name__ == '__main__':
    # output_AI = [imageNr, x, y] example 5, 2, 2
    # At position [2][1][1] is the error, the pixels before and after were classified to the class but not this pixel.
    # The objects do not move in such a speed, so the probability should be corrected.
    outputAI = np.array([[[0.1, 0], [0, 0]], [[0.8, 0.3], [0, 0.2]], [[1, 0.1], [0, 0.2]],
                         [[0.1, 0.3], [0, 0.2]], [[0.8, 0.3], [0, 0.2]]])
    correct_output = np.zeros(outputAI.shape)


    # I correct now in this example only all pixels in image 3, in the code a loop runs over the whole 3D array and
    # corrects every image and every pixel separately
    size_of_array = outputAI.shape
    x = range(size_of_array[0])
    for i in range(size_of_array[1]):
        for k in range(size_of_array[2]):
            args, cov = curve_fit(gaus, x, outputAI[:, i, k])
            correct_output[2, i, k] = gaus(2, *args)

    print(correct_output[2])
    # [[9.88432346e-01 2.10068763e-01]
    # [6.02428922e-20 2.07921125e-01]]
    # The wrong pixel at position [0][0] was corrected from 0.2 to almost 1, the others are still not assigned
    # to the class.

Tags: andofthetoinfromimportoutput

热门问题