rijyurin 2021-11-29 13:11 采纳率: 75%
浏览 77
已结题

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

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

img

  • 写回答

2条回答 默认 最新

  • 广大菜鸟 2021-11-30 01:36
    关注
    # 根据理解写的中值滤波
    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月30日
  • 已采纳回答 11月30日
  • 创建了问题 11月29日

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题