Python实现滤波

import numpy as np
def Read_raw(filename,w,h,m):
    #初始化image
    image=[]
    for i in range(m*h):
        image.append([])
    #读取raw
    with open(filename,'rb') as f:
        data=f.read()
    #写入image
    for i in range(m*h):
        for j in range(w):
            image[i].append(data[i*w+j])
    print('读取{0}成功'.format(filename))
    return image

def Write_raw(filename,image):
    #将数组image转为bytes
    bt=bytearray()
    for i in range(len(image)):
        for j in range(len(image[i])):
            bt.append(image[i][j])
    #写入raw
    with open(filename,'wb') as f:
        f.write(bt)
    f.close()
    print('写入{0}成功'.format(filename))

def convolution(image,ope):
    #初始化con
    con=[]
    for i in range(len(image)-len(ope)+1):
        con.append([])
        for j in range(len(image[i])-len(ope[0])+1):
            temp_window=[]
            for g in range(len(ope)):
                temp_window.append([])
                for h in range(len(ope[g])):
                    temp_window[g].append(image[i+g][j+h])
            temp_con=0
            for g in range(len(ope)):
                for h in range(len(ope[g])):
                    temp_con=temp_con+ope[g][h]*temp_window[g][h]
            if round(temp_con)>255:
                con[i].append(255)
            elif round(temp_con)<0:
                con[i].append(0)
            else:
                con[i].append(round(temp_con))
    return np.array(con)

def Non_Maximum(image):
    temp_image=image.copy()
    for i in range(len(temp_image)):
        for j in range(1,len(temp_image[i])-1):
            if temp_image[i][j]<max(temp_image[i][j-1],temp_image[i][j+1]):
                temp_image[i][j]=0
    return temp_image

def Double(image,low,high):
    temp_image=image.copy()
    for i in range(len(temp_image)):
        for j in range(len(temp_image[i])):
            if temp_image[i][j]<low:
                temp_image[i][j]=0
            elif temp_image[i][j]>high:
                temp_image[i][j]=255
    return temp_image

#降噪
ope1=[[1/25,1/25,1/25,1/25,1/25],
     [1/25,1/25,1/25,1/25,1/25],
     [1/25,1/25,1/25,1/25,1/25],
     [1/25,1/25,1/25,1/25,1/25],
     [1/25,1/25,1/25,1/25,1/25]]

#计算梯度
ope2_x=[[-1,-1,0],
        [-1, 0,1],
        [ 0, 1,1]]
ope2_y=[[ 1, 2, 0],
        [ 0, 0, 0],
        [-1,-2,-1]]
#边缘提取
ope=[[0,1,0],
     [1,-4,1],
     [0,1,0]]

#锐化
'''
ope=[[0,1,0],
     [-1,1,1],
     [0,-1,0]]
'''
image=Read_raw('lena1.raw',512,512,1)
image1=convolution(image,ope)
Write_raw('lena_laplace.raw',image1)
'''
image2_y=convolution(image1,ope2_y)
image3_y=Non_Maximum(image2_y)
image4_y=iteration(image3_y)
Write_raw('image2_y.raw',image2_y)
Write_raw('image3_y.raw',image3_y)
Write_raw('image4_y.raw',image4_y)

image2_x=convolution(image1,ope2_x)
image3_x=Non_Maximum(image2_x)
image4_x=Double(image3_x,50,150)
Write_raw('image2_x.raw',image2_x)
Write_raw('image3_x.raw',image3_x)
Write_raw('image4_x.raw',image4_x)

image5=convolution(image,ope)
image5_x=Double(image5,50,150)
Write_raw('lena_边缘提取.raw',image5_x)
'''

你可能感兴趣的:(python,图像处理)