【问题标题】:How can i get the max and min intensity value from an image?如何从图像中获取最大和最小强度值?
【发布时间】:2018-12-02 10:32:32
【问题描述】:

我正在尝试从名为btm 的图像中获取最大强度值和最小强度,以从“最大、最小”中获取平均值,然后使用该平均值作为阈值将图像转换为二值图像。 所以我使用了来自 aforge 库的直方图类,它接受一个 int 数组,所以我试图将我的图像 btm 转换为数组,但是我用来将图像返回数组从字节数据类型转换的函数 ImageToByteArray。

 System.Drawing.Image img = (System.Drawing.Image)btm;
 byte[] imgarr = ImageToByteArray(img);
 Histogram h = new Histogram(imgarr);
 int Maxval= h.max();
 int Minval= h.min();

.

    public static byte[] ImageToByteArray(System.Drawing.Image imageIn)
    {
        using (var ms = new MemoryStream())
        {
            imageIn.Save(ms, imageIn.RawFormat);
            return ms.ToArray();
        }
    }

【问题讨论】:

  • 您确实意识到 实际 平均值与从 max 和 min 获得的平均值不同,对吧? ({0, 99, 100} 的平均值是66,而不是50)。
  • 由于我们看不到您的图像是什么,一个简单的选择是将图像转换为灰度并将阈值设置为 (255-0)/2 = ~ 127。您可以将此值移动到两边有点找到你要找的东西。如果您能提及您需要二进制图像的应用程序,那就更好了
  • Aforge 的 Histogram 类不是处理图像的东西;它只处理一系列值。您将直方图应用于已保存文件中的字节,它与实际图像数据没有任何关系。另外,定义“强度”。如果你有一种颜色,它的“强度”是多少?
  • 请注意,RawFormat 仅表示“无论原始加载的文件保存在什么位置”。它并不意味着“代表原始图像数据的字节”。

标签: c# image-processing aforge


【解决方案1】:

我发布了两个例程。您可以检查它们以了解如何完成您的任务。

步骤 1. 将Bitmap 转换为int[,]:

    public static int[,] ToInteger(Bitmap input)
    {
        //// We are presuming that the image is grayscale.
        //// A color image is impossible to convert to 2D.
        int Width = input.Width;
        int Height = input.Height;

        int[,] array2d = new int[Width, Height];

        for (int y = 0; y < Height; y++)
        {
            for (int x = 0; x < Width; x++)
            {
                Color cl = input.GetPixel(x, y);

                // image is Grayscale
                // three elements are averaged.
                int gray = (int)Convert.ChangeType(cl.R * 0.3 + cl.G * 0.59 + cl.B * 0.11, typeof(int));

                array2d[x, y] = gray;
            }
        }

        return array2d;
    }

第 2 步。寻找最大值和最小值。

    public int Max(int[,] values)
    {
        int max = 0;

        for (int i = 1; i < values.GetLength(0); i++)
        {
            for (int j = 1; j < values.GetLength(1); j++)
            {
                if (values[i,j] > 0)
                {
                    max = values[i, j];
                }
            }
        }

        return max;
    }

    public int Min(int[,] values)
    {
           ... ... ...
                if (values[i,j] < 0)
                {
                    min = values[i];
                }
           ... ... ...

        return min;
    }

你可以结合最后两个。

希望你能明白。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多