如何从图像中获取最大和最小强度值?

问题描述 投票:0回答:1

我试图从一个名为btm的图像中获取最大强度值和最小强度,以获得“max,min”的平均值,然后使用此平均值作为阈值将图像转换为二进制图像。所以我使用了来自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();
        }
    }
c# image-processing aforge
1个回答
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;
    }

你可以结合最后两个。

希望你明白这个主意。

© www.soinside.com 2019 - 2024. All rights reserved.