我目前正在开展一个项目,涉及在像素级别上操纵二进制图像(细化,聚类等)。所使用的许多算法涉及检查相邻像素的属性。像素对象定义为:
Public class pixel
{
int x, y;
int NumberOfNeighbours;
int ConnectivityNumber;
int ClusterNumber;
}
我可以创建一个包含图像中所有像素的列表,用于按顺序处理它们,但我想将列表中的每个像素链接到它们在网格上的位置,以有效地获取其相邻像素的属性。
例如,如果我在地图中有一个9像素[A到I]的地图[7,7],
http://i.imgur.com/lTzSM.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;
任何建议/指导都将不胜感激。
答案 0 :(得分:0)
Public class pixel
{
int x, y;
int NumberOfNeighbours;
int ConnectivityNumber;
int ClusterNumber;
//neighbors:
Pixel _neighborN;
Pixel _neighborNE;
...
Pixel _neighborNW;
}
这里需要在初始化期间分配所有邻居像素。如果当前像素靠近边缘,则只需将其指定为null。但是当你使用它们时,不要忘记检查空值。