把图象文件转换成XML格式文件

把图象文件转换成XML格式文件

作者: 孟宪会 出自: 【孟宪会之精彩世界】 发布日期: 2003-8-31 2:10:12


 

利用.NET 框架下的FromBase64String和ToBase64String方法可以很容易地实现图象文件和XML文件的互换。这样可以轻易解决以XML格式保存图片的问题。代码如下:

Public   Class  Form1
            
Inherits  System.Windows.Forms.Form
            
#Region  " Windows 窗体设计器生成的代码 "
            
Public   Sub   New ()
            
MyBase .New()
            InitializeComponent()
            
' 在 InitializeComponent() 调用之后添加任何初始化
             End Sub
            
' 窗体重写处置以清理组件列表。
             Protected   Overloads   Overrides   Sub  Dispose( ByVal  disposing  As   Boolean )
            
If  disposing  Then
            
If   Not  (components  Is   Nothing Then
            components.Dispose()
            
End   If
            
End   If
            
MyBase .Dispose(disposing)
            
End Sub
            
' Windows 窗体设计器所必需的
             Private  components  As  System.ComponentModel.IContainer
            
' 注意:以下过程是 Windows 窗体设计器所必需的
             ' 可以使用 Windows 窗体设计器修改此过程。
             ' 不要使用代码编辑器修改它。
             Friend   WithEvents  Button1  As  System.Windows.Forms.Button
            
Friend   WithEvents  Button2  As  System.Windows.Forms.Button
            
Friend   WithEvents  PictureBox1  As  System.Windows.Forms.PictureBox
            
Friend   WithEvents  Button3  As  System.Windows.Forms.Button
            
Friend   WithEvents  Label1  As  System.Windows.Forms.Label
            
Friend   WithEvents  Label2  As  System.Windows.Forms.Label
            
& lt;System.Diagnostics.DebuggerStepThrough() & gt;  Private   Sub  InitializeComponent()
            
Me .Button1  =   New  System.Windows.Forms.Button()
            
Me .Button2  =   New  System.Windows.Forms.Button()
            
Me .PictureBox1  =   New  System.Windows.Forms.PictureBox()
            
Me .Button3  =   New  System.Windows.Forms.Button()
            
Me .Label1  =   New  System.Windows.Forms.Label()
            
Me .Label2  =   New  System.Windows.Forms.Label()
            
Me .SuspendLayout()
            
'
             ' Button1
             '
             Me .Button1.Location  =   New  System.Drawing.Point( 365 63 )
            
Me .Button1.Name  =   & quot;Button1 & quot;
            
Me .Button1.Size  =   New  System.Drawing.Size( 115 23 )
            
Me .Button1.TabIndex  =   0
            
Me .Button1.Text  =   & quot;将图象保存成XML & quot;
            
'
             ' Button2
             '
             Me .Button2.Location  =   New  System.Drawing.Point( 365 98 )
            
Me .Button2.Name  =   & quot;Button2 & quot;
            
Me .Button2.Size  =   New  System.Drawing.Size( 115 23 )
            
Me .Button2.TabIndex  =   1
            
Me .Button2.Text  =   & quot;从XML中得到图象 & quot;
            
'
             ' PictureBox1
             '
             Me .PictureBox1.Location  =   New  System.Drawing.Point( 18 6 )
            
Me .PictureBox1.Name  =   & quot;PictureBox1 & quot;
            
Me .PictureBox1.Size  =   New  System.Drawing.Size( 320 460 )
            
Me .PictureBox1.TabIndex  =   2
            
Me .PictureBox1.TabStop  =   False
            
'
             ' Button3
             '
             Me .Button3.Location  =   New  System.Drawing.Point( 365 28 )
            
Me .Button3.Name  =   & quot;Button3 & quot;
            
Me .Button3.Size  =   New  System.Drawing.Size( 115 23 )
            
Me .Button3.TabIndex  =   3
            
Me .Button3.Text  =   & quot;浏览图片 & amp;# 8230 ; & quot;
            
'
             ' Label1
             '
             Me .Label1.Location  =   New  System.Drawing.Point( 369 135 )
            
Me .Label1.Name  =   & quot;Label1 & quot;
            
Me .Label1.Size  =   New  System.Drawing.Size( 105 95 )
            
Me .Label1.TabIndex  =   4
            
'
             ' Label2
             '
             Me .Label2.Location  =   New  System.Drawing.Point( 367 437 )
            
Me .Label2.Name  =   & quot;Label2 & quot;
            
Me .Label2.Size  =   New  System.Drawing.Size( 130 16 )
            
Me .Label2.TabIndex  =   5
            
Me .Label2.Text  =   & quot;【孟宪会之精彩世界】 & quot;
            
'
             ' Form1
             '
             Me .AutoScaleBaseSize  =   New  System.Drawing.Size( 5 13 )
            
Me .ClientSize  =   New  System.Drawing.Size( 500 480 )
            
Me .Controls.AddRange( New  System.Windows.Forms.Control() { Me .Label2,  Me .Label1, _
            
Me .Button3,  Me .PictureBox1,  Me .Button2,  Me .Button1})
            
Me .Name  =   & quot;Form1 & quot;
            
Me .Text  =   & quot;图象文件和XML格式文件互换例子 & quot;
            
Me .ResumeLayout( False )
            
End Sub
            
#End Region
            
Private  MyFile  As   String   =   & quot; & quot;
            
Private  MyFileExt  As   String   =   & quot; & quot;
            
Private   Sub  Button2_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs) _
            
Handles  Button2.Click
            
Dim  pic  As   String
            
Dim  MyXml  As  System.Xml.XmlDocument  =   New  System.Xml.XmlDocument()
            MyXml.Load(
& quot;c: MyPhoto.xml & quot;)
            
Dim  picNode  As  System.Xml.XmlNode
            picNode 
=  MyXml.SelectSingleNode( & quot; / pic / photo & quot;)
            pic 
=  picNode.InnerText
            
Dim  memoryStream  As  System.IO.MemoryStream
            memoryStream 
=   New  System.IO.MemoryStream(Convert.FromBase64String(pic))
            
Me .PictureBox1.Image  =   New  System.Drawing.Bitmap(memoryStream)
            memoryStream.Close()
            
End Sub
            
Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs) _
            
Handles  Button1.Click
            
If  MyFile  =   & quot; & quot;  Then
            MessageBox.Show(
& quot;请选择一个图片! & quot;,  & quot;错误 & quot;, MessageBoxButtons.OK, MessageBoxIcon.Warning)
            
Exit Sub
            
End   If
            
Dim  MyImg  As  System.Drawing.Image  =  MyImg.FromFile(MyFile)
            
Dim  memoryStream  As  System.IO.MemoryStream  =   New  System.IO.MemoryStream()
            MyImg.Save(memoryStream, GetImageType(MyFileExt))
            
Dim  b()  As   Byte
            b 
=  memoryStream.GetBuffer()
            
Dim  pic  As   String   =  Convert.ToBase64String(b)
            memoryStream.Close()
            
Dim  MyXml  As  System.Xml.XmlDocument  =   New  System.Xml.XmlDocument()
            MyXml.LoadXml(
& quot; & lt;pic & gt; & lt;name & gt;孟宪会 & lt; / name & gt; & lt;photo & gt; & quot;  +  pic  +   & quot; & lt; / photo & gt; & lt; / pic & gt; & quot;)
            MyXml.Save(
& quot;c: MyPhoto.xml & quot;)
            Label1.Text 
=   & quot;文件被保存到了: & quot;  +  Microsoft.VisualBasic.ChrW( 13 +   & quot;c: MyPhoto.xml & quot;
            
End Sub
            
Private   Sub  Button3_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs) _
            
Handles  Button3.Click
            
Dim  openFileDialog1  As   New  OpenFileDialog()
            openFileDialog1.InitialDirectory 
=   & quot;c: & quot;
            openFileDialog1.Filter 
=   & quot;PNG( * .png)| * .png|Gif( * .gif)| * .gif|Jpg( * .jpg)| * .jpg|所有图象文件( * . * )| * . *& quot;
            openFileDialog1.FilterIndex 
=   2
            openFileDialog1.RestoreDirectory 
=   True
            
If  openFileDialog1.ShowDialog()  =  DialogResult.OK  Then
            MyFile 
=  openFileDialog1.FileName()
            MyFileExt 
=  MyFile.Substring(MyFile.LastIndexOf( & quot;. & quot;)  +   1 )
            
End   If
            
End Sub
            
Public   Function  GetImageType( ByVal   str   As   String As  System.Drawing.Imaging.ImageFormat
            
Select   Case   str .ToLower()
            
Case   & quot;jpg & quot;
            
Return  System.Drawing.Imaging.ImageFormat.Jpeg
            
Case   & quot;gif & quot;
            
Return  System.Drawing.Imaging.ImageFormat.Gif
            
Case   & quot;tiff & quot;
            
Return  System.Drawing.Imaging.ImageFormat.Tiff()
            
Case   & quot;icon & quot;
            
Return  System.Drawing.Imaging.ImageFormat.Icon
            
Case   & quot;image / png & quot;
            
Return  System.Drawing.Imaging.ImageFormat.Png
            
Case   Else
            
Return  System.Drawing.Imaging.ImageFormat.MemoryBmp
            
End   Select
            
End Function
            
Private   Sub  Form1_Closing( ByVal  sender  As   Object ByVal  e  As  System.ComponentModel.CancelEventArgs) _
            
Handles   MyBase .Closing
            System.Diagnostics.Process.Start(
& quot;IExplore.exe & quot;,  & quot;http: // xml.sz.luohuedu.net /& quot;)
            
End Sub
            
End Class

结果如下:

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
### 回答1: 将XML文件格式转化为YOLO格式的过程,需要经过以下几个步骤: 第一步,解析XML文件。使用Python中的ElementTree模块,读取XML文件中每一条标注信息。将每个对象的类别、位置、长宽等信息提取出来。 第二步,将位置信息用YOLO的格式表示。YOLO坐标系以图像的宽度和高度为基础,中心点坐标用归一化的形式表示。即将对象的位置信息用相对于整张图片的比例来表示。 第三步,将所有信息打包成YOLO的标注格式。每一张图片对应一个txt文件,用每个对象在图片中的位置和大小,以及对象的类别,按照一定的格式记录在文本文件中,每一行表示一个对象。 最后,需要对每个对象的类别进行编号。在YOLO中,每个对象有一个类别号,该号码是从0开始的,表示每一种类别。可以在代码中定义不同的类别号,如人、动物、汽车等等。在将文本文件存储为YOLO的标注格式之前,需要先根据类别号将对象的类别替换为相应的类别号码。 综上所述,将XML文件格式转换为YOLO格式需要先解析XML文件,将位置信息用YOLO格式表示,打包成YOLO格式的标注文件,并进行类别编号。这个过程需要使用Python编程语言和YOLO标注工具,可以有效地提高图像标注的效率和准确性。 ### 回答2: XML文件是一种用于存储结构化数据的文件格式,而YOLO是一种目标检测算法。将XML文件转换为YOLO格式可以使我们通过YOLO算法来进行对象检测。 要将XML文件格式转换成YOLO,通常需要进行以下步骤: 1、读取XML文件并解析出其中的数据。XML文件中通常会包含每个对象的类别、位置和大小等信息。 2、根据解析出的数据计算每个对象的边框左上角坐标(x,y)、宽度w和高度h,并计算中心坐标xc和yc以及标准化后的坐标(如YOLO中使用的相对坐标)。 3、将每个对象的类别和标准化后的坐标和大小等信息存储到一个文本文件中,文本文件的格式为:class x y w h(或 class xc yc w h)。其中“class”表示对象的类别,x,y,w,h分别表示对象的左上角坐标和宽高。xc, yc, w, h是相对坐标 4、将文本文件中的数据按照一定的格式转换成YOLO所需的标准格式,如将类别用数字表示,将坐标和大小除以图像的宽度和高度等。 5、将转换后的文件存储到指定位置,以便YOLO算法使用。 需要注意的是,不同的目标检测算法所需的数据格式可能不同,因此在转换前需要了解所用的算法的数据格式要求。此外,转换的准确性也取决于XML文件中的数据质量,如对象位置和大小的准确度,因此需要保证XML文件中的数据质量。 ### 回答3: Yolo是一种用于目标检测的深度学习算法,可以实现快速且准确的目标检测。要将xml文件格式转换成Yolo格式,我们需要完成以下几个步骤: 1. 准备数据集 首先,我们需要准备用于训练的数据集。数据集应包含至少一个xml文件和相应的图像文件xml文件应包含每个图像中每个对象的标注信息,例如位置,类别等。 2. 解析xml文件 接下来,我们需要使用解析库,例如ElementTree等,读取xml文件,提取包含对象标注的标签和属性。对于每个对象,我们需要计算出它的边界框的坐标和类别。 3. 转换为Yolo格式 此时,我们必须将边界框的坐标和类别转换为Yolo格式。Yolo使用归一化后的坐标,即,边界框的左上角坐标表示为相对于图像宽度和高度的比例,边界框的宽度和高度也是相对于图像宽度和高度的比例。同时,我们还需要将类别标签转换为数字,例如将"cat"转换为0,"dog"转换为1等。 4. 保存为文本文件 最后,我们需要将每个图像的边界框坐标和类别保存到一个文本文件中,该文件的名称应与图像文件名称相同,但扩展名应为txt。每个文本文件中的每一行应包含一个对象的信息。每一行应以类别数字为开头,其后是归一化的边界框坐标,用空格分隔。 完成上述步骤后,我们就可以使用转换后的Yolo格式进行目标检测的训练和测试了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟子E章

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值