java问题,下面像素的图像,分别通过中值和均值滤波会得到什么样的结果

java问题,下面像素的图像,分别通过中值和均值滤波会得到什么样的结果

问题描述:

以下图片,红线这一行,分别通过中值和均值滤波会得到什么样的结果,求解

img

# 根据理解写的中值滤波
import numpy as np


def MedianValue(src, row, col, scale, size):
    tmpList = []
    (rows, cols, _) = src.shape
    if row + size < rows and row - size >= 0 and col - size >= 0 and col + size < cols:
        for i in range(row - size, row + size + 1):
            for j in range(col - size, col + size + 1):
                tmpList.append(src[i][j][scale])
    if len(tmpList) > 0:
        tmpList = sorted(tmpList)
        return tmpList[len(tmpList) >> 1]
    else:
        return src[row][col][scale]


def medianBlur(src, ksize, dst=None):
    """
    medianBlur(src, ksize[, dst]) -> dst
    """
    if src is None:
        return None
    if ksize % 2 == 0 or ksize < 3:
        raise EOFError('ksize aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 ...')
    (rows, cols, scales) = src.shape
    dst = src.copy()
    for i in range(rows):
        for j in range(cols):
            for scale in range(scales):
                dst[i][j][scale] = MedianValue(dst, i, j, scale, ksize >> 1)
    return dst


def MeanValue(src, row, col, scale, size):
    tmpList = []
    (rows, cols, _) = src.shape
    if row + size < rows and row - size >= 0 and col - size >= 0 and col + size < cols:
        for i in range(row - size, row + size + 1):
            for j in range(col - size, col + size + 1):
                tmpList.append(src[i][j][scale])
    if len(tmpList) > 0:
        tmpList = sorted(tmpList)
        return np.mean(tmpList)
    else:
        return src[row][col][scale]

def meanBlur(src, ksize, dst=None):
    """
    medianBlur(src, ksize[, dst]) -> dst
    """
    if src is None:
        return None
    if ksize % 2 == 0 or ksize < 3:
        raise EOFError('ksize aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 ...')
    (rows, cols, scales) = src.shape
    dst = src.copy()
    for i in range(rows):
        for j in range(cols):
            for scale in range(scales):
                dst[i][j][scale] = MeanValue(dst, i, j, scale, ksize >> 1)
    return dst


def main():
    src_array = [[3, 2, 1, 2, 3, 4, 2, 2],
                 [1, 2, 3, 4, 3, 2, 1, 0],
                 [2, 2, 3, 3, 2, 1, 3, 5]]
    src_array = np.array(src_array)
    src_array = np.expand_dims(src_array, 2)

    median = medianBlur(src_array, min(src_array.shape[0], src_array.shape[1]), None)
    print("median:")
    print(np.squeeze(median))
    print()
    print(np.squeeze(median[1]).tolist()[1:-1])

    print()
    mean = meanBlur(src_array, min(src_array.shape[0], src_array.shape[1]), None)
    print("mean:")
    print(np.squeeze(mean))
    print()
    print(np.squeeze(mean[1]).tolist()[1:-1])


main()


img


前面是采用int类型,下面是采用float类型

# 2.3.2 根据理解写的中值滤波
import numpy as np


def MedianValue(src, row, col, scale, size):
    tmpList = []
    (rows, cols, _) = src.shape
    if row + size < rows and row - size >= 0 and col - size >= 0 and col + size < cols:
        for i in range(row - size, row + size + 1):
            for j in range(col - size, col + size + 1):
                tmpList.append(src[i][j][scale])
    if len(tmpList) > 0:
        tmpList = sorted(tmpList)
        return tmpList[len(tmpList) >> 1]
    else:
        return src[row][col][scale]


def medianBlur(src, ksize, dst=None):
    """
    medianBlur(src, ksize[, dst]) -> dst
    """
    if src is None:
        return None
    if ksize % 2 == 0 or ksize < 3:
        raise EOFError('ksize aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 ...')
    (rows, cols, scales) = src.shape
    dst = src.copy()
    for i in range(rows):
        for j in range(cols):
            for scale in range(scales):
                dst[i][j][scale] = MedianValue(dst, i, j, scale, ksize >> 1)
    return dst


def MeanValue(src, row, col, scale, size):
    tmpList = []
    (rows, cols, _) = src.shape
    if row + size < rows and row - size >= 0 and col - size >= 0 and col + size < cols:
        for i in range(row - size, row + size + 1):
            for j in range(col - size, col + size + 1):
                tmpList.append(src[i][j][scale])
    if len(tmpList) > 0:
        tmpList = sorted(tmpList)
        return np.mean(tmpList)
    else:
        return src[row][col][scale]


def meanBlur(src, ksize, dst=None):
    """
    medianBlur(src, ksize[, dst]) -> dst
    """
    if src is None:
        return None
    if ksize % 2 == 0 or ksize < 3:
        raise EOFError('ksize aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 ...')
    (rows, cols, scales) = src.shape
    dst = src.copy()
    for i in range(rows):
        for j in range(cols):
            for scale in range(scales):
                dst[i][j][scale] = MeanValue(dst, i, j, scale, ksize >> 1)
    return dst


def main():
    src_array = [[3, 2, 1, 2, 3, 4, 2, 2],
                 [1, 2, 3, 4, 3, 2, 1, 0],
                 [2, 2, 3, 3, 2, 1, 3, 5]]
    src_array = np.array(src_array, dtype=np.float)   
    src_array = np.expand_dims(src_array, 2)

    median = medianBlur(src_array, min(src_array.shape[0], src_array.shape[1]), None)
    print("median:")
    print(np.squeeze(median))
    print()
    print(np.squeeze(median[1]).tolist()[1:-1])

    print()
    mean = meanBlur(src_array, min(src_array.shape[0], src_array.shape[1]), None)
    print("mean:")
    print(np.squeeze(mean))
    print()
    print(np.squeeze(mean[1]).tolist()[1:-1])


main()


img

中值就是把旁边挨着的一圈数数一数,中间的那个就是,比如第二行第二列,他旁边是 3,2,1,1,2,3,2,2,3 排序 -> 1,1,2,2,2,2,3,3,3,中值是2,均值也是2,其他的一样的