Introduction of Mean value filter for image processing and implementation of C algorithm

Source: Internet
Author: User

1Introduction to mean-value filtering

Filtering is the operation of filtering a particular band frequency in a signal, which is a technique for extracting useful signals from a receiving signal containing interference.

Mean filter is a typical linear filtering algorithm, which refers to the image of the target pixel to a template, the template includes its surrounding neighboring pixels (such as the 3x3 template: The target pixel centered around 8 pixels, constitute a filter template, that is, remove the target pixel itself), The average of all the pixels in the template is used instead of the original pixel values.

Mean filter effect: Smoothing linear filter processing reduces the "sharp" change of the image. Because typical random noises are made up of drastic changes in the gray scale, the common application of smoothing is to reduce noise. The main application of the mean filter is to remove irrelevant details in the image, where "irrelevant" refers to a smaller pixel area than the size of the filter template. However, because the edge of image is also caused by the sharp change of the image grayscale, the mean filter processing still has the negative effect of edge ambiguity.

2The realization of the mean-value filter algorithm(Clanguage)
1 //junzhilvbo.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include"stdlib.h"6#include"string.h"7 8 #defineData_x 256//number of horizontal pixels in Digital image9 #defineData_y 256//number of vertical pixels in a digital imageTen  One voidOpenFile (Const Char*cfilepath,intnoriginaldata[data_y][data_x]) A { -printf"getting data ... \ n"); -FILE *FP; thefp = fopen (Cfilepath,"R"); -     if(NULL = =FP) -     { -printf"Open file failed! \ n"); +         return ; -     } +  AUnsignedChar*pdata = (unsignedChar*)malloc(sizeof(unsignedChar) *data_x*data_y); at     if(NULL = =pData) -     { -printf"Memory malloc failed!\n"); -         return ; -     } -  inFread (PData,sizeof(unsignedChar) *data_x*data_y,1, FP); -  to     intcount_x =0 ; +     intCount_y =0 ; -  the      for(; count_y < data_y; count_y++) *     { $          for(; count_x < data_x; count_x++)Panax Notoginseng         { -Noriginaldata[count_y][count_x] = pdata[count_y*data_y+count_x]; the         } +     } A  the      Free(pData); + fclose (FP);  -  $     return ; $ } -  - voidSaveFile (Const Char*cfilepath,intnresultdata[data_y][data_x]) the { -printf"saving data ... \ n");Wuyi     intcount_x,count_y; the  -FILE *FP; Wufp = fopen (Cfilepath,"W"); -     if(NULL = =FP) About     { $printf"Open file failed! \ n"); -         return ; -     } -  A      for(count_y=0; count_y<data_y;count_y++)  +     { the          for(count_x=0; count_x<data_x;count_x++)           -         { $Fwrite (&nresultdata[count_y][count_x],1,1, FP); the         } the     } the          the fclose (FP);  -printf"file saved successfully! \ n"); in  the     return ; the } About  the BOOLJunzhilvbo (Const intNoriginaldata[data_y][data_x],intnresultdata[data_y][data_x]) the { theprintf"mean filtering is in progress ... \ n"); +     intcount_x, count_y; -      the     /*3*3 template filter calculation, edge pixels are not computed*/Bayi      for(count_y =1; Count_y < data_y; count_y++) the     { the          for(count_x =1; count_x < data_x; count_x++) -         { -Nresultdata[count_y][count_x] = (int) ((noriginaldata[count_y-1][count_x-1]+ thenoriginaldata[count_y-1][COUNT_X] + thenoriginaldata[count_y-1][count_x+1]+ thenoriginaldata[count_y][count_x-1]  + theNORIGINALDATA[COUNT_Y][COUNT_X] + -noriginaldata[count_y][count_x+1]  + thenoriginaldata[count_y+1][count_x-1]+ thenoriginaldata[count_y+1][COUNT_X] + thenoriginaldata[count_y+1][count_x+1])/9); 94         } the     } the  the     /*assigning four edges directly to a value processing*/98      for(count_x=0; count_x<data_x;count_x++)//Horizontal edge pixels equal to the original pixel grayscale value About     { -nresultdata[0][count_x]=noriginaldata[0][count_x];101nresultdata[data_y-1][count_x]=noriginaldata[data_y-1][count_x];102     }103      for(count_y=1; count_y<data_y-1; count_y++)//Vertical edge pixels equal to the original pixel grayscale value104     { thenresultdata[count_y][0]=noriginaldata[count_y][0];106nresultdata[count_y][data_x-1]=noriginaldata[count_y][data_x-1];107     }108 109     return true ; the }111  the int_tmain (intARGC, _tchar*argv[])113 { the     intNORIGINALDATA[DATA_Y][DATA_X];//Save original image grayscale values the     intNRESULTDATA[DATA_Y][DATA_X];//preserving the grayscale value after filtering the 117memset (Noriginaldata,0,sizeof(Noriginaldata));//initializing an array118memset (Nresultdata,0,sizeof(Nresultdata)); 119  -     CharCopenfilepath[] ="Lena.raw";//image file Path121 122 OpenFile (Copenfilepath,noriginaldata); 123     124     if(! Junzhilvbo (Noriginaldata,nresultdata))//Filter Calculation the     {126printf"operation failed!\n");127         return 0; -     }129  the     CharCsavefilepath[] ="Result.raw";//File Save path131  the SaveFile (csavefilepath,nresultdata);133 134     return 0;135}
3 mean filter algorithm effect comparison

Before mean filter : after mean filter :

Introduction of Mean value filter for image processing and implementation of C algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.