I am trying to implement the Kuwahara Filter in C#. I think my code is right, but I am not getting an exact output. Can someone help me with where I am going wrong?

unsafe
            {
                try
            {
            Bitmap b1 = (Bitmap)pictureBox1.Image.Clone();
            Bitmap b2 = new Bitmap(b1.Width, b1.Height);
            BitmapData bd1 = b1.LockBits(new Rectangle(0, 0, b1.Width, b1.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            BitmapData bd2 = b2.LockBits(new Rectangle(0, 0, b2.Width, b2.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            byte* p1 = (byte*)(void*)bd1.Scan0.ToPointer();
            byte* p2 = (byte*)(void*)bd2.Scan0.ToPointer();
            //Graphics g = Graphics.FromImage(b2);
            //g.DrawImage(b1, new Rectangle(0, 0, b1.Width, b1.Height), new Rectangle(0, 0, b1.Width, b1.Height), GraphicsUnit.Pixel);
            //g.Dispose();
            int[] aminx = { -(b1.Width * b1.Height) / 2, 0, -(b1.Width * b1.Height) / 2, 0 };
            int[] amaxx = { 0, (b1.Width * b1.Height) / 2, 0, (b1.Width * b1.Height) / 2 };
            int[] aminy = { -(b1.Width * b1.Height) / 2, -(b1.Width * b1.Height) / 2, 0, 0 };
            int[] amaxy = { 0, 0, (b1.Width * b1.Height) / 2, (b1.Width * b1.Height) / 2 };
            for (int x = 0; x < b2.Width; x++)
            {
                for (int y = 0; y < b2.Height; y++)
                {
                    int[] RValues = { 0, 0, 0, 0 };
                    int[] GValues = { 0, 0, 0, 0 };
                    int[] BValues = { 0, 0, 0, 0 };
                    int[] NumPixels = { 0, 0, 0, 0 };
                    int[] MaxRValue = { 0, 0, 0, 0 };
                    int[] MaxGValue = { 0, 0, 0, 0 };
                    int[] MaxBValue = { 0, 0, 0, 0 };
                    int[] MinRValue = { 255, 255, 255, 255 };
                    int[] MinGValue = { 255, 255, 255, 255 };
                    int[] MinBValue = { 255, 255, 255, 255 };
                    for (int i = 0; i < 4; ++i)
                    {
                        for (int x2 = aminx[i]; x2 < amaxx[i]; ++x2)
                        {
                            int TempX = x + x2;
                            if (TempX >= 0 && TempX < b2.Width)
                            {
                                for (int y2 = aminy[i]; y2 < amaxy[i]; ++y2)
                                {
                                    int TempY = y + y2;
                                    if (TempY >= 0 && TempY < b2.Height)
                                    {
                                        //Color TempColor = b1.GetPixel(TempX, TempY);
                                        RValues[i] += p1[2];// TempColor.R;
                                        GValues[i] += p1[1];// TempColor.G;
                                        BValues[i] += p1[0];// TempColor.B;
                                        if (p1[2] > MaxRValue[i])
                                        {
                                            MaxRValue[i] = p1[2];// TempColor.R;
                                        }
                                        else if (p1[2] < MinRValue[i])
                                        {
                                            MinRValue[i] = p1[2];// TempColor.R;
                                        }

                                        if (p1[1] > MaxGValue[i])
                                        {
                                            MaxGValue[i] = p1[1];// TempColor.G;
                                        }
                                        else if (p1[1] < MinGValue[i])
                                        {
                                            MinGValue[i] = p1[1];// TempColor.G;
                                        }

                                        if (p1[0] > MaxBValue[i])
                                        {
                                            MaxBValue[i] = p1[0];// TempColor.B;
                                        }
                                        else if (p1[0] < MinBValue[i])
                                        {
                                            MinBValue[i] = p1[0];// TempColor.B;
                                        }
                                        ++NumPixels[i];
                                        
                                    }
                                    p1 += 3;
                                }
                            }
                        }
                    }
                    int j = 0;
                    int mind = 10000;
                    for (int i = 0; i < 4; i++)
                    {
                        int cd = MaxRValue[i] - MinRValue[i] + MaxGValue[i] - MinGValue[i] + MaxBValue[i] - MinBValue[i];
                        if (cd < mind && NumPixels[i] > 0)
                        {
                            j = i;
                            mind = cd;
                        }
                    }
                    
                    p2[0] = (byte)(BValues[j] / NumPixels[j]);
                    p2[1] = (byte)(GValues[j] / NumPixels[j]);
                    p2[2] = (byte)(RValues[j] / NumPixels[j]);
                    //Color meanpixel = Color.FromArgb(RValues[j] / NumPixels[j], GValues[j] / NumPixels[j], BValues[j] / NumPixels[j]);
                    //b2.SetPixel(x, y, meanpixel);

                }
            }

            b2.Save(@"C:\users\pashok\tt.jpg");
            int boo = 0;
            b2.Save(@"C:\users\pashok\kuw.jpg");
            b1.UnlockBits(bd1);
            b2.UnlockBits(bd2);
            textBox1.Text = "at the end";
            //pictureBox2.Image = (Image)b2;
                }        catch(Exception ex)
            {
                }
        }

Recommended Answers

All 2 Replies

is this related to the image distorted thread you had? Have you had a chance to look at my response to that? I managed to get the code you had working, it was an issue with the padding in bitmapdata arrays.
If this is unrelated, then can you be more specific about why the output isnt as expected :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.