集合中不同的子集

发布于 2024-11-13 11:31:40 字数 842 浏览 4 评论 0原文

我编写了一个扩展方法,它从位图中返回 YUV 值的二维数组,即:

public static YUV[,] ToYuvLattice(this System.Drawing.Bitmap bm)
{
    var lattice = new YUV[bm.Width, bm.Height];
    for(var ix = 0; ix < bm.Width; ix++)
    {
        for(var iy = 0; iy < bm.Height; iy++)
        {
            lattice[ix, iy] = bm.GetPixel(ix, iy).ToYUV();
        }
    }
    return lattice;
}

然后我需要提取具有相同 U 和 V 分量的集合。即Set1包含所有[item1;item2]对,Set2包含[_item1;_item2]对。所以我想获得列表的列表。

public IEnumerable<List<Cell<YUV>>> ExtractClusters()
{            
    foreach(var cell in this.lattice)
    {
        if(cell.Feature.U != 0 || cell.Feature.V != 0)
        {
            // other condition to be defined
        }
        // null yet
        yield return null;
    }
} 

我从上面的代码开始,但我坚持使用不同值的条件。

I wrote an extension method which returns me 2-dimensional array of YUV values from a bitmap i.e.:

public static YUV[,] ToYuvLattice(this System.Drawing.Bitmap bm)
{
    var lattice = new YUV[bm.Width, bm.Height];
    for(var ix = 0; ix < bm.Width; ix++)
    {
        for(var iy = 0; iy < bm.Height; iy++)
        {
            lattice[ix, iy] = bm.GetPixel(ix, iy).ToYUV();
        }
    }
    return lattice;
}

Then I need to extract sets with the same U and V components. I.e. Set1 contains all [item1;item2] pairs, Set2 conatains [_item1;_item2] pairs. So I want to get List of Lists.

public IEnumerable<List<Cell<YUV>>> ExtractClusters()
{            
    foreach(var cell in this.lattice)
    {
        if(cell.Feature.U != 0 || cell.Feature.V != 0)
        {
            // other condition to be defined
        }
        // null yet
        yield return null;
    }
} 

I started with above code but I stuck with condition to distinct values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2024-11-20 11:31:40

听起来您有一个等价关系并且想要对数据进行分区。通过等价关系,我的意思是:

  1. A r A
  2. A r B => B r A
  3. A r B 和 B r C => A r C

如果这就是你所拥有的,那么这应该可以工作。

public static class PartitionExtension
{
    static IEnumerable<List<T>> Partition<T>(this IEnumerable<T> source, Func<T, T, bool> equivalenceRelation)
    {
        var result = new List<List<T>>();
        foreach (var x in source)
        {
            List<T> partition = result.FirstOrDefault(p => equivalenceRelation(p[0], x));
            if (partition == null)
            {
                partition = new List<T>();
                result.Add(partition);
            }

            partition.Add(x);
        }
        return result;
    }
}

用法:

return this.lattice
.Where( c=> c.Feature.U != 0 && c.Feature.V != 0 )
.Partition((x,y)=>
     x.Feature.U == y.Feature.U &&
     x.Feature.V == y.Feature.V);

It sounds like you have an equivalence relation and you want to partition the data. By equivalence relation, I mean:

  1. A r A
  2. A r B => B r A
  3. A r B and B r C => A r C

If that is what you have then this should work.

public static class PartitionExtension
{
    static IEnumerable<List<T>> Partition<T>(this IEnumerable<T> source, Func<T, T, bool> equivalenceRelation)
    {
        var result = new List<List<T>>();
        foreach (var x in source)
        {
            List<T> partition = result.FirstOrDefault(p => equivalenceRelation(p[0], x));
            if (partition == null)
            {
                partition = new List<T>();
                result.Add(partition);
            }

            partition.Add(x);
        }
        return result;
    }
}

Usage:

return this.lattice
.Where( c=> c.Feature.U != 0 && c.Feature.V != 0 )
.Partition((x,y)=>
     x.Feature.U == y.Feature.U &&
     x.Feature.V == y.Feature.V);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文