[英]Value was either too large or too small for an Int32 when processing 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));
}
問題是我在處理圖像時遇到以下錯誤
對於Int32,值太大或太小。
上
int A = lM + Convert.ToInt32(tm2);
知道問題是什么嗎?
編輯:做了一些調試
lM保持0且lVAR保持0
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));
}
或類似的東西......
有符號整數只能存儲-2,147,483,648和2,147,483,647之間的值
如果你不需要負值,你可以使用unsigned
整數獲得超過40億的數字。
如果這還不夠,你可以使用long
類型,它可以存儲更大的數字。
此外, NaN
表示“非數字”,您應該使用調試器來查看該數字是如何計算的,確保您沒有除以零或與其他NAN進行算術運算或做其他不做的事情數學意義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.