Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

I am trying to put sharpen filter over an image in my application after cropping the image. The function i am using is not giving expected output , no changes are reflected in the image,Please check the function where does i am wrong.

C#
public static WriteableBitmap sharpen(WriteableBitmap image)
        {
            WriteableBitmap sharpenImage = new WriteableBitmap(image); ;
            int filterWidth = 4;
            int filterHeight = 4;
            int w = image.PixelWidth;
            int h = image.PixelHeight;

            double[,] filter = new double[filterWidth, filterHeight];

            filter[0, 0] = filter[0, 1] = filter[0, 2] = filter[1, 0] = filter[1, 2] = filter[2, 0] = filter[2, 1] = filter[2, 2] = -1;
            filter[1, 1] = 9;

            double factor = 1.0;
            double bias = 0.0;

            Color[,] result = new Color[image.PixelWidth, image.PixelHeight];

            for (int x = 0; x < w; ++x)
            {
                for (int y = 0; y < h; ++y)
                {
                    double red = 0.0, green = 0.0, blue = 0.0,any=0.0;
                    Color imageColor = image.GetPixel(x, y);

                    for (int filterX = 0; filterX < filterWidth; filterX++)
                    {
                        for (int filterY = 0; filterY < filterHeight; filterY++)
                        {
                            int imageX = (x - filterWidth / 2 + filterX + w) % w;
                            int imageY = (y - filterHeight / 2 + filterY + h) % h;
                            any += imageColor.A * filter[filterX, filterY];
                            red += imageColor.R * filter[filterX, filterY];
                            green += imageColor.G * filter[filterX, filterY];
                            blue += imageColor.B * filter[filterX, filterY];
                        }
                        byte a = Convert.ToByte( Math.Min(Math.Max((int)(factor * any + bias), 0), 255));
                        byte r = Convert.ToByte(Math.Min(Math.Max((int)(factor * red + bias), 0), 255));
                        byte g = Convert.ToByte(Math.Min(Math.Max((int)(factor * green + bias), 0), 255));
                        byte b = Convert.ToByte(Math.Min(Math.Max((int)(factor * blue + bias), 0), 255));

                        result[x, y] = Color.FromArgb(a,r, g, b);
                    }
                }
            }
            for (int i = 0; i < w; ++i)
            {
                for (int j = 0; j < h; ++j)
                {
                    sharpenImage.SetPixel(i, j, result[i, j]);
                }
            }
            return sharpenImage;
        }
Posted
Comments
Sergey Alexandrovich Kryukov 14-Mar-13 18:17pm    
Why checking it? This is not a question.
Please, no one likes to waste time and do monkey job.
—SA
Ariana Bond 14-Mar-13 18:27pm    
Ok then, i think you would like to help ?
Sergey Alexandrovich Kryukov 14-Mar-13 18:35pm    
Certainly. Can you see my solution?
—SA
Sergey Alexandrovich Kryukov 14-Mar-13 18:38pm    
Please, next time, formulate a distinct question.
Please understand: we are overwhelmed with non-question, so your post, even though it does make sense, can be accidentally removed.
—SA

1 solution

Before looking at the code, I would advise you through out this code, because you are doing it all wrong, from the very beginning.

You are not really using writeable capabilities of WriteableBitmap as you don't write anything, only reading. The only writing part is SetPixel which defeats the purpose of this class completely. Performance of SetPixel is always prohibitively bad (unless you want to write very few pixels).

Look at the code sample of the class documentation: http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap.aspx[^].

How could you possibly miss the main idea of this class? You call Lock, directly manipulate memory, and then call Unlock. This is the main idea. That's why it give good performance. (It it similar to System.Windows.Forms.Bitmap.LockBits.)

Now, I cannot see how you algorithm can sharpen anything. I don't think it can. I know at least two methods of sharpening: one is unsharp mask, another one simply perform the image convolution with a fixed core (matrix) and is very simple and fast. Are you familiar with convolution operations? In discrete objects, they are pretty easy.

[EDIT #1]

You can find good example of convolutions of the image with a 5x5 convolution matrix demonstrating sharpening, blur, edge enhance, edge detect and emboss here:
http://docs.gimp.org/en/plug-in-convmatrix.html[^].

Isn't that amazing? Just one operation for such different and striking effects! You can easily implement it using WriteableBitmap, can you?

[EDIT #2]

I found an open-source library which should do it for Silverlight, WPF and more: http://writeablebitmapex.codeplex.com/[^].

You can try to find more: http://bit.ly/WKz4pj[^].

—SA
 
Share this answer
 
v5
Comments
Ariana Bond 14-Mar-13 18:37pm    
I know convolution as a mathematical operations between two functions or set of integers which produce third function, that is it for convolution , in terms of C# or Silverlight i know nothing. So can you please tell me the easiest possible way?
Sergey Alexandrovich Kryukov 14-Mar-13 18:43pm    
This is pretty much the same, in 2D functions modeling discrete objects (in this case, sets of pixels making an image). The article I referenced above basically explains it, but you can find it in many other places.

This is one of the most basic techniques of image processing, presents in all more or less advanced libraries. The one I remember is AForge.NET, but it was initially oriented to System.Drawing. Still, it can be used...

—SA
Ariana Bond 14-Mar-13 18:46pm    
Thanks for your help...
Sergey Alexandrovich Kryukov 14-Mar-13 18:48pm    
Wait a minute... Please see new update to my answer, about ready-to-use libraries, please see after [EDIT #2].
—SA
Sergey Alexandrovich Kryukov 14-Mar-13 18:49pm    
You are welcome.
Good luck, call again (please ask questions, formally, to be heard — I explained the problem of non-questions).
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900