开发者

how to detect if a point lies in a non-rectangular region in a 2D numpy array?

I have a 2D array. Lets suppose tha开发者_开发问答t it has some connected region which is non rectangular. Consider the following example

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

In above array all the ones are connected. I know all these coordinates which are stored lets say as a 2d numpy array or a list of elements (x,y).

Now I want to query that if an arbitrary points lies in this area. The point could also be a float.

In case of float coordinates the values will be round of to the nearest integer pixel value.

Thanks a lot


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

def is_in_area(x,y, area_num):
    return areas[round(x), round(y)] == area_num

Is this what you're going for?

Of course, you might want some bounds checking in there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜