Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
public static Bitmap Deblur(Bitmap image)
        {
            
            Bitmap deblurImage = (Bitmap)image.Clone();

            int filterWidth = 3;
            int filterHeight = 3;
            int width = image.Width;
            int height = image.Height;

            // Create deburing filter.
//how these filter are working?
            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.Width, image.Height];

            // Lock image bits for read/write.
//pixelformate.formate24bpprgb is what for?
            BitmapData pbits = deblurImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            // Declare an array to hold the bytes of the bitmap.
//what is stride?
            int bytes = pbits.Stride * height;
            byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array.
//what is Marshal used for?
            System.Runtime.InteropServices.Marshal.Copy(pbits.Scan0, rgbValues, 0, bytes);

            int rgb;
//how it is filling the color in image?
            // Fill the color array with the new deblured color values.
            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    double red = 0.0, green = 0.0, blue = 0.0;

                    for (int filterX = 0; filterX < filterWidth; filterX++)
                    {
                        for (int filterY = 0; filterY < filterHeight; filterY++)
                        {
                            int imageX = (x - filterWidth / 2 + filterX + width) % width;
                            int imageY = (y - filterHeight / 2 + filterY + height) % height;

                            rgb = imageY * pbits.Stride + 3 * imageX;

                            red += rgbValues[rgb + 2] * filter[filterX, filterY];
                            green += rgbValues[rgb + 1] * filter[filterX, filterY];
                            blue += rgbValues[rgb + 0] * filter[filterX, filterY];
                        }
//what is the purpose of the following statments?
//how these are working?
                        int r = Math.Min(Math.Max((int)(factor * red + bias), 0), 255);
                        int g = Math.Min(Math.Max((int)(factor * green + bias), 0), 255);
                        int b = Math.Min(Math.Max((int)(factor * blue + bias), 0), 255);
//what is FormArgb?
                        result[x, y] = Color.FromArgb(r, g, b);
                    }
                }
            }
//how the following two loops are working?mean how these are adjusting the values of the pixels
            // Update the image with the deblured pixels.
            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    rgb = y * pbits.Stride + 3 * x;

                    rgbValues[rgb + 2] = result[x, y].R;
                    rgbValues[rgb + 1] = result[x, y].G;
                    rgbValues[rgb + 0] = result[x, y].B;
                }
            }

            // Copy the RGB values back to the bitmap.
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, pbits.Scan0, bytes);
            // Release image bits.
            deblurImage.UnlockBits(pbits);

            return deblurImage;
        }
Posted
Updated 20-Jun-13 21:49pm
v3
Comments
Rajesh Anuhya 21-Jun-13 3:34am    
Where/what to analyse??
Naeem Ahmed 21-Jun-13 3:52am    
Now I have commented in the above code please tell me how each and every statement is working specially the filters...

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?

Instead, look at where you got it. There is probably an explanation of what it does, or a link to an explanation. As it stands, we don't even know if that code does what it purports to do!
 
Share this answer
 
Comments
Naeem Ahmed 21-Jun-13 3:58am    
Now here I can not understand the following code. And how it is being used in the code?

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;
OriginalGriff 21-Jun-13 4:10am    
So try it in the debugger. It's pretty obvious code: all it is doing is setting a bunch of values to presets:
-1 -1 -1
-1 9 -1
-1 -1 -1

If you don't understand something like that, execute it in the debugger, and see what it does!
//how debluring filter are working?
I have no idea.
C#
BitmapData pbits = deblurImage.LockBits(
    new Rectangle(0, 0, width, height),
    ImageLockMode.ReadWrite,
    PixelFormat.Format24bppRgb);

//pixelformate.formate24bpprgb is what for?

It tells the Bitmap.LockBits()[^] method, how many bits are used for each pixel. Depending on the image format at hand, it could be necessary to use another member of the PixelFormat Enumeration[^].

//what is stride?
MSDN on BitmapData.Stride[^]

//what is Marshal used for?
MSDN on Marshalling[^]

//how it is filling the color in image?
Again, I have no idea.
//what is the purpose of the following statments?
//how these are working?
C#
int r = Math.Min(Math.Max((int)(factor * red + bias), 0), 255);
int g = Math.Min(Math.Max((int)(factor * green + bias), 0), 255);
int b = Math.Min(Math.Max((int)(factor * blue + bias), 0), 255);

The combination of Min() and Max() methods ensure that values for r, g and b stay in defined range (0..255).
//what is FormArgb?

Color.FromArgb[^]

//how the following two loops are working?mean how these are adjusting the values of the pixels?
C#
// Update the image with the deblured pixels.
for (int x = 0; x < width; ++x)
{
    for (int y = 0; y < height; ++y)
    {
        rgb = y * pbits.Stride + 3 * x;
 
        rgbValues[rgb + 2] = result[x, y].R;
        rgbValues[rgb + 1] = result[x, y].G;
        rgbValues[rgb + 0] = result[x, y].B;
    }
}

LockBits() has transformed the pixels into a one-dimensional array of bytes. The nested loop accordingly transformes co-ordinates (somewhat human-readable) into the matching position within the array.
Remember that there can be more than one byte for every pixel. That's why there are three bytes to set for changing the colour of one pixel in this case.
 
Share this answer
 
You have several questions in your comments. I'll see if I can address them individually...

1. How does a deburring filter work?
You should google for information about gaussian filters and how they work. That, really, is an image processing question, not a software engineering question. The software engineering simply implements the maths of the image processing algorithm.

2. What is PixelFormat.Format24bppRgb for in the LockBits call?
The simple (but useless) answer is "to make it work".
A little more helpfully - it controls how the .Net framework interprets the bytes in memory. Images can be stored in many different ways - 1 bit per pixel (black or white, only), 4, 8 24, 32 bits per pixel are also common. Having the byte value of the pixel represent percentages of red, green and blue, or hue, saturation and brightness, is common, but having it be an index into a table of RGB values is also common.
Have you read the MSDN library documentation for the enum?

3. What is stride?
Stride is all about the memory layout of the data that represents the image.
Have you read the MSDN library documentation? The answer is there, you know. I read it myself just the other day.

4. What is Marshall all about?
The .Net framework is a managed memory system - meaning that your code doesn't directly deal with raw memory, but references it via abstractions that you normally do not need to know about. Marshalling memory moves data from memory outside the scope of the .Net managed framework into that scope ... and back out again.

5. How is it filling in the colour in the image?
What is the purpose of the Math.Min(...) statements? and
How are the following two loops working?

These sections are the heart of the code, they are doing the work of the filter.
I think you need to read some books (or web pages) about image processing, including how coloured pixels are represented by proportionate amounts of red, green and blue light.
Once you understand that, you need to read about gaussian filters - which are taking information about a group of neighbouring pixels to decide the colour and brightness of a corresponding pixel. As others have said, it would take far too long to describe line by line what the code is doing .. and if we did that you wouldn't learn anything, anyway.
If you have a question about a specific part of the code (for example the Math.Max/Min bit) and can demonstrate that you've spent some time trying to understand the code, you are quite likely to get a specific answer.

7. What is FromARGB()?
Please stop being lazy, and read the documentation.
 
Share this answer
 

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