c语言程序图片马赛克,关于c语言的图像均值滤波 请问大神为什么我的结果都是马赛克...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

#include

#include

#include

#include "stdlib.h"

#include "string.h"

#define width 256

#define higth 256

//原图象的宽度和高度

int lvbo(unsigned char D[])

{

int a;

a=(D[0]+D[1]+D[2]+D[3]+D[4]+D[5]+D[6]+D[7]+D[8]+D[9]+D[10]+D[11]+D[12]+D[13]+D[14]+D[15]+D[16]+D[17]+D[18]+D[19]+D[20]+D[21]+D[22]+D[23]+D[24])/25;

//printf("%d",a);

return a;

}

void main()

{

FILE *fp,*newfp;

int i,j;

unsigned char D[25];//定义选取框

unsigned char buffer[54+1024];//定义原图像头缓冲区

unsigned long length=width*higth;//图像的总象素个数

unsigned char readData[higth][width]; //用于存储原图数据的数组

unsigned char writeData[higth][width]; //用于存储原图数据的数组

if(!(fp=fopen("C:\\Users\\Alan\\Desktop\\pic\\line.jpg","rb")))

{

printf("Open file1 error!\n");

return ;

}

if(!(newfp=fopen("C:\\Users\\Alan\\Desktop\\pic\\2.jpg","wb")))

{

printf("Open file2 error!\n");

return ;

}

fread(buffer,1,54+1024,fp);//读取文件头54个字节

fread(&readData[0][0], sizeof(unsigned char),length, fp);//从原图读入数据

for(i=0;i

{

for(j=0;j

{

writeData[i][j]=readData[i][j];

}

}

for(i=2;i

{

for(j=2;j

{

D[0]=readData[i-2][j-2];

D[1]=readData[i-2][j-1];

D[2]=readData[i-2][j];

D[3]=readData[i-2][j+1];

D[4]=readData[i-2][j+2];

D[5]=readData[i-1][j-2];

D[6]=readData[i-1][j-1];

D[7]=readData[i-1][j];

D[8]=readData[i-1][j+1];

D[9]=readData[i-1][j+2];

D[10]=readData[i][j-2];

D[11]=readData[i][j-1];

D[12]=readData[i][j];

D[13]=readData[i][j+1];

D[14]=readData[i][j+2];

D[15]=readData[i+1][j-2];

D[16]=readData[i+1][j-1];

D[17]=readData[i+1][j];

D[18]=readData[i+1][j+1];

D[19]=readData[i+1][j+2];

D[20]=readData[i+2][j-2];

D[21]=readData[i+2][j-1];

D[22]=readData[i+2][j];

D[23]=readData[i+2][j+1];

D[24]=readData[i+2][j+2];

writeData[i][j]=lvbo(D);

}

}

fwrite(buffer,sizeof(unsigned char),54+1024,newfp);

fwrite(writeData,sizeof(unsigned char),length,newfp);

fclose(newfp);

fclose(fp);

printf("success\n");

return ;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 C 语言程序,可以实现图像均值滤波: ```c #include <stdio.h> #include <stdlib.h> #define WIDTH 640 // 图像宽度 #define HEIGHT 480 // 图像高度 // 读取图像数据 void read_image(unsigned char *data) { FILE *fp; fp = fopen("image.raw", "rb"); fread(data, 1, WIDTH*HEIGHT, fp); fclose(fp); } // 保存图像数据 void save_image(unsigned char *data) { FILE *fp; fp = fopen("image_filtered.raw", "wb"); fwrite(data, 1, WIDTH*HEIGHT, fp); fclose(fp); } // 均值滤波 void mean_filter(unsigned char *src, unsigned char *dst) { int i, j, k, l; int sum; for(i = 1; i < HEIGHT-1; i++) { for(j = 1; j < WIDTH-1; j++) { sum = 0; for(k = -1; k <= 1; k++) { for(l = -1; l <= 1; l++) { sum += src[(i+k)*WIDTH+j+l]; } } dst[i*WIDTH+j] = (unsigned char)(sum/9); } } } int main() { unsigned char *image = (unsigned char *)malloc(WIDTH*HEIGHT); unsigned char *filtered_image = (unsigned char *)malloc(WIDTH*HEIGHT); // 读取图像数据 read_image(image); // 均值滤波 mean_filter(image, filtered_image); // 保存滤波后的图像数据 save_image(filtered_image); free(image); free(filtered_image); return 0; } ``` 上面的程序中,`read_image` 函数从文件中读取图像数据,`mean_filter` 函数对图像进行均值滤波,`save_image` 函数将滤波后的图像数据保存到文件中。程序中使用了一个 `unsigned char` 类型的数组来存储图像数据,数组大小为图像宽度乘以高度。在均值滤波函数中,使用了一个 3x3 的卷积核,对每个像素点取周围 9 个像素点的平均值作为该像素点的值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值