Shan
Shan

Reputation: 19293

how to create non-rectangular bounding box around specific values in 2d numpy array?

I have a 2D numpy array. I want to find the non rectangular bounding box around specific values of the array. Rectangular bounding box has already been solved in the Question:How to select all locations of unique elements in numpy 2d array with bounding box around them?

Lets consider the following example

array([[1, 1, 2, 2],\
      [0, 1, 0, 1],\
      [3, 0, 1, 4],\
      [0, 3, 1, 1]])

The result is a bit complicated

 For one unique value 1, (0,0),(0,1),(1,1),(1,2),(1,3),(2,2), (3,2),(3,3)

We want to construct the indices in such a way that zero values within the extremas are considered within the region. In this sense it is more of a region segment problem with exactly similar value with exception of zeros inside the region. It is defining the boundary of a region.

It is to be noted that the region is only consist of a unique value and zero. By construction of the problem, there can not be any non-unique values lying within one another.

Perhaps, it is contour finding problem in an image in the image processing sense(I am not sure)

One we find this region. We want to search from a bunch of feature that which feature lie in a certain regions. For example we detect SIFT. We want to find which sift features lie in one unique area without any comparison of the features.

Please let me know for further clarifications.

EDIT: It is important to note that this is not a connected component problem. In connected component one region is actually consist of homogeneous values. In our case there can be zeros (or any other value) in between. So this is merely detecting a region constrained by a unique value. It is also important to note that inside the region there can not be any arbitrary values but a single value like zero or 255 depicting the background.

Thanks a lot.

Upvotes: 2

Views: 1042

Answers (1)

cyborg
cyborg

Reputation: 10139

You can use a segmentation algorithm based on a graph partition method. For example, you can use Random Walker. But you will have to alter the weights in the following way.

For every pixel v_i let g_i be the pixel's color. While the original algorithm defines w_{ij} as:

w_{ij} = exp{-beta*(g_i - g_j)^2)}

You should then set w_{ij}=0 when g_i==0 or g_j==0.

Upvotes: 1

Related Questions