如何用代码表示图像上的一个范围?并看一个点是否在该范围内? - V2EX
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 3739 次点击
    这是一个创建于 1954 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有一张底图,希望能在上面用代码表示一个范围,并计算一个坐标是否在该范围内,怎么办?

    目前的思路是用几个长方形和圆形拼接,但是很不精确,有更好的方法吗?
    15 条回复    2020-06-08 10:53:37 +08:00
    Latin
        1
    Latin  
       2020-06-05 17:00:36 +08:00
    绘制多边形啊
    sivacohan
        2
    sivacohan  
    PRO
       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  
       200-06-05 17:16:09 +08:00
    numpy.array
    U7Q5tLAex2FI0o0g
        5
    U7Q5tLAex2FI0o0g  
       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
    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     Solana     2691 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 136ms UTC 07:24 PVG 15:24 LAX 00:24 JFK 03:24
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86