Big connected component works on a black-white image & find the big connected black region.

Bitmap Bit;

int[][] big;

int[] count;

public Bitmap big_conect(Bitmap b)
{

int start = 1, i, j;
Bit = new Bitmap(b);

int hight = Bit.Height;
int weight = Bit.Width;

big = new int[weight + 5][];
count = new int[1000];

for (i = 0; i < weight + 5; i++)
{
big[i] = new int[hight + 5];
for (j = 0; j < hight + 5; j++)
big[i][j] = 0;
}
int st = 0;
for (i = 0; i < weight; i++)
for (j = 0; j < hight; j++)
if (big[i][j] == 0 && Bit.GetPixel(i, j).B == 0 && Bit.GetPixel(i, j).R == 0 && Bit.GetPixel(i, j).G == 0)
{
count[start] = 1;
st = 1;
big[i][j] = start;
conect(i, j, weight, hight);
start++;
}

int max = 0, value = 1;
for (i = 1; i < start; i++)
if (count[i] > max)
{
max = count[i];
value = i;
}

for (i = 0; i < weight; i++)
for (j = 0; j < hight; j++)
{
if (big[i][j] == value)
Bit.SetPixel(i, j, Color.Black);
else
Bit.SetPixel(i, j, Color.White);
}
return Bit;
}

public void conect(int x, int y, int w, int h) // Mainly BFS
{
int[] bfs_x = new int[w * h];
int[] bfs_y = new int[w * h];
int n, m;
bfs_x[0] = x;
bfs_y[0] = y;
n = 0;
m = 1;

while (n != m)
{
x = bfs_x[n];
y = bfs_y[n];

if (x – 1 >= 0)
if (Bit.GetPixel(x – 1, y).B == 0 && Bit.GetPixel(x – 1, y).R == 0 && Bit.GetPixel(x – 1, y).G == 0 && big[x – 1][y] == 0)
{
count[big[x][y]]++;
big[x – 1][y] = big[x][y];
bfs_x[m] = x – 1;
bfs_y[m] = y;
m++;
}
if (y – 1 >= 0)
if (Bit.GetPixel(x, y – 1).B == 0 && Bit.GetPixel(x, y – 1).R == 0 && Bit.GetPixel(x, y – 1).G == 0 && big[x][y – 1] == 0)
{
count[big[x][y]]++;
big[x][y – 1] = big[x][y];
bfs_x[m] = x;
bfs_y[m] = y – 1;
m++;
}
if (x + 1 < w)
if (Bit.GetPixel(x + 1, y).B == 0 && Bit.GetPixel(x + 1, y).R == 0 && Bit.GetPixel(x + 1, y).G == 0 && big[x + 1][y] == 0)
{
count[big[x][y]]++;
big[x + 1][y] = big[x][y];
bfs_x[m] = x + 1;
bfs_y[m] = y;
m++;
}
if (y + 1 < h)
if (Bit.GetPixel(x, y + 1).B == 0 && Bit.GetPixel(x, y + 1).R == 0 && Bit.GetPixel(x, y + 1).G == 0 && big[x][y + 1] == 0)
{
count[big[x][y]]++;
big[x][y + 1] = big[x][y];
bfs_x[m] = x;
bfs_y[m] = y + 1;
m++;
}

if (x – 1 >= 0 && y – 1 >= 0)
if (Bit.GetPixel(x – 1, y – 1).B == 0 && Bit.GetPixel(x – 1, y – 1).R == 0 && Bit.GetPixel(x – 1, y – 1).G == 0 && big[x – 1][y – 1] == 0)
{
count[big[x][y]]++;
big[x – 1][y – 1] = big[x][y];
bfs_x[m] = x – 1;
bfs_y[m] = y – 1;
m++;
}
if (x + 1 < w && y – 1 >= 0)
if (Bit.GetPixel(x + 1, y – 1).B == 0 && Bit.GetPixel(x + 1, y – 1).R == 0 && Bit.GetPixel(x + 1, y – 1).G == 0 && big[x + 1][y – 1] == 0)
{
count[big[x][y]]++;
big[x + 1][y – 1] = big[x][y];
bfs_x[m] = x + 1;
bfs_y[m] = y – 1;
m++;
}
if (x + 1 < w && y + 1 < h)
if (Bit.GetPixel(x + 1, y + 1).B == 0 && Bit.GetPixel(x + 1, y + 1).R == 0 && Bit.GetPixel(x + 1, y + 1).G == 0 && big[x + 1][y + 1] == 0)
{
count[big[x][y]]++;
big[x + 1][y + 1] = big[x][y];
bfs_x[m] = x + 1;
bfs_y[m] = y + 1;
m++;
}
if (x – 1 >= 0 && y + 1 < h)
if (Bit.GetPixel(x – 1, y + 1).B == 0 && Bit.GetPixel(x – 1, y + 1).R == 0 && Bit.GetPixel(x – 1, y + 1).G == 0 && big[x – 1][y + 1] == 0)
{
count[big[x][y]]++;
big[x – 1][y + 1] = big[x][y];
bfs_x[m] = x – 1;
bfs_y[m] = y + 1;
m++;
}
n++;
}

}

Advertisements