请帮忙分析一下代码 [英] please help to analyse the code

查看:74
本文介绍了请帮忙分析一下代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

您是否知道逐行解释代码的工作量是多少?

每一行都需要一段解释!例如:

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();



创建一个名为next的新变量,它可以包含一个整数值。从先前声明的Random实例r,调用Next方法获取一个新的随机数,并将其分配给next变量。



可以你想象我们需要多长时间才能解释一个像你的例子一样的非常短的代码片段,一行一行?



不会发生这种情况。如果您有特定问题,请询问有关它的问题。但首先考虑一下 - 你想坐下45分钟然后输入逐行描述没有充分理由吗?



相反,看看你在哪里得到它了。可能有一个解释它的作用,或解释的链接。就目前而言,我们甚至不知道该代码是否符合它的意图!


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!


// debluring过滤器是如何工作的?

我没有想法。

//how debluring filter are working?
I have no idea.
BitmapData pbits = deblurImage.LockBits(
    new Rectangle(0, 0, width, height),
    ImageLockMode.ReadWrite,
    PixelFormat.Format24bppRgb);



//pixelformate.formate24bpprgb是为了什么?



它告诉 Bitmap.LockBits() [ ^ ]方法,每个像素使用多少位。根据手头的图像格式,可能需要使用的其他成员。 PixelFormat枚举 [ ^ ]。



//大步是什么?

BitmapData.Stride上的MSDN [ ^ ]



// Marshal用于什么?

MSDN编组 [ ^ ]



//如何填充图像中的颜色?

再次,我有不知道。

//以下声明的目的是什么?

//这些是如何工作的?


//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?

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);



Min()和Max()方法的组合确保r,g和b的值保持在定义的范围内(0..255)。

//什么是FormArgb?



Color.FromArgb [ ^ ]



//以下两个循环如何工作?意味着这些是如何调整像素值的?


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?

// 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()已将像素转换为一维字节数组。因此,嵌套循环将坐标(有点人类可读)转换为数组中的匹配位置。

请记住,每个像素可以有多个字节。这就是为什么在这种情况下有三个字节可以设置来改变一个像素的颜色。


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.


你的评论中有几个问题。我会看看我是否可以单独解决这些问题...



1。去毛刺滤镜如何工作?

你应该谷歌获取有关高斯滤波器及其工作原理的信息。实际上,这是一个图像处理问题,而不是软件工程问题。软件工程只是实现图像处理算法的数学。



2。在LockBits调用中, PixelFormat.Format24bppRgb 是什么?

简单(但没用)的答案是让它工作。

更有帮助 - 它控制.Net框架如何解释内存中的字节。图像可以以许多不同的方式存储 - 每像素1比特(仅黑色或白色),每像素4,824,32比特也是常见的。让像素的字节值表示红色,绿色和蓝色的百分比,或色调,饱和度和亮度,这很常见,但是将其作为RGB值表的索引也很常见。

您是否阅读了枚举的MSDN库文档?



3。什么是跨步?

Stride是关于代表图像的数据的内存布局。

您是否阅读过MSDN库文档? ?你知道答案就在那里。我前几天自己看了。



4。什么是Marshall呢?

.Net框架是一个托管内存系统 - 意味着你的代码不直接处理原始内存,而是通过抽象引用它你通常不需要知道的。编组内存将数据从.Net托管框架范围之外的内存移动到该范围中......然后再次退出。



5。它是如何填充图像中的颜色的?

Math.Min(...)语句的目的是什么?和

以下两个循环如何工作?


这些部分是代码的核心,它们正在完成过滤器的工作。

我认为你需要阅读一些关于图像处理的书籍(或网页),包括彩色像素如何用红色,绿色和蓝色光的比例表示。

一旦理解了这一点,就需要了解高斯滤波器 - 它们会获取有关一组相邻像素的信息,以决定相应像素的颜色和亮度。正如其他人所说的那样,逐行描述代码正在做什么需要太长时间......如果我们这样做,你就不会学到任何东西。

如果你有问题关于代码的特定部分(例如Math.Max / Min位)并且可以证明您花了一些时间来尝试理解代码,您很可能会得到一个特定的答案。



7。什么是FromARGB()?

请停止懒惰,并阅读文档。
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.


这篇关于请帮忙分析一下代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆