【问题标题】:Which indices of a 3 dimensional array hold the largest value?3 维数组的哪些索引具有最大值?
【发布时间】:2020-11-15 20:55:51
【问题描述】:

我有一个整数的三维数组,每个维度代表一种颜色R、G或B,整数代表R、G、B的颜色值在图像中出现的次数。

现在我需要找出这张图片中出现次数最多的 X 种颜色。

到目前为止,我已经创建了一个结构列表,该结构同时包含颜色和计数。我遍历原始数组的三个维度,对于每个大于列表中第一项的值,我替换列表中的项,然后对列表进行排序。

这当然需要很长时间,每次运行将近 2 秒。

如何以省时的方式找到此数组中出现次数最多的 X 颜色?目前在我的计算机上每次运行要花费我 2 秒,但这段代码需要在 Raspberry Pi 上运行,这需要更长的时间。理想情况下,我在 Pi 上寻找 100 毫秒的时间,但我不知道如何到达那里或使用什么算法。

编辑:根据 Jerry 的要求添加代码

// Struct holding colours & colour count
struct ColourCount
{
    public byte red;
    public byte green;
    public byte blue;
    public Int32 count;
}

List<Int32> matrix = new List<Int32>();
ColourCount[] count = new ColourCount[100];
int idx = 0;
for (int r = 0; r < 256; r++)
{
    for (int g = 0; g < 256; g++)
    {
        for (int b = 0; b < 256; b++)
        {
            if (idx < count.Length)
            {
                count[idx].red = (byte)r;
                count[idx].green = (byte)g;
                count[idx].blue = (byte)b;
                count[idx].count = colours[r, g, b];
                idx++;
            }
            else
            {
                Array.Sort<ColourCount>(count, (x, y) => (x.count.CompareTo(y.count)));
                if (colours[r, g, b] > count[0].count)
                {
                    count[0].red = (byte)r;
                    count[0].green = (byte)g;
                    count[0].blue = (byte)b;
                    count[0].count = colours[r, g, b];
                }
            }
        }
    }
}

for (int i = 0; i < count.Length; i++)
{
    matrix.Add((count[i].red * 1000 * 1000) + (count[i].green * 1000) + count[i].blue);
}

【问题讨论】:

  • 你应该展示你的代码而不是试图描述它。
  • 由于这段特定的代码已经运行了很长时间,我想可以肯定地假设不可能将它改进到它的性能点,所以我把它省略了,希望一个新的见解。我不认为代码比描述的更多,但我还是添加了它。
  • 是那种会害死你的人。一种方法是使用字典来保存颜色,在找到它们时添加或增加它们。然后在最后排序一次以获得直方图或你在做什么
  • 如果在设计时所有颜色都是已知的,那么只需使用一个数组,并对它们进行索引。说我不完全理解你在这里想要达到的目标,但如果你删除了排序,你可能会发现这会因因素而加快
  • @将军 我打算用字典试试,看看性能够不够好。我不太明白您的第二条评论 - 颜色已经是所有颜色的索引(以 R、G 和 B 为索引),其中包含颜色计数。问题是我如何过滤掉 X 最高计数。有没有办法对三维数组进行排序(如果这有意义的话)?

标签: c# arrays list colors


【解决方案1】:

移动你的排序。如果您更改了最小值,则只需对数组进行排序。

平均时间:48 毫秒,i7-6700k

ColourCount[] count = new ColourCount[100];
int idx = 0;
for (int r = 0; r < 256; r++)
{
    for (int g = 0; g < 256; g++)
    {
        for (int b = 0; b < 256; b++)
        {
            if (idx < count.Length)
            {
                count[idx].red = (byte)r;
                count[idx].green = (byte)g;
                count[idx].blue = (byte)b;
                count[idx].count = colours[r, g, b];
                idx++;
            }
            else
            {
                if (colours[r, g, b] > count[0].count)
                {
                    count[0].red = (byte)r;
                    count[0].green = (byte)g;
                    count[0].blue = (byte)b;
                    count[0].count = colours[r, g, b];
                    Array.Sort(count, (x, y) => (x.count.CompareTo(y.count)));
                }
            }
        }
    }
}

【讨论】:

  • Jerry - 你的代码和我的不一样。考虑 count[0] 在 idx = count.Length 处保持最大值,但下一个 colours[r,g,b] 仍高于此计数,则将替换计数中的最大值,而不是最小值。我在我的开发机器上对此进行了测试,它平均运行了 218 毫秒。我的机器可能很旧,它仍然比 Raspberry Pi 快很多,所以我看不到它如何在 Pi 上运行低于 100 毫秒。这是一个开始,但我担心离我寻求的解决方案还很远。不过感谢您的信息!
猜你喜欢
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 2023-01-12
  • 1970-01-01
  • 2019-08-12
  • 1970-01-01
  • 2016-02-24
相关资源
最近更新 更多