【问题标题】:Average value of array elements数组元素的平均值
【发布时间】: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


【解决方案1】:

在我做了一些修改后,它可以工作了。所以这是代码:

    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;

                }
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 2014-03-31
    相关资源
    最近更新 更多