MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   用Python或C语言编写的Matlab / Octave bwdist() (https://www.labfans.com/bbs/showthread.php?t=26564)

poster 2019-12-14 20:13

用Python或C语言编写的Matlab / Octave bwdist()
 
有人知道Matlab / Octave bwdist()函数的Python替代品吗?对于给定的矩阵,此函数将每个像元的欧几里德距离返回到最接近的非零像元。我看到了一个Octave C实现,一个纯Matlab实现,我想知道是否有人必须在ANSI C(不包含任何Matlab / Octave标头,因此我可以很容易地从Python集成)中实现它。

我提到的两个链接如下:

[URL="http://octave-image.sourcearchive.com/documentation/1.0.10/____bwdist_8cc-source.html"]C ++[/URL]

[URL="http://www.irit.fr/PERSONNEL/SAMOVA/joly/Teaching/M2IRR/IRR05/Barre-Piquot/bwdist.m"]Matlab M文件[/URL]

作为测试,Matlab代码/输出如下所示:

bw= [0 1 0 0 0; 1 0 0 0 0; 0 0 0 0 1; 0 0 0 0 0; 0 0 1 0 0] D = bwdist(bw) D = 1.00000 0.00000 1.00000 2.00000 2.00000 0.00000 1.00000 1.41421 1.41421 1.00000 1.00000 1.41421 2.00000 1.00000 0.00000 2.00000 1.41421 1.00000 1.41421 1.00000 2.00000 1.00000 0.00000 1.00000 2.00000 我在Python中测试了一个推荐的distance_transform_edt调用,它给出了以下结果:

从scipy导入ndimage导入numpy作为np

a = np.array(([0,1,0,0,0], [1,0,0,0,0], [0,0,0,0,1], [0,0,0,0,0], [0,0,1,0,0])) res = ndimage.distance_transform_edt(a) print res [[ 0. 1. 0. 0. 0.] [ 1. 0. 0. 0. 0.] [ 0. 0. 0. 0. 1.] [ 0. 0. 0. 0. 0.] [ 0. 0. 1. 0. 0.]] 此结果似乎与Octave / Matlab输出不匹配。



[B]回答:[/B]

Matlab bwdist将距离返回到最接近的非零像元,而Python [URL="https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.morphology.distance_transform_edt.html"]distance_transform_edt[/URL]返回距离“最接近的背景元素”? SciPy文档尚不清楚它被认为是“背景”吗?它后面有一些类型转换机器;实际上, 0是背景,非零是前景。

因此,如果我们有矩阵a :

>>> a = np.array(([0,1,0,0,0], [1,0,0,0,0], [0,0,0,0,1], [0,0,0,0,0], [0,0,1,0,0])) 然后要计算相同的结果,我们需要将零替换为零,并将零替换为零,例如考虑矩阵1-a :

>>> a array([[0, 1, 0, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0]]) >>> 1 - a array([[1, 0, 1, 1, 1], [0, 1, 1, 1, 1], [1, 1, 1, 1, 0], [1, 1, 1, 1, 1], [1, 1, 0, 1, 1]]) 在这种情况下, [URL="https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.morphology.distance_transform_edt.html"]scipy.ndimage.morphology.distance_transform_edt[/URL]提供了预期的结果:

>>> distance_transform_edt(1-a) array([[ 1. , 0. , 1. , 2. , 2. ], [ 0. , 1. , 1.41421356, 1.41421356, 1. ], [ 1. , 1.41421356, 2. , 1. , 0. ], [ 2. , 1.41421356, 1. , 1.41421356, 1. ], [ 2. , 1. , 0. , 1. , 2. ]])

[url=https://stackoverflow.com/questions/5260232]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 09:01

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.