Hi. I am implementing the Kuwahara Filter in c#. The code for this is given below.
unsafe
{
Bitmap bmps = (Bitmap)pictureBox1.Image;
Bitmap bmpd = (Bitmap)pictureBox1.Image.Clone();
BitmapData bms = bmps.LockBits(new Rectangle(0, 0, bmps.Width, bmps.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData bmd = bmpd.LockBits(new Rectangle(0, 0, bmpd.Width, bmpd.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
byte* b1 = (byte*)(void*)bms.Scan0.ToPointer();
byte* b2 = (byte*)(void*)bmd.Scan0.ToPointer();
b2 += (2 * bmd.Width * 3 + 2 * 3);
float[] bm = { (float)0.0, (float)0.0, (float)0.0, (float)0.0 };
float[] gm = { (float)0.0, (float)0.0, (float)0.0, (float)0.0 };
float[] rm = { (float)0.0, (float)0.0, (float)0.0, (float)0.0 };
float s, yy;
float[] bx = { (float)0.0, (float)0.0, (float)0.0, (float)0.0 };
float[] gx = { (float)0.0, (float)0.0, (float)0.0, (float)0.0 };
float[] rx = { (float)0.0, (float)0.0, (float)0.0, (float)0.0 };
float[] mi = { (float)0.0, (float)0.0, (float)0.0, (float)0.0 };
float[] mv = { (float)0.0, (float)0.0, (float)0.0, (float)0.0 };
int x, y, ctr = 0, ctr1 = 0,i1,j1;
int r;
try
{
for (int i = 2; i <= pictureBox1.Height - 2; i++)
{
i1 = i;
for (int j = 2; j <= pictureBox1.Width - 2; j++)
{
j1 = j;
//region 1
//bb = gg = rr = 0;
x = i - 2;
y = j - 2;
while (true)
{
if (ctr == 3)
{
x = x + 1;
y = j - 2;
ctr = 0;
continue;
}
mi[0] += (float)((float)((float)b2[x * bmd.Stride + y] * 0.11) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.59) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.3));
bm[0] += (float)b2[x * bmd.Stride + y];
gm[0] += (float)b2[x * bmd.Stride + y + 1];
rm[0] += (float)b2[x * bmd.Stride + y + 2];
y = y + 3;
++ctr;
++ctr1;
if (ctr1 == 9)
break;
}
mi[0] /= 9;
bm[0] /= 9;
gm[0] /= 9;
rm[0] /= 9;
for (x = i - 2; x <= i; x++)
{
for (y = j - 2; y <= j; y++)
{
mv[0] += (float)Math.Pow((float)((float)((float)b2[x * bmd.Stride + y] * 0.11) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.59) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.3)) - mi[0], 2);
//gg += (float)Math.Pow(b2[x * bmd.Stride + y + 1], 2);
//rr += (float)Math.Pow(b2[x * bmd.Stride + y + 2], 2);
}
}
mv[0] /= 9;
//bx[0] = bb / 9;
//gx[0] = gg / 9;
//rx[0] = rr / 9;
//region 2
ctr = 0;
ctr1 = 0;
//bb = gg = rr = 0;
x = i - 2;
y = 3*j;
while (true)
{
if (ctr == 3)
{
x = x + 1;
y = j - 2;
ctr = 0;
continue;
}
mi[1] += (float)((float)((float)b2[x * bmd.Stride + y] * 0.11) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.59) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.3));
bm[1] += (float)b2[x * bmd.Stride + y];
gm[1] += (float)b2[x * bmd.Stride + y + 1];
rm[1] += (float)b2[x * bmd.Stride + y + 2];
y = y + 3;
++ctr;
++ctr1;
if (ctr1 == 9)
break;
}
mi[1] /= 9;
bm[1] /= 9;
gm[1] /= 9;
rm[1] /= 9;
for (x = i - 2; x <= i; x++)
{
for (y = j - 2; y <= j; y++)
{
mv[1] += (float)Math.Pow((float)((float)((float)b2[x * bmd.Stride + y] * 0.11) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.59) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.3)) - mi[0], 2);
//gg += (float)Math.Pow(b2[x * bmd.Stride + y + 1], 2);
//rr += (float)Math.Pow(b2[x * bmd.Stride + y + 2], 2);
}
}
mv[1] /= 9;
//bx[0] = bb / 9;
//gx[0] = gg / 9;
//rx[0] = rr / 9;
//region 3
ctr = 0;
ctr1 = 0;
//bb = gg = rr = 0;
x = i;
y = 3 * j;
while (true)
{
if (ctr == 3)
{
x = x + 1;
y = j - 2;
ctr = 0;
continue;
}
mi[2] += (float)((float)((float)b2[x * bmd.Stride + y] * 0.11) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.59) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.3));
bm[2] += (float)b2[x * bmd.Stride + y];
gm[2] += (float)b2[x * bmd.Stride + y + 1];
rm[2] += (float)b2[x * bmd.Stride + y + 2];
y = y + 3;
++ctr;
++ctr1;
if (ctr1 == 9)
break;
}
mi[2] /= 9;
bm[2] /= 9;
gm[2] /= 9;
rm[2] /= 9;
for (x = i - 2; x <= i; x++)
{
for (y = j - 2; y <= j; y++)
{
mv[2] += (float)Math.Pow((float)((float)((float)b2[x * bmd.Stride + y] * 0.11) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.59) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.3)) - mi[0], 2);
//gg += (float)Math.Pow(b2[x * bmd.Stride + y + 1], 2);
//rr += (float)Math.Pow(b2[x * bmd.Stride + y + 2], 2);
}
}
mv[2] /= 9;
//bx[0] = bb / 9;
//gx[0] = gg / 9;
//rx[0] = rr / 9;
//region 4
ctr = 0;
ctr1 = 0;
//bb = gg = rr = 0;
x = i;
y = j - 2;
while (true)
{
if (ctr == 3)
{
x = x + 1;
y = j - 2;
ctr = 0;
continue;
}
mi[3] += (float)((float)((float)b2[x * bmd.Stride + y] * 0.11) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.59) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.3));
bm[3] += (float)b2[x * bmd.Stride + y];
gm[3] += (float)b2[x * bmd.Stride + y + 1];
rm[3] += (float)b2[x * bmd.Stride + y + 2];
y = y + 3;
++ctr;
++ctr1;
if (ctr1 == 9)
break;
}
mi[3] /= 9;
bm[3] /= 9;
gm[3] /= 9;
rm[3] /= 9;
for (x = i - 2; x <= i; x++)
{
for (y = j - 2; y <= j; y++)
{
mv[3] += (float)Math.Pow((float)((float)((float)b2[x * bmd.Stride + y] * 0.11) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.59) + (float)((float)b2[x * bmd.Stride + y + 1] * 0.3)) - mi[0], 2);
//gg += (float)Math.Pow(b2[x * bmd.Stride + y + 1], 2);
//rr += (float)Math.Pow(b2[x * bmd.Stride + y + 2], 2);
}
}
mv[3] /= 9;
//bx[0] = bb / 9;
//gx[0] = gg / 9;
//rx[0] = rr / 9;
s = mv[0];
r = 1;
for (int h = 1; h <= 3; h++)
{
yy = mv[h];
if (yy < s)
{
s = yy;
r = h + 1;
}
}
textBox1.Text = r.ToString();
if (r == 1)
{
b2[0] = (byte)bm[0];
b2[1] = (byte)gm[0];
b2[2] = (byte)rm[0];
b2 += 3;
}
else if (r == 2)
{
b2[0] = (byte)bm[1];
b2[1] = (byte)gm[1];
b2[2] = (byte)rm[1];
b2 += 3;
}
else if (r == 3)
{
b2[0] = (byte)bm[2];
b2[1] = (byte)gm[2];
b2[2] = (byte)rm[2];
b2 += 3;
}
else if (r == 4)
{
b2[0] = (byte)bm[3];
b2[1] = (byte)gm[3];
b2[2] = (byte)rm[3];
b2 += 3;
}
//switch (r)
/*{
case 1:
b2[0] = (byte)bm[0];
b2[1] = (byte)gm[0];
b2[2] = (byte)rm[0];
b2 += 3;
break;
case 2:
b2[0] = (byte)bm[1];
b2[1] = (byte)gm[1];
b2[2] = (byte)rm[1];
b2 += 3;
break;
case 3:
b2[0] = (byte)bm[2];
b2[1] = (byte)gm[2];
b2[2] = (byte)rm[2];
b2 += 3;
break;
case 4:
b2[0] = (byte)bm[3];
b2[1] = (byte)gm[3];
b2[2] = (byte)rm[3];
b2 += 3;
break;
}*/
textBox2.Text = j.ToString();
j = j1;
textBox2.Text += j.ToString();
if (j <= pictureBox1.Width - 2)
continue;
}
i = i1;
if (i <= pictureBox1.Height - 2)
continue;
}
}
catch (Exception ex)
{
}
textBox1.Text += "HERE. Did not go into next iteration";
bmpd.Save(@"C:\users\pashok\kuwaop.jpg");
bmps.UnlockBits(bms);
bmpd.UnlockBits(bmd);
pictureBox2.Refresh();
pictureBox2.Image = (Image)(new Bitmap(@"C:\users\pashok\kuwaop.jpg"));
pictureBox2.Visible = true;
}
The problem I am facing is that this the for loop(i&j) run only once. Inspite of giving break points and verifying, I still am not able to figure out the error. Can someone help me. Its kinda urgent