示例#1
0
def median_filter(img):
    """九点中值滤波"""
    dim = 3
    n = dim * dim
    mid_pos = int((n + 1) / 2)
    margin = int((dim - 1) / 2)
    shape = img.shape
    new_img = immake(shape)  # 构造新像素矩阵
    rows, cols = shape

    for i in range(0, rows):
        for j in range(0, cols):
            # 边缘像素不处理
            if i <= margin or i >= rows - margin or j <= margin or j >= cols - margin:
                new_img[i, j] = img[i, j]
                continue

            # 邻域像素排序后取中值
            arr = [
                img[x, y] for x in range(i - margin, i + margin + 1)
                for y in range(j - margin, j + margin + 1)
            ]
            arr.sort()
            new_img[i, j] = arr[mid_pos]

    return new_img
示例#2
0
def linear_filter(img):
    """3*3九点均值滤波"""
    operator = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
    size = 9
    dim = len(operator)
    shape = img.shape
    new_img = immake(shape)  # 构造新像素矩阵
    rows, cols = shape
    for i in range(rows):
        for j in range(cols):
            r = 0
            for m in range(dim):
                # 超出边界行的元素按边界值处理
                x = max(i + m - 1, 0)
                x = min(x, rows - 1)
                for n in range(dim):
                    # 超出边界列的元素按边界值处理
                    y = max(j + n - 1, 0)
                    y = min(y, cols - 1)
                    # 与相应模板值做卷积
                    w = operator[m][n]
                    r += w * img[x, y]

            new_img[i, j] = r / size

    return new_img
示例#3
0
def roberts_sharpen(img):
    """Roberts交叉差分锐化滤波"""
    shape = img.shape
    new_img = immake(shape)
    rows, cols = shape
    for i in range(0, rows - 1):
        for j in range(0, cols - 1):
            grad = abs(int(img[i, j]-int(img[i+1, j+1]))) + \
                abs(int(img[i+1, j])-int(img[i, j+1]))
            new_img[i, j] = grad

    return new_img
示例#4
0
def grad_sharpen(img):
    """梯度法锐化滤波"""
    shape = img.shape
    new_img = immake(shape)
    rows, cols = shape
    for i in range(0, rows):
        for j in range(0, cols):
            grad = 0
            if i < rows - 1:
                grad += abs(int(img[i, j]) - int(img[i + 1, j]))
            if j < cols - 1:
                grad += abs(int(img[i, j]) - int(img[i, j + 1]))

            new_img[i, j] = grad

    return new_img
示例#5
0
def laplacian_enhance(img):
    operator = [
        [1, 1, 1],
        [1, -9, 1],
        [1, 1, 1],
    ]
    shape = img.shape
    new_img = immake(shape)
    rows, cols = shape
    for i in range(1, rows - 1):
        for j in range(1, cols - 1):
            s = 0
            for m in range(0, 3):
                for n in range(0, 3):
                    w = operator[m][n]
                    x = i - 1 + m
                    y = j - 1 + n
                    s += w * img[x][y]

            new_img[i, j] = abs(int(s))

    return new_img
示例#6
0
def laplacian_sharpen(img):
    """3*3中心点为-8的掩模拉普拉斯锐化滤波"""
    operator = [
        [1, 1, 1],
        [1, -8, 1],
        [1, 1, 1],
    ]
    shape = img.shape
    new_img = immake(shape)
    rows, cols = shape
    for i in range(1, rows - 1):
        for j in range(1, cols - 1):
            s = 0
            for m in range(0, 3):
                for n in range(0, 3):
                    w = operator[m][n]
                    x = i - 1 + m
                    y = j - 1 + n
                    s += w * img[x][y]

            new_img[i, j] = abs(int(s))

    return new_img