Python:使用多线程友好代码计算轮廓所包围的面积



我想扩展这个问题,以多线程友好的方式找到轮廓的面积。

我尝试了以下代码:

    c = cntr.Cntr(Z, X_, radial)
    # Trace a contour at calc_levels
    for loop in range(len(calc_levels)):
        res = c.trace(calc_levels[loop])
        # result is a list of arrays of vertices and path codes
        # (see docs for matplotlib.path.Path)
        nseg = len(res) // 2
        segments = res[:nseg]
        area = PolyArea(segments[0][:,0], segments[0][:,1])
def PolyArea(x,y):
    return 0.5*np.abs(np.dot(x,np.roll(y,1))-np.dot(y,np.roll(x,1)))

我尝试从这里调整代码以及PolyArea的代码来自这个问题 但是1(面积的计算不太正确,2(它减慢了我的多线程代码。

说该区域不正确,因为使用以下代码,我花了很长时间检查,我得到了完全不同的答案。但是,此代码绝对不兼容多线程,并导致整个代码

停止
    fig1 = plt.figure()
    ax1 = fig1.add_subplot(111)
    cs = ax1.contour(Z,X_,radial, levels = calc_levels,colors='k')
    for loop in range(len(calc_levels)):
        vs = None
        contour_ = None
        contour_ = cs.collections[loop]
        vs = contour_.get_paths()[0].vertices
        # Compute area enclosed by vertices
        features['Radial_Area_' + mystr + str(int(thisLevels[loop]*100))] = area(vs)

有没有人能够帮助我调试代码的第一部分,或者编写一些不同的东西来更好地工作?

谢谢

抱歉,我没有比较喜欢和喜欢。现在查看相同的文件,我发现计算的面积是相同的!所以代码

c = cntr.Cntr(Z, X_, radial)
# Trace a contour at calc_levels
for loop in range(len(calc_levels)):
    res = c.trace(calc_levels[loop])
    # result is a list of arrays of vertices and path codes
    # (see docs for matplotlib.path.Path)
    nseg = len(res) // 2
    segments = res[:nseg]
    area = PolyArea(segments[0][:,0], segments[0][:,1])
def PolyArea(x,y):
    return 0.5*np.abs(np.dot(x,np.roll(y,1))-np.dot(y,np.roll(x,1)))

确实按照我认为的那样工作,并且比其他代码快得多。

我稍后会删除这篇文章,让那些正在看时间的人看看我的答案

最新更新