0

I am trying to get the max intensity value and min intensity from an image called btm to get the average from the "max, min", then use this average as a threshold to convert image to binary image. So I used histogram class from aforge library which takes an int array, so I am trying to convert my image btm to the array but the function ImageToByteArray that I used to convert image return array from byte data type.

 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();
        }
    }
Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
Nemo
  • 203
  • 3
  • 12
  • You do realize that the *actual* average is not the same as the average obtained from max and min, right? (the average of `{0, 99, 100}` is `66`, not `50`). – Rufus L Jun 22 '18 at 23:42
  • Since we cannot see what your image is, a simple option would be to convert the image to grayscale and set the threshold to (255-0)/2 = ~ 127. You can move this value on either side a bit to find what you're looking for. If you could mention what application you need the binary image for, it would be better – Vaibhav Mehrotra Jun 23 '18 at 05:29
  • Aforge's Histogram class is not something that processes images; it just processes a range of values. You're applying your histogram on _bytes in a saved file_, which have _no_ relation whatsoever to the actual image data. Also, define "intensity". If you had a colour, what would be its "intensity"? – Nyerguds Jun 23 '18 at 17:16
  • Note that `RawFormat` just means "whatever the original loaded file was saved in". It does not mean "bytes representing the raw image data". – Nyerguds Jun 23 '18 at 17:22

1 Answers1

2

I am posting two routines. You can examine them to get an idea regarding how to achieve your task.

Step 1. Convert Bitmap to 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;
    }

Step 2. Seach for Max and Min.

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

You can combine last two.

Hope you get the idea.

user366312
  • 16,949
  • 65
  • 235
  • 452