【发布时间】:2013-12-29 18:55:10
【问题描述】:
我正在尝试计算一维数组的值,这是我的代码:
所以当我点击“检测”时,它应该通过我的图像开始一个阈值,从 i = 0 到图像高度,从 j = 0 到图像宽度:
public void detektieren_Click(object sender, RoutedEventArgs e)
{
for (i = 0; i < bitmap.Height; i++)
{
for (j = 0; j < bitmap.Width; j++)
{
stride = bitmap.PixelWidth * (bitmap.Format.BitsPerPixel / 8);
data = new byte[stride * bitmap.PixelHeight];
bitmap.CopyPixels(data, stride, 0);
index = i * stride + 4 * j;
现在访问 ARGB 数据:
byte A = data[index + 3];
byte R = data[index + 2];
byte G = data[index + 1];
byte B = data[index];
阈值后,如果有任意像素满足条件R=0 & G=0 & B=255:
if (Convert.ToInt32(R) == 0 && Convert.ToInt32(G) == 0 && Convert.ToInt32(B) == 255)
{
// Create a writer and open the file:
StreamWriter Messdaten;
if (!File.Exists("C:/Users/.../Messdaten.csv"))
{
Messdaten = new StreamWriter("C:/Users/.../Messdaten.csv");
}
else
{
Messdaten = File.AppendText("C:/Users/.../Messdaten.csv");
}
// Write to the file:
Messdaten.WriteLine(j + ";" + i);
// Close the stream:
Messdaten.Close();
for (y = 0; y < bitmap.Height; y++)
{
for (x = 0; x < bitmap.Width; x++)
{
double x_mw = 0; double y_mw = 0;
int[] x_array = new int[(int)bitmap.Width];
int[] y_array = new int[(int)bitmap.Height];
x_array[x] = j;
x_mw = x_array.Average();
y_array[y] = i;
y_mw = y_array.Average();
xy_coord.Content = (int) x_mw + ";" + (int) y_mw;
}
}
}
}
}
}
一切都在 CSV 文件中完美运行,我可以检测到一个像素(例如 R=0 G=0 B=255 的蓝色)。但我也想将每个像素的数据复制到数组中。但显然它并没有真正提供我想要的东西。它不计算蓝色像素总和的平均值(= 蓝色像素散点的质心),而只是显示 x_mw = 0 和 y_mw = 0。我做错了什么?
【问题讨论】:
-
在循环中重复执行此代码对我来说毫无意义。步幅 = bitmap.PixelWidth * (bitmap.Format.BitsPerPixel / 8);数据 = 新字节 [步幅 * bitmap.PixelHeight]; bitmap.CopyPixels(data, stride, 0);
-
在 x 上的内部循环中重复创建 x_array 和 y_array 也是没有意义的。
标签: c# wpf arrays image-processing bitmap