V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Zhepro
V2EX  ›  Python

如何用代码表示图像上的一个范围?并看一个点是否在该范围内?

  •  
  •   Zhepro · 2020-06-05 16:34:39 +08:00 · 2980 次点击
    这是一个创建于 1392 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有一张底图,希望能在上面用代码表示一个范围,并计算一个坐标是否在该范围内,怎么办?

    目前的思路是用几个长方形和圆形拼接,但是很不精确,有更好的方法吗?
    15 条回复    2020-06-08 10:53:37 +08:00
    Latin
        1
    Latin  
       2020-06-05 17:00:36 +08:00
    绘制多边形啊
    sivacohan
        2
    sivacohan  
       2020-06-05 17:06:57 +08:00
    python cv2
    图像就是一个二维数组,你就是检查数组里面某个特征点是否落在指定的 Index 范围。
    Latin
        3
    Latin  
       2020-06-05 17:07:42 +08:00
    判断点是否在多边形区域内的方法
    https://paste.ubuntu.com/p/dbPJBCcf7j/
    MLLB
        4
    MLLB  
       2020-06-05 17:16:09 +08:00
    numpy.array
    littleylv
        5
    littleylv  
       2020-06-05 17:22:35 +08:00
    针对凸多边形,任意形状,任意边数,只需要知道连续的顶点的坐标,[x0,y0],[x1,y1]...[xn,yn]
    对任意点[x,y],有一套算法可以得出[x,y]是否在多边形范围内
    arrow8899
        6
    arrow8899  
       2020-06-05 17:26:36 +08:00
    point in polygon 问题,github 上搜一大把
    https://www.algorithms-and-technologies.com/point_in_polygon/
    不过要注意一些特殊情况,比如点重合,点在边上等,然后结合需求修改代码。
    RealMadrid
        7
    RealMadrid  
       2020-06-06 08:07:31 +08:00
    用 python 的话,可以用 matplotlib 里面 path,来判断点是否在一个多边形里
    from matplotlib import path
    def is_point_in_polygon(point, polygon):
    p = path.Path(polygon)
    return p.contains_point(point)
    qile1
        8
    qile1  
       2020-06-06 10:14:25 +08:00 via Android
    图像转为坐标点,然后判断两次 x 和 y 是否大于最大值是不是就可以,这个点你如何获取?
    qile1
        9
    qile1  
       2020-06-06 10:39:55 +08:00 via Android
    https://www.v2ex.com/t/674900#;
    楼上几位大佬能不能帮看看我这个问题,谢谢
    aguesuka
        10
    aguesuka  
       2020-06-06 15:29:47 +08:00
    这是个很大的问题,计算机图形学的核心问题之一
    tblxdezhu
        11
    tblxdezhu  
       2020-06-07 21:27:28 +08:00
    楼上的都不知道这个库吗? Shapely,实例化 Point
    文档在此: https://shapely.readthedocs.io/en/latest/manual.html
    necomancer
        12
    necomancer  
       2020-06-08 10:29:45 +08:00
    1. 获取区域 pointcloud 的坐标,这个想办法,或者找现成工具例如 datathief;
    2. 用 scipy 的 convex hull 模块生成 hull = convexhull(pointcloud);
    3. 判断使用 point_in_hull = np.allclose([p.dot(_[:-1]) for _ in hull.equations],0)

    如果不是用 Point cloud 表示一个奇怪的形状,而是有方程描述,直接用方程。尤其对形状有线性方程描述,直接使用上述的 3 就可以了。
    necomancer
        13
    necomancer  
       2020-06-08 10:34:36 +08:00
    不是数据坐标的话,就用像素坐标
    necomancer
        14
    necomancer  
       2020-06-08 10:43:27 +08:00
    我弄错了一个地方……sorry,应该是 np.all(hull.equations[:,:-1].dot(p) + hull.equations.T[-1] < 0)

    The convex hull of a point set P is the smallest convex set that contains P. If P is finite, the convex hull defines a matrix A and a vector b such that for all x in P, Ax+b <= [0,...].

    Qhull 的文档,我给搞成等于 0 了。
    necomancer
        15
    necomancer  
       2020-06-08 10:53:37 +08:00
    np.all(np.less_equal(np.einsum('ij,...j->...i', hull.equations[:,:-1], <array(N_points, N_dimension)>) + hull.equations.T[-1], 0), axis=-1),可以做批量判断,numpy 的处理比 for 循环快很多。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1130 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:47 · PVG 02:47 · LAX 11:47 · JFK 14:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.