private int[][] A;
private int[] B;
private int count;
private Bitmap sak;
private Bitmap noise_remove(Bitmap b)
{
int i, j, h, w;
h = b.Height;
w = b.Width;
A = new int[w][];

sak = new Bitmap(b);

for (i = 0; i < w; i++)
{
A[i] = new int[h];
for (j = 0; j < h; j++)
A[i][j] = 0;
}

B= new int[(h*w)/4];
count = 1;

for (i = 0; i < w; i++)
for (j = 0; j < h; j++)
if(A[i][j]==0)
{
continuous_check(i, j);
}
int limit;
limit = (w * h)/50000;
if (limit < 3)
limit = 3;
int p, q;

for (i = 1; i < w-1; i++)
for (j = 1; j < h-1; j++)
if(B[A[i][j]]<limit)
{
p = i – 1;
q = j;
if (A[i-1][j-1] > A[p][q])
{
p = i – 1;
q = j – 1;
}
if (A[i][j – 1] > A[p][q])
{
p = i;
q = j – 1;
}
if (A[i + 1][j – 1] > A[p][q])
{
p = i + 1;
q = j – 1;
}
if (A[i + 1][j] > A[p][q])
{
p = i + 1;
q = j;
}
if (A[i – 1][j + 1] > A[p][q])
{
p = i – 1;
q = j + 1;
}
if (A[i][j + 1] > A[p][q])
{
p = i;
q = j + 1;
}
if (A[i + 1][j + 1] > A[p][q])
{
p = i + 1;
q = j + 1;
}

b.SetPixel(i,j,b.GetPixel(p,q));
}
return b;
}

void continuous_check(int h1, int h2)
{
B[count] = 1;
int mhn = 10;
int w,h,n,m;
w=sak.Width;
h=sak.Height;
A[h1][h2]=count;
int[] x;
int[] y;
x= new int[w*h];
y = new int[w*h];
x[0] = h1;
y[0] = h2;
n = 1;
m = 0;
int r, g, b;
int p, q;
while (n != m)
{
r = sak.GetPixel(x[m], y[m]).R;
g = sak.GetPixel(x[m], y[m]).G;
b = sak.GetPixel(x[m], y[m]).B;

p = x[m] + 1;
q = y[m];
if(0<=p&&p<w&&0<=q&&q<h)
if (A[p][q] == 0)
{
if (Math.Abs(r – sak.GetPixel(p, q).R) < mhn && Math.Abs(g – sak.GetPixel(p, q).G) < mhn && Math.Abs(b – sak.GetPixel(p, q).B) < mhn)
{
A[p][q] = count;
B[count]++;
x[n] = p;
y[n] = q;
n++;
}
}

p = x[m];
q = y[m] + 1;
if (0 <= p && p < w && 0 <= q && q < h)
if (A[p][q] == 0)
{
if (Math.Abs(r – sak.GetPixel(p, q).R) < mhn && Math.Abs(g – sak.GetPixel(p, q).G) < mhn && Math.Abs(b – sak.GetPixel(p, q).B) < mhn)
{
A[p][q] = count;
B[count]++;
x[n] = p;
y[n] = q;
n++;
}
}

p = x[m] – 1;
q = y[m];
if (0 <= p && p < w && 0 <= q && q < h)
if (A[p][q] == 0)
{
if (Math.Abs(r – sak.GetPixel(p, q).R) < mhn && Math.Abs(g – sak.GetPixel(p, q).G) < mhn && Math.Abs(b – sak.GetPixel(p, q).B) < mhn)
{
A[p][q] = count;
B[count]++;
x[n] = p;
y[n] = q;
n++;
}
}

p = x[m];
q = y[m] – 1;
if (0 <= p && p < w && 0 <= q && q < h)
if (A[p][q] == 0)
{
if (Math.Abs(r – sak.GetPixel(p, q).R) < mhn && Math.Abs(g – sak.GetPixel(p, q).G) < mhn && Math.Abs(b – sak.GetPixel(p, q).B) < mhn)
{
A[p][q] = count;
B[count]++;
x[n] = p;
y[n] = q;
n++;
}
}

p = x[m] + 1;
q = y[m] + 1;
if (0 <= p && p < w && 0 <= q && q < h)
if (A[p][q] == 0)
{
if (Math.Abs(r – sak.GetPixel(p, q).R) < mhn && Math.Abs(g – sak.GetPixel(p, q).G) < mhn && Math.Abs(b – sak.GetPixel(p, q).B) < mhn)
{
A[p][q] = count;
B[count]++;
x[n] = p;
y[n] = q;
n++;
}
}

p = x[m] + 1;
q = y[m] – 1;
if (0 <= p && p < w && 0 <= q && q < h)
if (A[p][q] == 0)
{
if (Math.Abs(r – sak.GetPixel(p, q).R) < mhn && Math.Abs(g – sak.GetPixel(p, q).G) < mhn && Math.Abs(b – sak.GetPixel(p, q).B) < mhn)
{
A[p][q] = count;
B[count]++;
x[n] = p;
y[n] = q;
n++;
}
}

p = x[m] – 1;
q = y[m] + 1;
if (0 <= p && p < w && 0 <= q && q < h)
if (A[p][q] == 0)
{
if (Math.Abs(r – sak.GetPixel(p, q).R) < mhn && Math.Abs(g – sak.GetPixel(p, q).G) < mhn && Math.Abs(b – sak.GetPixel(p, q).B) < mhn)
{
A[p][q] = count;
B[count]++;
x[n] = p;
y[n] = q;
n++;
}
}

p = x[m] – 1;
q = y[m] – 1;
if (0 <= p && p < w && 0 <= q && q < h)
if (A[p][q] == 0)
{
if (Math.Abs(r – sak.GetPixel(p, q).R) < mhn && Math.Abs(g – sak.GetPixel(p, q).G) < mhn && Math.Abs(b – sak.GetPixel(p, q).B) < mhn)
{
A[p][q] = count;
B[count]++;
x[n] = p;
y[n] = q;
n++;
}
}

m++;
}

count++;
}

Advertisements