如何将包含图像信息的.xml文件转换为jpeg或png [英] How can I convert .xml files which contain image information to jpeg or png

查看:575
本文介绍了如何将包含图像信息的.xml文件转换为jpeg或png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是为人体动作检测"算法准备数据.总而言之,我得到了一些图像.xml文件(已使用Kinect传感器拍摄),我希望将其转换为jpeg或png,以便从算法中读取.我认为我必须使用OpenCV.

My task is to prepare the data for a Human Action Detection algorithm. To sum up I have been given some image .xml files (that have been taken with a Kinect sensor) that I want to convert to jpeg or png to be readable from the algorithm. I assume I have to use OpenCV.

我确定这是一个初学者的问题,但我在互联网上搜索后一无所获.

I'm sure it's a beginners question but I've searched the internet and found nothing.

<?xml version="1.0"?>
<opencv_storage>
<depthImg190 type_id="opencv-image">
  <width>320</width>
  <height>240</height>
  <origin>top-left</origin>
  <layout>interleaved</layout>
  <dt>w</dt>
  <data>
    0 0 0 0 27120 27384 27120 27120 27384 27120 27120 27120 27120 27384
    27384 27664 27664 27944 27944 27664 27664 27944 27944 27944 28224
    27944 27944 28224 28224 28224 28224 28520 28816 29120 29120 29120
    29120 29120 29120 29120 29432 29744 30072 30072 29744 29744 30072
    30072 30072 30400 30400 30736 30736 31080 31080 31080 31440 31440
    31440 31440 31800 31800 31800 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 27120 27120 27120 27120 27384 27384 27384 27384 27384 27384
    27664 27944 27944 27944 27944 27944 27944 27944 27944 27944 28224
    27944 28224 28224 28224 28224 28224 28520 29120 29120 29120 29120
    29120 29120 29120 29432 29432 29744 29744 30072 30072 30072 30072
    30400 30400 30400 30400 30736 30736 31080 31080 31440 31440 31440
    31440 31440 31800 31800 31800 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 27120 27120 27120 27120 27664 27384 27384 27384 27120 27384
    27664 27944 27944 27944 27944 27664 27944 27944 27944 27944 28224

推荐答案

这是一个非常丑陋的骇客,但是它允许您将XML文件转换为

This is a really ugly hack, but it allows you to convert the XML file into a NetPBM file which you can easily read with OpenCV, or ImageMagick, or Photoshop, or convert into a PNG or JPEG:

#!/bin/bash

xml=$1

# Get width and height of image
w=$(xmlstarlet sel -t -m "//width"  -v . -n "$xml")
h=$(xmlstarlet sel -t -m "//height" -v . -n "$xml")

# Generate PPM header - https://en.wikipedia.org/wiki/Netpbm_format
printf "P2\n$w $h\n65535\n" > image.ppm

# Append ASCII image data
xmlstarlet sel -t -m "//data" -v . -n "$xml"  >> image.ppm

您可以将其另存为HOME目录中的xml2ppm并使其可执行(仅一次):

You would save it in your HOME directory as xml2ppm and make it executable (just one time) with:

chmod +x $HOME/xml2ppm

您可以通过以下方式运行它:

You would run it with:

$HOME/xml2ppm someFile.xml

如果您假设所有图像均为320x240,并使用awk从XML中提取像素数据,则您也可以删除对xmlstarlet的依赖,并使其更加丑陋和骇人听闻.

You could also remove the dependency on xmlstarlet and make something even uglier and hackier, if you assume all the images are 320x240 and extract the pixel data from the XML using awk:

#!/bin/bash

# Generate PPM header - https://en.wikipedia.org/wiki/Netpbm_format
# and follow with ASCII data from XML file embedded in <data>...</data> tags

xml=$1

awk '
  BEGIN      {print "P2\n320 240\n65535\n"}
  /<data>/   {flag=1; next}
  /<\/data>/ {exit}
  flag
  ' "$xml" > image.ppm


您还可以使用 ImageMagick 将生成的 PPM 文件转换为 PNG :


You can also convert the resulting PPM file to PNG with ImageMagick:

magick image.ppm result.png    # convert to PNG
magick image.ppm result.jpg    # convert to JPEG

如果您的 ImageMagick 为v6或更高版本,请使用convert代替magick.

Use convert in place of magick if your ImageMagick is v6 or older.

关键字:XML,xmlstarlet,Kinect,OpenCV,opencv-storage,depthImg190,opencv-image

Keywords: XML, xmlstarlet, Kinect, OpenCV, opencv-storage, depthImg190, opencv-image

这篇关于如何将包含图像信息的.xml文件转换为jpeg或png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆