Python中的平均过滤(手动)

时间:2018-12-11 16:14:05

标签: python filtering media gaussian manual

我想实现均值和高斯滤波。我实现了中位数,但是现在我在尝试计算平均值/均值和高斯时迷失了。

我需要手动执行帮助,因为使用库结果要容易得多,但任务是手动执行。

enter code here
#Import functions from OpenCV
import cv2

if __name__ == '__main__':
    ini = cv2.imread("img3.png",0) # Load an image
    final = ini[:]
    for y in range(len(ini)):
        for x in range(y):
            final[y,x]=ini[y,x]

 # create a sliding window of size 9
    pix=[ini[0,0]]*9
    for y in range(1,ini.shape[0]-1):
        for x in range(1,ini.shape[1]-1):

            #Pick up window element
            pix[0] = ini[y-1,x-1]
            pix[1] = ini[y,x-1]
            pix[2] = ini[y+1,x-1]
            pix[3] = ini[y-1,x]
            pix[4] = ini[y,x]
            pix[5] = ini[y+1,x]
            pix[6] = ini[y-1,x+1]
            pix[7] = ini[y,x+1]
            pix[8] = ini[y+1,x+1]

            pix.sort()#sort the window to find median
            final[y,x]=pix[4]#assign the median to centered element of the matrix

    cv2.imshow('Final_Picture', final) #Show the image
    cv2.waitKey()

0 个答案:

没有答案