数字图像处理 均值滤波

import numpy as np
import cv2
import scipy.signal

def convert_2d(r):
    n = 3
    kernel = np.ones((n, n)) / n ** 2
    s = scipy.signal.convolve2d(r,kernel,mode='same',boundary='symm')
    return s.astype(np.uint8)

def convert_3d(r):
    s_dsplit = []
    for d in range(r.shape[2]):
        rr = r[:,:,d]
        ss = convert_2d(rr)
        s_dsplit.append(ss)
    s = np.dstack(s_dsplit)
    return s

im = cv2.imread('tetet.jpg')
im_converted_mat = convert_3d(im)
cv2.imshow('original',im)
cv2.imshow('mean',im_converted_mat)
cv2.imwrite('mean.jpg',im_converted_mat)
cv2.waitKey()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是纯Java实现的算数均值滤波器代码,可以用于数字图像处理中的降噪和模糊处理: ``` public static BufferedImage arithmeticMeanFilter(BufferedImage image, int filterSize) { int width = image.getWidth(); int height = image.getHeight(); int[][] pixels = new int[width][height]; int[][] resultPixels = new int[width][height]; int filterRadius = filterSize / 2; int filterArea = filterSize * filterSize; // 将图像像素存储到二维数组中 for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { pixels[x][y] = image.getRGB(x, y) & 0xff; } } // 对每个像素进行均值滤波 for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int sum = 0; for (int i = -filterRadius; i <= filterRadius; i++) { for (int j = -filterRadius; j <= filterRadius; j++) { int px = Math.min(Math.max(x + i, 0), width - 1); int py = Math.min(Math.max(y + j, 0), height - 1); sum += pixels[px][py]; } } resultPixels[x][y] = sum / filterArea; } } // 将处理后的像素存储到新的BufferedImage中 BufferedImage resultImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { resultImage.setRGB(x, y, resultPixels[x][y] << 16 | resultPixels[x][y] << 8 | resultPixels[x][y]); } } return resultImage; } ``` 其中,`image`为输入的原始图像,`filterSize`为滤波器的大小,返回值为处理后的图像。该算法使用了算数均值滤波器,对于每个像素,计算其周围滤波器大小范围内的像素值的平均值,并将结果作为该像素的新值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值