数组元素的平均值

时间:2013-12-11 16:59:16

标签: c# wpf arrays image-processing bitmap

我正在尝试计算单维数组的值,这是我的代码:

所以当我点击“检测”时,它应该从我的图像开始一个阈值,从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&amp; G = 0&amp; 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)。但我还想将每个Pixel的数据复制到Array中。但显然它并没有真正实现我想要的东西。它不计算蓝色像素之和的平均值(=蓝色像素散射的质心),而只是显示x_mw = 0和y_mw = 0.我做错了什么?

1 个答案:

答案 0 :(得分:0)

我做了一些修改后就可以了。所以这是代码:

    public void detektieren_Click(object sender, RoutedEventArgs e)
    {

        int x_sum = 0; int y_sum = 0; int x_count = 0; int y_count = 0; int x_mw; int y_mw;
        int[] x_array = new int[(int)bitmap.Width];
        int[] y_array = new int[(int)bitmap.Height];
        int[] x_array_copy = new int[(int)bitmap.Width];
        int[] y_array_copy = new int[(int)bitmap.Height];

        stride = bitmap.PixelWidth * (bitmap.Format.BitsPerPixel / 8);
        data = new byte[stride * bitmap.PixelHeight];
        bitmap.CopyPixels(data, stride, 0);

        for (i = 0; i < (int) bitmap.Height; i++)
        {
            for (j = 0; j < (int) bitmap.Width; j++)
            {
                index = i * stride + 4 * j;

                byte A = data[index + 3];
                byte R = data[index + 2];
                byte G = data[index + 1];
                byte B = data[index];

                if (Convert.ToInt32(R) == 0 && Convert.ToInt32(G) == 0 && Convert.ToInt32(B) == 255)
                {
                    x_array[j] = j;
                    x_count++;
                    x_array_copy[j] = x_array_copy[j] + j;
                    x_sum = (int) x_array_copy.Sum();
                    x_mw = x_sum / x_count;

                    y_array[i] = i;
                    y_count++;
                    y_array_copy[i] = y_array_copy[i] + i;
                    y_sum = (int) y_array_copy.Sum();
                    y_mw = y_sum / y_count;

                    xy_coord.Content = x_mw + ";" + y_mw;

                }
            }
        }
    }