【发布时间】:2012-08-05 22:53:11
【问题描述】:
下面是我尝试应用图像过滤器清除图像的代码的一部分
//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的值是多少?
-
嗯...
IM + Convert.ToInt32(tm2)超出 Int32 的范围?我认为这个例外是不言自明的。在调试模式下运行您的代码,看看当时tm2和IM中的值究竟是什么,这样您就可以追溯它们超出范围的原因。 -
@hometoast tm2 = NaN 和 tmp = 23..
标签: c# .net image-processing