c# 读取opencv 生成的svm训练好的xml分类器

需要用到opencv里面SVM训练分类器的数据:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Linq; //需要添加的


namespace xml_duqu
{
    public partial class Form1 : Form
    {
        static string strPath ;  
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                strPath = open.FileName;
                
                test(strPath);
            }

        }

        private void test(string filename)
        {
            XElement xe = XElement.Load(strPath);//加载XML文件  
            //使用LINT从XML文件中查询信息  
            XmlTextReader reader = new XmlTextReader(filename);

            XmlDocument xml = new XmlDocument();
            //读取xml文件
            xml.Load(filename);  //你的xml地址
            
            richTextBox1.Text = "";

            FileStream fs = new FileStream("data.data", FileMode.Create);
            StreamWriter sw; 
            sw = new StreamWriter(fs, Encoding.Default);

            bool flag = false;
            while (reader.Read())
            {
                if (reader.Name != "")
                {
                    if (reader.Name == "sv_total" || reader.Name == "_" || reader.Name == "rho" || reader.Name == "alpha" || reader.Name == "index")//根据想要的属性保存数据
                    {
                        //if (reader.Name == "sv_total" || reader.Name == "rho" || reader.Name == "alpha" || reader.Name == "index")
                        //sw.WriteLine(reader.Name);
                        flag = true;
                    }
                }

                if (reader.Name == ""&& flag)
                {
                   // richTextBox1.Text = richTextBox1.Text + reader.Name + "\n";

                   // sw.WriteLine(reader.Name);

                    if (reader.HasValue)
                    {
                        richTextBox1.Text = richTextBox1.Text + reader.Value + "\n";
                         string uu= reader.Value.Replace("\r\n","");  
                       string []m = uu.Split(' ');

                       for (int i = 0; i < m.Length; i++)
                       {
                           if (m[i] != "\r\n" && m[i] != "")
                               sw.Write(m[i]+" ");    //保存到TXT 文件里面
                       }    
                        sw.WriteLine();
                        flag = false;
                    }
                }
                
            }
            sw.Close();
            sw.Dispose();
           
 
        }

        }
        } 



效果如下:

winform 效果

TXT 效果

XML 文件



--------------------------------------------------------------------------------------------------------------

下面是把不同的属性存在不同的文件里面

-------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Linq; 


namespace xml_duqu
{
    public partial class Form1 : Form
    {
        static string strPath ;
        static string[] total;
        static string[] total1;
        static string sv_count;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                strPath = open.FileName;
             
                test(strPath);
            }

        }

        private void test(string filename)
        {
            XElement xe = XElement.Load(strPath);//加载XML文件  
            //使用LINT从XML文件中查询信息  
            XmlTextReader reader = new XmlTextReader(filename);

            XmlDocument xml = new XmlDocument();
            //读取xml文件
            xml.Load(filename);  //你的xml地址
            
            richTextBox1.Text = "";

            FileStream fs = new FileStream("data_sv.data", FileMode.Create);//支持向量
            FileStream fs1 = new FileStream("data_alpha.data", FileMode.Create);//alpha 系数
            FileStream fs2 = new FileStream("data_index.data", FileMode.Create);//index索引
            FileStream fs3= new FileStream("data_rho.data", FileMode.Create);//rho偏置
            StreamWriter sw; 
            sw = new StreamWriter(fs, Encoding.Default);//

            StreamWriter sw1;
            sw1 = new StreamWriter(fs1, Encoding.Default);//

            StreamWriter sw2;
            sw2 = new StreamWriter(fs2, Encoding.Default);//


            StreamWriter sw3;
            sw3 = new StreamWriter(fs3, Encoding.Default);//

            bool flag = false;
            bool flag_sv_total=false;
            int count = 0;
            string name="";
           // int alpha = 0;
            //int index = 0;
            List<string> sv_c= new List<string>();
            List<string> sv_alpha = new List<string>();
            List<string> sv_index = new List<string>();
            while (reader.Read())
            {
                if (reader.Name != "")
                {
                    if (reader.Name == "sv_total"||reader.Name=="sv_count" || reader.Name == "_" || reader.Name == "rho" || reader.Name == "alpha" || reader.Name == "index")
                    {
                        name = reader.Name;
                        //if (reader.Name == "sv_total" || reader.Name == "rho" || reader.Name == "alpha" || reader.Name == "index")
                        //sw.WriteLine(reader.Name);
                        if (reader.Name == "sv_total"&&count==0)
                        {
                            count++;
                            flag_sv_total = true;   
                        }

                        flag = true;
                    }
                }

                if (reader.Name == ""&& flag)
                {
                   // richTextBox1.Text = richTextBox1.Text + reader.Name + "\n";

                   // sw.WriteLine(reader.Name);

                    if (reader.HasValue)
                    {
                        //richTextBox1.Text = richTextBox1.Text + reader.Value + "\n";
                        if (count == 1 && flag_sv_total)
                        {
                             //total = new string[Convert.ToInt32(reader.Value)]; //生成多少个支持向量的alpha
                             //total1 = new string[Convert.ToInt32(reader.Value)]; //生成多少个支持向量的alpha
                            count++;
                        }
                       string uu= reader.Value.Replace("\r\n","");
                       string []m = uu.Split(' ');

                       for (int i = 0; i < m.Length; i++)
                       {

                           if (m[i] != "\r\n" && m[i] != "")
                           {
                               if (name == "_")//支持向量的存入txt
                               {
                                   sw.Write(m[i] + " ");
                               }
                               if (name == "alpha")
                               {
                                   //total[alpha] = m[i];
                                  // sv_alpha.Add(m[i]);
                                   sw1.WriteLine(m[i]);
                                  // alpha++;
                               }
                               if (name == "index")
                               {
                                   //total1[index] = m[i];
                                  // sv_index.Add(m[i]);
                                   sw2.WriteLine(m[i]);
                                   //index++;
                               }
                               if (name == "rho")
                               {
                                   //sv_c.Add(m[i]);
                                   sw3.WriteLine(m[i]);
                                   
                               }
                               if (name == "sv_count")

                               {
                                   //sv_c.Add(m[i]);
                                   sw3.Write(m[i]+" ");
                               }


                           }
                           

                       }    
                        sw.WriteLine();
                        flag = false;
                    }
                }
                
            }
            sw.Close();
            sw.Dispose();
            sw1.Close();
            sw1.Dispose();
            sw2.Close();
            sw2.Dispose();
            sw3.Close();
            sw3.Dispose();
            fs.Dispose();
            fs1.Dispose();
            fs2.Dispose();
            fs3.Dispose();
        }


        
    }
}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值