Ask Your Question
1

How to find neighbours of each region ?

asked 2017-03-28 23:50:02 -0600

Nbb gravatar image

After I run the watershed segmentation algorithm on a subset of my actual image, I get the following output image description 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.

image description

edit retag flag offensive close merge delete

Comments

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

LBerger gravatar imageLBerger ( 2017-03-29 01:57:54 -0600 )edit

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 gravatar imagekbarni ( 2017-03-29 05:51:44 -0600 )edit

@kbarni Which algorithm can do such job in one pass?

LBerger gravatar imageLBerger ( 2017-03-29 08:24:39 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-03-29 10:48:42 -0600

kbarni gravatar image

updated 2017-03-29 10:53:36 -0600

Here is a pseudocode of the algorithm I think would work:

for each pixel P in image
    if(P!=0)  //skip the 0 pixels
        neighbor_vector = getneighbors(P) //the list of the neighbor pixels (marked in orange)
                                          //without the zeros and pixels with value P
        for each element E in neighbor_vector
            if(connections[P] doesn't contain E)
                connections[P].push_back(E)

connections is an array of vectors with as many elements as the number of labels. Each vector in the connections will contain the list of the neithboring labels to the current label.

The only step where you scan the pixels several times is in the getneighbors function. But this is inevitable, even with morphological operations.

I think this algorithm is quite fast, and can be easily parallelized. Just be sure to avoid direct pixel access with the at operator.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-03-28 23:50:02 -0600

Seen: 1,828 times

Last updated: Mar 29 '17