1

Below is a part of my code where I am trying to apply an image filer to clear an image

       //Convert the image to integer array
        uint[,] image = new uint[use.Width, use.Height];
        for (i = 0; i < use.Width; i++)
        {
            for (j = 0; j < use.Height; j++)
            {
                image[i, j] = Convert.ToUInt32(use.GetPixel(i, j).R);
            }
        }

        int Block = 5;
        int BlockSqr = 25;

        // Internal Block Processing
        for (i = Block; i < use.Width- Block; i = i + 1)
            for (j = Block; j < use.Height- Block; j = j + 1)
            {
                int lM, lVAR;

                // Calculating the Mean of the Block
                tmp = 0;
                for (int k = i - Block; k < i + Block; k = k + 1)
                {
                    for (int l = j - Block; l < j + Block; l = l + 1)
                    {
                        tmp = tmp + image[k, l];
                    }
                }
                lM = Convert.ToInt32(Math.Abs(tmp / BlockSqr));

                // Calculating the Variance of the Block
                tmp = 0;
                M1 = 0;
                for (int k = i - Block; k < Block + i; k = k + 1)
                {
                    for (int l = j - Block; l < Block + j; l = l + 1)
                    {
                        M1 = ((image[k, l] - Convert.ToUInt32(lM)) * (image[k, l] - Convert.ToUInt32(lM)));
                        tmp = tmp + M1;
                    }
                }
                lVAR = Convert.ToInt32(Math.Abs(tmp / BlockSqr));

                //Putting the filtered value

                float tm = (lVAR - mVAR);
                float tm1 = tm / lVAR;
                int mm = Convert.ToInt32(image[i, j] - lM);
                float tm2 = tm1 * (image[i, j] - lM);
                int A = lM + Convert.ToInt32(tm2);

                if (A < 255)
                    Wiener.SetPixel(i, j, Color.FromArgb(255, A, A, A));

            }

The problem is that I get the below error when i process the image

Value was either too large or too small for an Int32.

On

int A = lM + Convert.ToInt32(tm2);

Any idea what the problem is?

edit: did some debugging

lM hold 0 and lVAR hold 0

LiveEn
  • 3,193
  • 12
  • 59
  • 104
  • 1
    Well, when it fails, what is the value of tm2? – hometoast Aug 08 '12 at 18:50
  • Well... `IM + Convert.ToInt32(tm2)` is out of range for an Int32? I think the exception is rather self-explanatory. Run your code in debug mode to see what exactly the values are in `tm2` and `IM` at the time so you can trace back why they became out of range. – mellamokb Aug 08 '12 at 18:50
  • @hometoast tm2 = NaN and tmp = 23.. – LiveEn Aug 08 '12 at 18:54
  • possible duplicate of [Value was either too large or too small for an Int32](http://stackoverflow.com/questions/6340570/value-was-either-too-large-or-too-small-for-an-int32) – HatSoft Aug 08 '12 at 18:54

2 Answers2

2

It's likely that tm2 is a value that is larger than the max Int32 value or lower than the min Int32 value...I would add a check, something like...

double AD = lM + Convert.ToInt32(tm2);
if((AD >= 0) && (AD < 255)) {
  int A = Convert.ToInt32(AD);
  Wiener.SetPixel(i, j, Color.FromArgb(255, A, A, A));
}

or something similar...

jceddy
  • 1,667
  • 2
  • 12
  • 18
1

a signed integer can only store values between −2,147,483,648 and 2,147,483,647

if you don't need negative values, than you can get numbers over 4 billion with an unsigned integer.

If that's not enough you can use the long type, which can store much much larger numbers.

Also, NaN means "Not a Number", and you should probably use your debugger to see how that number is getting calculated, make sure you're not dividing by zero or doing arithmetic with other NANs or doing other things that don't make mathematical sense.