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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助