均值平滑

#均值平滑
#先积分,上,左边界补0,省去判断边界的问题
import numpy as np
from scipy import signal
import cv2
def integral(image):
    rows,cols = image.shape
    #行积分运算
    inteImagec = np.zeros((rows,cols),np.float32)
    for r in range(rows):
        for c in range(cols):
            if c == 0:
                inteImagec[r][c] = image[r][c]
            else:
                inteImagec[r][c] = inteImagec[r][c-1]+image[r][c]
    #列积分运算
    inteImage = np.zeros(image.shape,np.float32)
    for c in range(cols):
        for r in range(rows):
            if r == 0:
                inteImage[r][c] = inteImagec[r][c]
            else:
                inteImage[r][c] = inteImage[r-1][c]+inteImagec[r][c]
    #上边左边补0
    inteImage_0 = np.zeros((rows+1,cols+1),np.float32)
    inteImage_0[1:rows+1,1:cols+1] = inteImage
    return inteImage_0
def fastMeanBlur(image,winSize,borderType = cv2.BORDER_DEFAULT):
    halfH = (winSize[0] - 1)/2
    halfW = (winSize[1] - 1)/2
    ratio = 1.0/(winSize[0]*winSize[1])
    #边界扩充
    paddImage = cv2.copyMakeBorder(image,halfH,halfH,halfW,halfW,borderType)
    #图像积分
    paddIntegral = integral(paddImage)
    #图像的高、宽
    rows,cols = image.shape
    #均值滤波后的结果
    meanBlurImage = np.zeros(image.shape,np.float32)
    r,c = 0,0
    for h in range(halfH,halfH+rows,1):
        for w in range(halfW,halfW+cols,1):
            meanBlurImage[r][c]=(paddIntegral[h+halfH+1][w+halfW+1]+paddIntegral[h-halfH][w-halfW]-
                    paddIntegral[h+halfH+1][w-halfW]-paddIntegral[h-halfH][w+halfW+1])*ratio
            c+=1
        r+=1
        c=0
    return meanBlurImage

需要astype(numpy.uint8)
opencv提供积分函数integral()
integral()
opencv提供快速均值平滑boxFilter和blur两个函数
boxFilter和blur添加链接描述

### 数据均值平滑处理的实现 #### 方法概述 均值平滑是一种常见的数据处理技术,主要用于减少随机噪声的影响并使数据更加平稳。其实现的核心在于通过对一定范围内的数据取平均值来替代原始数据点[^1]。 #### 数学表达式 假设有一组时间序列数据 \( x_1, x_2, \ldots, x_n \),窗口大小为 \( k \) 的均值平滑可以通过以下公式表示: \[ y_i = \frac{1}{k} \sum_{j=i-\lfloor k/2 \rfloor}^{i+\lceil k/2 \rceil - 1} x_j \] 其中 \( y_i \) 表示第 \( i \) 个平滑后的数据点,\( k \) 是窗口宽度,需满足 \( k \leq n \)。 对于边界情况(当索引超出数组范围时),可以选择忽略部分数据或者采用补零的方式进行填充。 #### 实现代码 以下是基于 Python均值平滑算法实现: ```python def mean_smoothing(data, window_size): """ 对输入的数据列表执行均值平滑操作。 参数: data (list): 输入的一维数据列表。 window_size (int): 平滑窗口的大小。 返回: list: 经过均值平滑处理后的数据列表。 """ smoothed_data = [] half_window = window_size // 2 for i in range(len(data)): start_index = max(0, i - half_window) end_index = min(len(data), i + half_window + 1) current_window = data[start_index:end_index] smoothed_value = sum(current_window) / len(current_window) smoothed_data.append(smoothed_value) return smoothed_data # 示例用法 data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] window_size = 3 smoothed_result = mean_smoothing(data, window_size) print("Smoothed Data:", smoothed_result) ``` 上述代码定义了一个函数 `mean_smoothing`,接受两个参数:待处理的数据列表和窗口大小。通过遍历每个数据点及其周围邻居区域,计算局部平均值作为新的数据点。 #### 图像处理中的应用 在图像领域,均值滤波通常应用于二维矩阵形式的像素阵列上。其基本思路是对目标像素周围的固定区域内所有像素求平均值,并以此替换原位置上的数值[^2]。这种方法能够有效抑制高斯白噪等类型的干扰项,但也可能造成边缘锐度下降等问题[^3]。 #### 点云数据的应用 针对三维空间里的离散点集合——即所谓“点云”,也可以运用类似的策略来进行降噪和平整化工作。例如,在 Point Cloud Library(PCL) 中就提供了专门面向此类任务的功能模块[^4]。用户只需指定合适的体素尺寸即可灵活调节最终呈现出来的细腻程度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值