小编use*_*293的帖子

从XML移除ns0

我有一个XML文件,我想在其中编辑某些属性。我能够正确地编辑属性,但是当我将更改写入文件时,标记上添加了一个奇怪的“ ns0”。我该如何摆脱呢?这是我尝试过但未成功的方法。我正在使用python并使用lxml。

    import xml.etree.ElementTree as ET
    from xml.etree import ElementTree as etree
    from lxml import etree, objectify
    frag_xml_tree = ET.parse(xml_name)
    frag_root = frag_xml_tree.getroot()

    for e in frag_root: 
        for elem in frag_root.iter(e):
            elem.attrib[frag_param_name] = update_val
        etree.register_namespace("", "http://www.w3.org/2001")
        frag_xml_tree.write(xml_name)
Run Code Online (Sandbox Code Playgroud)

但是,执行此操作时,只会收到错误“无效标签名称u”。如果xml标记以数字开头,我认为会出现此错误,但我的xml并非如此。我真的对如何继续执迷不悟。谢谢

python xml lxml

7
推荐指数
2
解决办法
3980
查看次数

加快 Python 中图像距离变换的计算

我想在不使用 scipy 包 distance_trnsform_edt() 的情况下以最快的方式找到二值图像的距离变换。图像是 256 x 256。我不想使用 scipy 的原因是因为在 tensorflow 中使用它很困难。每次我想使用这个包时,我都需要开始一个新的会话,这需要很多时间。所以我想做一个只使用numpy的自定义函数。

我的方法如下:找到图像中所有 1 和所有零的协调。找到每个零像素 (a) 和一个像素 (b) 之间的欧几里德距离,然后每个 (a) 位置的值是到 (b) 像素的最小距离。我为每个 0 像素执行此操作。生成的图像与原始二值图具有相同的尺寸。我这样做的尝试如下所示。

我试图尽可能快地做到这一点,不使用循环,只使用矢量化。但是我的函数仍然不能像 scipy 包那样快。当我对代码进行计时时,对变量“a”的赋值似乎花费了最长的时间。但我不知道是否有办法加快速度。

如果有人对不同的算法有任何其他建议来解决这个距离变换问题,或者可以指导我使用 python 中的其他实现,将不胜感激。

def get_dst_transform_img(og): #og is a numpy array of original image
    ones_loc = np.where(og == 1)
    ones = np.asarray(ones_loc).T # coords of all ones in og
    zeros_loc = np.where(og == 0)
    zeros = np.asarray(zeros_loc).T # coords of all zeros in og

    a = -2 * np.dot(zeros, ones.T) 
    b = np.sum(np.square(ones), …
Run Code Online (Sandbox Code Playgroud)

python numpy computer-vision

5
推荐指数
1
解决办法
3694
查看次数

标签 统计

python ×2

computer-vision ×1

lxml ×1

numpy ×1

xml ×1