Median filter and mean filter C + + code

Source: Internet
Author: User

mean filter and median filter code

2008-11-24 16:07:36| Category: Programming | Report | Font size Subscription

------------------mean-value filter

BOOL Filterav (unsigned char *image,int height,int width)

{

int i,j;

unsigned char *p= (unsigned char*) malloc (height*width);

for (i=1;i

{

for (j=1;j<width-1;j++)

{

p[i*width+j]= (unsigned char) (((int) image[(i-1) *width+j-1]

+ (int) image[(i-1) *width+j]

+ (int) image[(i-1) *width+j+1]

+ (int) image[i*width+j-1]

+ (int) image[i*width+j]

+ (int) image[i*width+j+1]

+ (int) image[(i+1) *width+j-1]

+ (int) image[(i+1) *width+j]

+ (int) image[(i+1) *width+j+1])/9);

}

}

for (i=1;i

{

for (j=1;j<width-1;j++)

{

IMAGE[I*WIDTH+J]=P[I*WIDTH+J];

}

}

Free (p);

return true;

}

----------------------------Median Filter

BOOL Filtermid (unsigned char *image,int height,int width)

{

int i,j,k,l;

int POS;

unsigned char temp;

unsigned char psr[9];

unsigned char *p= (unsigned char*) malloc (height*width);

for (i=1;i

{

for (j=1;j<width-1;j++)

{//---3*3 window matrix

psr[0]=image[(i-1) *width+j-1];

psr[1]=image[(i-1) *width+j];

psr[2]=image[(i-1) *width+j+1];

PSR[3]=IMAGE[I*WIDTH+J-1];

PSR[4]=IMAGE[I*WIDTH+J];

PSR[5]=IMAGE[I*WIDTH+J+1];

psr[6]=image[(i+1) *width+j-1];

psr[7]=image[(i+1) *width+j];

psr[8]=image[(i+1) *width+j+1];

--------Select Sort

for (k=0;k<9;k++)

{

Pos=k;

for (l=k;l<9;l++)

{

if (Psr[l]<psr[pos])

Pos=l;

}

TEMP=PSR[K];

Psr[k]=psr[pos];

Psr[pos]=temp;

}

------Take the median value

P[I*WIDTH+J]=PSR[4];

}

}

for (i=1;i

{

for (j=1;j<width-1;j++)

{

IMAGE[I*WIDTH+J]=P[I*WIDTH+J];

}

}

Free (p);

return true;

}

Median filter and mean filter C + + code

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.