使用python从XML文件中提取2D数组数据

时间:2017-07-10 20:10:38

标签: python arrays xml

我有一个立体图像的深度图女巫被保存为xml文件,我想从这个文件中提取2D浮点数并将其保存为浮动图像,我解决了我的请求的第二部分女巫正在保存2D数组作为浮动图像成功,我搜索了相关的答案,但我没有找到,所以有人会帮我吗?我的XML文件看起来与此类似:


?xml version="1.0"?

opencv_storage

depth type_id="opencv-matrix"

   rows 480 /rows

   cols 640 /cols

   dt f /dt

   data

    2.66372559e+02 2.66372559e+02 2.66372559e+02 2.66372559e+02
    2.66372559e+02 2.66372559e+02 2.66372559e+02 2.66372559e+02
    2.66372559e+02 2.66372559e+02 2.66372559e+02 2.66372559e+02
    2.66372559e+02 2.66372559e+02 2.66372559e+02 2.65196075e+02
    2.64019592e+02 2.64019592e+02 2.64019592e+02 2.64019592e+02
    2.64019592e+02 2.63823517e+02 2.63823517e+02 2.64607849e+02 ....

1 个答案:

答案 0 :(得分:0)

我已解决了以下问题:

from PIL import Image
import cv2

cv_file = cv2.FileStorage("test.xml", cv2.FILE_STORAGE_READ)       % reading my xml file as a storage file
             matrix = cv_file.getNode("depth").mat()               % getting the node of the 2D array named "depth" as shown in the  above xml file.
             img1 = Image.fromarray(matrix)                        % reading the 2D array as an image
             img1.save(dest, "tiff")                               % saving the image with a tiff format