数字图像处理·均值滤波

题目信息

对给定灰度图像src,按照给定的滤波器大小进行均值滤波,输出图像滤波结果dst。
要求:
1.对图像边界处的运算,在范围外的图像,内容按零处理。
2.运算结束需要四舍五入,按整型数输出结果,不用对数据范围进行处理,即输出可以大于255.
滤波器大小为奇数。

vector<vector<int>> blur(vector<vector<int>>src,  
    int height_filter,
    int width_filter)

src:待处理的图像 int[]
dst:处理结果
height_src:输入图像的高。即src.size()
width_src:输入图像的宽。即src[0].size()。
height_filter:滤波器的高。
width_filter:滤波器的宽。

1 <= src.size() <= 10000
1 <= src[0].size() <= 10000
1 <= height_filter <= 10000
1 <= width_filter <= 10000

输入

height_src,
width_src,
heiht_filter,
width_filter,
height_src行 width_src列的图像矩阵数据。

输出

height_src行 width_src列的均值滤波结果。(空格相隔)

测试样例

5 5
3 3
9	0	6	2	2
9	7	6	2	10
3	5	9	9	7
7	5	8	0	5
2	9	6	5	5
3 4 3 3 2 
4 6 5 6 4 
4 7 6 6 4 
3 6 6 6 3 
3 4 4 3 2 

解答

#include <iostream>
#include <vector>

using namespace std;

int int_part(double x)
{
    return (int) (x + 0.5);
}

vector<vector<int>> blur(vector<vector<int>> src,
                         int height_filter,
                         int width_filter)
{
    int height_side = (height_filter - 1) / 2;
    int width_side = (width_filter - 1) / 2;

    int height_src = src.size();
    int width_src = src[0].size();

    double sum = height_filter * width_filter;

    vector<vector<int>> dst;
    for (int i = 0; i < height_src; i++)
    {
        vector<int> line;
        for (int j = 0; j < width_src; j++)
        {
            int num = 0;
            for (int p = i - height_side; p <= i + height_side; p++)
            {
                for (int q = j - width_side; q <= j + width_side; q++)
                {
                    if (p < 0 || q < 0 || p >= height_src || q >= width_src)
                    {
                        num += 0;
                    }
                    else
                    {
                        num += src[p][q];
                    }
                }
            }
            line.push_back(int_part(num / sum));
        }
        dst.push_back(line);
    }
    return dst;
}

int main()
{
    freopen("/Users/zhj/Downloads/test.txt", "r", stdin);

    int height_src;
    int width_src;
    int height_filter;
    int width_filter;
    cin >> height_src >> width_src >> height_filter >> width_filter;

    vector<vector<int>> src;
    for (int i = 0; i < height_src; i++)
    {
        vector<int> line;
        for (int j = 0; j < width_src; j++)
        {
            int tmp;
            cin >> tmp;
            line.push_back(tmp);
        }
        src.push_back(line);
    }

    vector<vector<int>> dst = blur(src, height_filter, width_filter);

    for (unsigned int i = 0; i < src.size(); i++)
    {
        for (unsigned int j = 0; j < src[0].size(); j++)
        {
            cout << dst[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

zhj12399

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值