简体   繁体   English

处理图像时,Int32的值太大或太小

[英]Value was either too large or too small for an Int32 when processing image

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. 对于Int32,值太大或太小。

On

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

Any idea what the problem is? 知道问题是什么吗?

edit: did some debugging 编辑:做了一些调试

lM hold 0 and lVAR hold 0 lM保持0且lVAR保持0

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... tm2可能是一个大于最大Int32值或低于最小Int32值的值...我会添加一个检查,类似于......

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... 或类似的东西......

a signed integer can only store values between −2,147,483,648 and 2,147,483,647 有符号整数只能存储-2,147,483,648和2,147,483,647之间的值

if you don't need negative values, than you can get numbers over 4 billion with an unsigned integer. 如果你不需要负值,你可以使用unsigned整数获得超过40亿的数字。

If that's not enough you can use the long type, which can store much much larger numbers. 如果这还不够,你可以使用long类型,它可以存储更大的数字。

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. 此外, NaN表示“非数字”,您应该使用调试器来查看该数字是如何计算的,确保您没有除以零或与其他NAN进行算术运算或做其他不做的事情数学意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM