I am currently working on a project involving manipulation of binary images on pixel level (thinning, clustering etc.). Many of the algorithms used involve checking the attributes of neighboring pixels. A pixel object defined as:
Public class pixel
{
int x, y;
int NumberOfNeighbours;
int ConnectivityNumber;
int ClusterNumber;
}
I can create a list containing all the pixels in the image for processing them sequentially but I want to link each of the pixels in the list to their position on a grid to efficiently get properties of its neighbor pixels.
For example, if I have a map[7,7] with 9 pixels [A to I] in the map,
https://i.stack.imgur.com/UwHTn.jpg
and I want to check the value of the ClusterNumber of each neighbour of each pixel. It seems very inefficient to run through the list 8 times to find the neighbors of each pixel and it would be easier to use the grid to just check the neighbors. Is there a way to reference a pixel in the grid so that I can access its properties as a neighbor of the current pixel in the list? I’ve thought about creating a int[,] map = new int[width,height] containing the indexes of the pixels in the list, but this creates a problem when a pixel is removed from the list and map would need to be updated every time a pixel is removed from the list.
Alternatively, is there a way I can link/point to the neighbors of a pixel in the class itself such that (schematically) I could access a neighbor’s properties
//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;
Any advice/guidance would be appreciated.
Here you need to assign all neighbors pixels during initialization. If current pixels is near the edge, you just assign it to null. But do not forget to check null-values when you operate with them.