【发布时间】:2015-11-13 23:32:21
【问题描述】:
真的,我正在尝试通过 C# 应用 3X3 中值过滤,根据我对中值过滤概念的理解,我编写了以下代码,但是当我运行它时,表单挂起。我认为在最后一个嵌套的 for 循环中有一些问题,但我不知道应用中位数概念的错误或错误在哪里!
public static Bitmap MedianFiltering(Bitmap bm)
{
List<int> termsList = new List<int>();
Bitmap res, temp;
Color c;
int counter = 0;
//Convert to Grayscale
for (int i = 0; i < bm.Width; i++)
{
for (int j = 0; j < bm.Height; j++)
{
c = bm.GetPixel(i, j);
byte gray = (byte)(.333 * c.R + .333 * c.G + .333 * c.B);
bm.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
}
}
temp = bm;
//applying Median Filtering
for (int i = 0; i <= temp.Width - 3; i++)
for (int j = 0; j <= temp.Height - 3; j++)
{
for (int x = i; x <= i + 2; x++)
for (int y = j; y <= j + 2; y++)
{
c = temp.GetPixel(x, y);
termsList.Add(c.R);
counter++;
}
int[] terms = termsList.ToArray();
Array.Sort<int>(terms);
Array.Reverse(terms);
int color = terms[4];
temp.SetPixel(i + 1, j + 1, Color.FromArgb(color, color, color));
counter = 0;
}
res = temp;
return res;
}
谢谢。
【问题讨论】:
-
如果您在 UI 线程上运行此代码(例如,从按钮处理程序),UI 挂起是正常的。但它应该只挂起,直到中值滤波器计算完成。您可以使用异步方法使表单在计算过程中不挂起。
-
我现在试过了,在这一行! temp.SetPixel(i + 1, j + 1, Color.FromArgb(color, color, color));
-
所以它完全挂了?如果您等待足够的时间,它会完成吗?
-
我等了 5 分钟以上,结果相同
-
5分钟后有进展吗?将
Debug.WriteLine(i + ", " + j);放在temp.SetPixel(i + 1, j + 1,....行之后。这会将当前像素坐标输出到输出窗口(在 Visual Studio 中查看 -> 输出)。这样,您可以查看程序是否仍在运行。
标签: c# .net image-processing