如何在c#中链接/指向特定的对象

本文关键字:对象 链接 | 更新日期: 2023-09-27 18:10:55

我目前正在研究一个涉及在像素级(细化,聚类等)操作二进制图像的项目。使用的许多算法涉及检查相邻像素的属性。定义为:

的像素对象
Public class pixel
{
int x, y;
int NumberOfNeighbours;
int ConnectivityNumber;
int ClusterNumber;
}

我可以创建一个包含图像中所有像素的列表,用于顺序处理它们,但我想将列表中的每个像素链接到它们在网格上的位置,以有效地获取其相邻像素的属性。

例如,如果我有一个地图[7,7],在地图中有9个像素[a到I],https://i.stack.imgur.com/UwHTn.jpg我想检查每个像素的每个邻居的ClusterNumber的值。在列表中运行8次来找到每个像素的邻居似乎非常低效,而使用网格来检查邻居会更容易。是否有一种方法可以在网格中引用pixel,以便我可以访问其属性作为列表中当前像素的邻居?我想过创建一个包含列表中像素索引的int[,] map = new int[width,height],但这会产生一个问题,当从列表中删除一个像素时,每次从列表中删除一个像素时,都需要更新映射。

或者,是否有一种方法可以链接/指向类本身中像素的邻居,这样(粗略地)我可以访问邻居的属性

//list[0].NeighborE  == null
//list[0].NeighborNE == B
//list[0].neighborN  == null
//...
//list[1].NeighborE  == G
//list[1].NeighborNE == D
//list[1].NeighborN  == C
//list[1].NeighborNW == null
...
if (list[i].NeighborE.ClusterNumber > 2)
    list[i].ClusterNumber = 2;

如何在c#中链接/指向特定的对象

Public class pixel
{
    int x, y;
    int NumberOfNeighbours;
    int ConnectivityNumber;
    int ClusterNumber;
    //neighbors:
    Pixel _neighborN;
    Pixel _neighborNE;
    ...
    Pixel _neighborNW;
}

这里你需要在初始化时分配所有的邻居像素。如果当前像素靠近边缘,只需将其赋值为null。但是在操作空值时,不要忘记检查空值