图像处理之均值滤波介绍及C算法实现
生活随笔
收集整理的這篇文章主要介紹了
图像处理之均值滤波介绍及C算法实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1?均值濾波介紹
????? 濾波是濾波是將信號中特定波段頻率濾除的操作,是從含有干擾的接收信號中提取有用信號的一種技術。
???? 均值濾波是典型的線性濾波算法,它是指在圖像上對目標像素給一個模板,該模板包括了其周圍的臨近像素(如3×3模板:以目標象素為中心的周圍8個象素,構成一個濾波模板,即去掉目標象素本身),再用模板中的全體像素的平均值來代替原來像素值。
????? 均值濾波效果:平滑線性濾波處理降低了圖像的“尖銳”變化。由于典型的隨機噪聲由灰度級的急劇變化組成,因此常見的平滑處理的應用就是降低噪聲。均值濾波器的主要應用是去除圖像中的不相關細節,其中“不相關”是指與濾波器模板尺寸相比較小的像素區域。然而,由于圖像的邊緣也是由圖像灰度的尖銳變化帶來的特性,所以均值濾波處理還是存在著邊緣模糊的負面效應。
2?均值濾波算法實現(C語言)?
1 // junzhilvbo.cpp : 定義控制臺應用程序的入口點。 2 // 3 4 #include "stdafx.h" 5 #include "stdlib.h" 6 #include "string.h" 7 8 #define DATA_X 256 //數字圖像水平像素個數 9 #define DATA_Y 256 //數字圖像豎直像素個數 10 11 void OpenFile(const char *cFilePath , int nOriginalData[DATA_Y][DATA_X]) 12 { 13 printf("正在獲取數據......\n"); 14 FILE *fp ; 15 fp = fopen(cFilePath , "r"); 16 if(NULL == fp) 17 { 18 printf("open file failed! \n"); 19 return ; 20 } 21 22 unsigned char *pData = (unsigned char *)malloc(sizeof(unsigned char)*DATA_X*DATA_Y); 23 if(NULL == pData) 24 { 25 printf("memory malloc failed!\n"); 26 return ; 27 } 28 29 fread(pData , sizeof(unsigned char)*DATA_X*DATA_Y , 1 , fp); 30 31 int count_x = 0 ; 32 int count_y = 0 ; 33 34 for(;count_y < DATA_Y ; count_y++) 35 { 36 for(; count_x < DATA_X ;count_x++) 37 { 38 nOriginalData[count_y][count_x] = pData[count_y*DATA_Y+count_x]; 39 } 40 } 41 42 free(pData); 43 fclose(fp); 44 45 return ; 46 } 47 48 void SaveFile(const char *cFilePath , int nResultData[DATA_Y][DATA_X]) 49 { 50 printf("正在保存數據......\n"); 51 int count_x,count_y; 52 53 FILE *fp ; 54 fp = fopen(cFilePath , "w"); 55 if(NULL == fp) 56 { 57 printf("open file failed! \n"); 58 return ; 59 } 60 61 for(count_y=0;count_y<DATA_Y;count_y++) 62 { 63 for(count_x=0;count_x<DATA_X;count_x++) 64 { 65 fwrite(&nResultData[count_y][count_x],1,1,fp); 66 } 67 } 68 69 fclose(fp); 70 printf("文件保存成功! \n"); 71 72 return ; 73 } 74 75 bool JunZhiLvBo(const int nOriginalData[DATA_Y][DATA_X], int nResultData[DATA_Y][DATA_X]) 76 { 77 printf("正在進行均值濾波......\n"); 78 int count_x ,count_y ; 79 80 /*3*3模版濾波計算,不計算邊緣像素*/ 81 for(count_y = 1 ; count_y < DATA_Y ; count_y++) 82 { 83 for(count_x = 1 ; count_x < DATA_X ;count_x++) 84 { 85 nResultData[count_y][count_x] = (int)((nOriginalData[count_y-1][count_x-1]+ 86 nOriginalData[count_y-1][count_x] + 87 nOriginalData[count_y-1][count_x+1]+ 88 nOriginalData[count_y][count_x-1] + 89 nOriginalData[count_y][count_x] + 90 nOriginalData[count_y][count_x+1] + 91 nOriginalData[count_y+1][count_x-1]+ 92 nOriginalData[count_y+1][count_x] + 93 nOriginalData[count_y+1][count_x+1])/9); 94 } 95 } 96 97 /*對四個邊緣直接進行賦值處理*/ 98 for(count_x=0;count_x<DATA_X;count_x++) //水平邊緣像素等于原來像素灰度值 99 { 100 nResultData[0][count_x]=nOriginalData[0][count_x]; 101 nResultData[DATA_Y-1][count_x]=nOriginalData[DATA_Y-1][count_x]; 102 } 103 for(count_y=1;count_y<DATA_Y-1;count_y++) //豎直邊緣像素等于原來像素灰度值 104 { 105 nResultData[count_y][0]=nOriginalData[count_y][0]; 106 nResultData[count_y][DATA_X-1]=nOriginalData[count_y][DATA_X-1]; 107 } 108 109 return true ; 110 } 111 112 int _tmain(int argc, _TCHAR* argv[]) 113 { 114 int nOriginalData[DATA_Y][DATA_X]; //保存原始圖像灰度值 115 int nResultData[DATA_Y][DATA_X]; //保存濾波后的灰度值 116 117 memset(nOriginalData,0,sizeof(nOriginalData)); //初始化數組 118 memset(nResultData,0,sizeof(nResultData)); 119 120 char cOpenFilePath[] = "Lena.raw"; //圖像文件路徑 121 122 OpenFile(cOpenFilePath,nOriginalData); 123 124 if(!JunZhiLvBo(nOriginalData,nResultData)) //濾波計算 125 { 126 printf("操作失敗!\n"); 127 return 0; 128 } 129 130 char cSaveFilePath[] = "Result.raw"; //文件保存路徑 131 132 SaveFile(cSaveFilePath,nResultData); 133 134 return 0; 135 }3?均值濾波算法效果對比
均值濾波之前:?????????????????????????????? ?均值濾波之后:
?
總結
以上是生活随笔為你收集整理的图像处理之均值滤波介绍及C算法实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像处理之中值滤波介绍及C实现
- 下一篇: 【faster-rcnn】训练自己的数据