How to find neighbours of each region ?
After I run the watershed segmentation algorithm on a subset of my actual image, I get the following output where the black lines indicate the boundary separating two regions. I also have another Mat that stores the labels of each region i.e. pixels belonging to a region would have a similar label.
I would now like to find the neighbors of each region. In the example below, I would have
A is connected to B and C
B is connected to A and C
C is connected to A and B
In my implementation, when I am at a certain pixel (blue box), I scan the outer ring (orange boxes) to see if there is a change in the label and I update the adjacency list accordingly. The problem is, I am visiting each pixel multiple times and is quite inefficient. And so I was wondering if anyone has any suggestion on how I can initialize this adjacency list in an efficient manner. Note that the neighbors can be 8-connected.
I hope i'm wrong but I don't know function in opencv that can give you neighbourhood. You have to write your own code using dilate
I also think that you have to write your own algorithm. However I think that only one pass through the image will allow you to create the neighborhood list.
If speed is critical, parallelize the outer loop. You can also use line pointers for fast element access (instead of image.at).
@kbarni Which algorithm can do such job in one pass?