Category: Image Processing


Line draw in image

draw a line between two point & the line color will be black.

public Bitmap line(int x, int y, int x1, int y1,Bitmap b)
{
int i, j;
double m,t1,t2,t3,t4,bbb;

if (x > x1)
{
i = x;
x = x1;
x1 = i;
i = y;
y = y1;
y1 = i;
}

t1 = x;
t2 = y;
t3 = x1;
t4 = y1;
m=(t4-t2)/(t3-t1);
bbb = t2 – m * t1;
b.SetPixel(x,y,Color.Black);

for (i = x; i <= x1; i++)
{
t1 = m *Convert.ToDouble(i) + bbb;
j =Convert.ToInt16(t1);
b.SetPixel(i,j,Color.Black);
}

return b;
}

Make skin color black & others white

private  Bitmap skin_color_segmentation(Bitmap bm)
{
Bitmap bmp = new Bitmap(bm.Width, bm.Height);

double tot_pixel = bm.Height * bm.Width;
tot_pixel /= 100;
tot_pixel *= 10;
int min_x = bm.Width + 5;
int max_x = 0;
int max_y = 0;
int min_y = bm.Height + 5;

Color color = new Color();
double g, r, avg = 0;
double f_upper, f_lower, w, l;
bool R1, R2, R3, R4, s;
double c, cr, cb;
R1 = R3 = R4 = s = false;
R2 = true;

int cr_start = 140;
int cr_end = 170;
int cb_start = 105;
int cb_end = 150;

for (int i = 0; i < bm.Width; i++)
{
for (int j = 0; j < bm.Height; j++)
{
color = bm.GetPixel(i, j);
r = Convert.ToDouble(color.R) / Convert.ToDouble(color.R + color.G + color.B);
g = Convert.ToDouble(color.G) / Convert.ToDouble(color.R + color.G + color.B);

f_upper = -1.3767 * r * r + 1.0743 * r + 0.1452;
f_lower = -0.776 * r * r + 0.5601 * r + 0.1766;

if (g > f_lower && g < f_upper)
R1 = true;
else
R1 = false;

w = Math.Pow((r – 0.33), 2) + Math.Pow((g – 0.33), 2);

if (w <= 0.0004)
{
R2 = true;
avg++;
}
else
R2 = false;

if (color.R > color.G && color.G > color.B)
R3 = true;
else
R3 = false;

if ((color.R – color.G) >= 45)
R4 = true;
else
R4 = false;

if (R3 && R4)
s = true;
else
s = false;

c = 0.257 * Convert.ToDouble(color.R) + 0.504 * color.G + 0.098 * color.B + 16;
cb = 0.148 * Convert.ToDouble(color.R) – 0.291 * Convert.ToDouble(color.G) + 0.439 * Convert.ToDouble(color.B) + 128;
cr = 0.439 * Convert.ToDouble(color.R) – 0.368 * Convert.ToDouble(color.G) – 0.071 * Convert.ToDouble(color.B) + 128;

if (s)
{
bmp.SetPixel(i, j, Color.Black);
R1 = R3 = R4 = s = false;
R2 = true;

if (i < bm.Width / 2 && i < min_x)
{
min_x = i;
}
if ((i >= bm.Width / 2 && i < bm.Width) && i > max_x)
{
max_x = i;
}

if (j < bm.Height / 2 && j < min_y)
{
min_y = j;
}
if ((j >= bm.Height / 2 && i < bm.Height) && j > max_y)
{
max_y = j;
}
}
else
bmp.SetPixel(i, j, Color.White);
}
}
return bmp;
}

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++;
}

}

public Bitmap contrast_function(Bitmap b)
{
double nContrast = 30;
double pixel = 0, contrast = (100.0 + nContrast) / 100.0;

contrast *= contrast;

int red, green, blue;

// GDI+ still lies to us – the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;

unsafe
{
byte* p = (byte*)(void*)Scan0;

int nOffset = stride – b.Width * 3;

for (int y = 0; y < b.Height; ++y)
{
for (int x = 0; x < b.Width; ++x)
{
blue = p[0];
green = p[1];
red = p[2];

pixel = red / 255.0;
pixel -= 0.5;
pixel *= contrast;
pixel += 0.5;
pixel *= 255;
if (pixel < 0) pixel = 0;
if (pixel > 255) pixel = 255;
p[2] = (byte)pixel;

pixel = green / 255.0;
pixel -= 0.5;
pixel *= contrast;
pixel += 0.5;
pixel *= 255;
if (pixel < 0) pixel = 0;
if (pixel > 255) pixel = 255;
p[1] = (byte)pixel;

pixel = blue / 255.0;
pixel -= 0.5;
pixel *= contrast;
pixel += 0.5;
pixel *= 255;
if (pixel < 0) pixel = 0;
if (pixel > 255) pixel = 255;
p[0] = (byte)pixel;

p += 3;
}
p += nOffset;
}
}

b.UnlockBits(bmData);
return b;

}

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++;
}

public Bitmap black_and_white(Bitmap b)
{
int A, B, C, c;
int limit = 110;
for (int i = 1; i < b.Height; i++) // loop for the image pixels height
{
for (int j = 1; j < b.Width; j++) // loop for the image pixels width
{
Color col;
col = b.GetPixel(j, i);

A = Convert.ToInt32(col.R);
B = Convert.ToInt32(col.G);
C = Convert.ToInt32(col.B);
if (A > limit || B > limit || C > limit)
c = 255;
else
c = 0;

if (c == 0)
b.SetPixel(j, i, Color.Black);
else
b.SetPixel(j, i, Color.White);

}
}
return b;
}

public Bitmap gray(Bitmap b)
{
for (int i = 1; i < b.Height; i++) // loop for the image pixels height
{
for (int j = 1; j < b.Width; j++) // loop for the image pixels width
{
Color col;
col = b.GetPixel(j, i);
b.SetPixel(j, i, Color.FromArgb((col.R + col.G + col.B) / 3, (col.R + col.G + col.B) / 3, (col.R + col.G + col.B) / 3));
}
}
return b;
}

class Prewitt
{
public Bitmap apply(Bitmap b)
{
int[,] gx = new int[,] { { 1, 1, 1 }, { 0, 0, 0 }, { -1, -1, -1 } }; // The matrix Gx
int[,] gy = new int[,] { { -1, 0, 1 }, { -1, 0, 1 }, { -1, 0, 1 } }; // The matrix Gy
Bitmap b1 = new Bitmap(b.Width,b.Height);
for (int i = 1; i < b.Height – 1; i++) // loop for the image pixels height
{
for (int j = 1; j < b.Width – 1; j++) // loop for image pixels width
{
float new_x = 0, new_y = 0;
float c;
for (int hw = -1; hw < 2; hw++) //loop for cov matrix
{
for (int wi = -1; wi < 2; wi++)
{
c = (b.GetPixel(j + wi, i + hw).B + b.GetPixel(j + wi, i + hw).R + b.GetPixel(j + wi, i + hw).G) / 3;

new_x += gx[hw + 1, wi + 1] * c;
new_y += gy[hw + 1, wi + 1] * c;
}
}
if (new_x * new_x + new_y * new_y > 128 * 100)
b1.SetPixel(j, i, Color.Black);
else
b1.SetPixel(j, i, Color.White);
}
}

return b1;
}
}

public Bitmap sobel(Bitmap b)
{
int[,] gx = new int[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } }; // The matrix Gx
int[,] gy = new int[,] { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } }; // The matrix Gy

Bitmap b1 = new Bitmap(b.Width,b.Height);
for (int i = 1; i < b.Height – 1; i++) // loop for the image pixels height
{
for (int j = 1; j < b.Width – 1; j++) // loop for image pixels width
{
float new_x = 0, new_y = 0;
float c;
for (int hw = -1; hw < 2; hw++) //loop for cov matrix
{
for (int wi = -1; wi < 2; wi++)
{
c = (b.GetPixel(j + wi, i + hw).B + b.GetPixel(j + wi, i + hw).R + b.GetPixel(j + wi, i + hw).G) / 3;
new_x += gx[hw + 1, wi + 1] * c;
new_y += gy[hw + 1, wi + 1] * c;
}
}
if (new_x * new_x + new_y * new_y > 128 * 128)
b1.SetPixel(j, i, Color.Black);
else
b1.SetPixel(j, i, Color.White);
}
}

return b1;
}

int val = 90;

// this is the Thresold value, if we change it, output will be different & for different situation we can change it.
public Bitmap My_Edge_Detection(Bitmap b)
{
int n, m;
n = b.Height;
m = b.Width;
int cc = 0, total = 0;
Bitmap B1 = new Bitmap(m, n);
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
{
B1.SetPixel(i, j, Color.White);
}

for (int i = 1; i < m – 1; i++)
for (int j = 1; j < n – 1; j++)
{
total = Math.Abs(b.GetPixel(i + 1, j + 1).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i + 1, j + 1).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i + 1, j + 1).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i, j + 1).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i, j + 1).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i, j + 1).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i + 1, j).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i + 1, j).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i + 1, j).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i – 1, j – 1).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i – 1, j – 1).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i – 1, j – 1).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i, j – 1).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i, j – 1).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i, j – 1).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i – 1, j).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i – 1, j).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i – 1, j).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i – 1, j + 1).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i – 1, j + 1).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i – 1, j + 1).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

total = Math.Abs(b.GetPixel(i + 1, j – 1).B – b.GetPixel(i, j).B) + Math.Abs(b.GetPixel(i + 1, j – 1).R – b.GetPixel(i, j).R) + Math.Abs(b.GetPixel(i + 1, j – 1).G – b.GetPixel(i, j).G);
if (total > val)
{ B1.SetPixel(i, j, Color.Black); continue; }

}

return B1;
}