图像尺寸可调的均值滤波C语言
时间: 2023-09-29 20:02:44 浏览: 44
实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char byte;
byte* loadImage(FILE* f, int* width, int* height) {
// 读取 PGM 文件头
char magic[3];
fgets(magic, 3, f);
if (strcmp(magic, "P5") != 0) {
printf("Unsupported PGM format!\n");
return NULL;
}
char line[256];
fgets(line, 256, f);
while (line[0] == '#') {
fgets(line, 256, f);
}
sscanf(line, "%d %d", width, height);
fgets(line, 256, f);
int maxval = atoi(line);
// 读取 PGM 图像数据
byte* data = (byte*)malloc(*width * *height);
fread(data, 1, *width * *height, f);
return data;
}
void saveImage(FILE* f, byte* data, int width, int height) {
// 写入 PGM 文件头
fprintf(f, "P5\n");
fprintf(f, "# Created by mean filtering\n");
fprintf(f, "%d %d\n", width, height);
fprintf(f, "255\n");
// 写入 PGM 图像数据
fwrite(data, 1, width * height, f);
}
void meanFilter(byte* src, byte* dst, int width, int height, int size) {
// 计算滤波器半径
int radius = size / 2;
// 遍历图像像素
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// 计算滤波器范围
int left = x - radius;
int right = x + radius;
int top = y - radius;
int bottom = y + radius;
// 计算滤波器内像素的平均值
int sum = 0;
int count = 0;
for (int j = top; j <= bottom; j++) {
if (j < 0 || j >= height) {
continue;
}
for (int i = left; i <= right; i++) {
if (i < 0 || i >= width) {
continue;
}
sum += src[j * width + i];
count++;
}
}
dst[y * width + x] = sum / count;
}
}
}
int main(int argc, char** argv) {
// 检查命令行参数
if (argc != 4) {
printf("Usage: %s <input.pgm> <output.pgm> <size>\n", argv[0]);
return 0;
}
// 打开输入文件
FILE* inputFile = fopen(argv[1], "rb");
if (!inputFile) {
printf("Failed to open input file!\n");
return 0;
}
// 读取输入图像
int width, height;
byte* inputImage = loadImage(inputFile, &width, &height);
fclose(inputFile);
if (!inputImage) {
printf("Failed to load input image!\n");
return 0;
}
// 解析滤波器大小
int size = atoi(argv[3]);
if (size < 3 || size % 2 == 0) {
printf("Invalid filter size!\n");
return 0;
}
// 分配输出图像内存
byte* outputImage = (byte*)malloc(width * height);
// 执行均值滤波
meanFilter(inputImage, outputImage, width, height, size);
// 打开输出文件
FILE* outputFile = fopen(argv[2], "wb");
if (!outputFile) {
printf("Failed to open output file!\n");
return 0;
}
// 保存输出图像
saveImage(outputFile, outputImage, width, height);
fclose(outputFile);
// 释放内存
free(inputImage);
free(outputImage);
return 0;
}
```
此代码实现了从 PGM 文件中读取图像、执行可调大小的均值滤波、保存图像到 PGM 文件的功能。可以通过命令行参数指定输入文件、输出文件和滤波器大小。
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)