Ejemplo n.º 1
0
 // 特征提取
 private void fetureToolStripMenuItem_Click(object sender, EventArgs e)
 {
     // 首先 如果16个训练样本没进行选择的话
     pcxlist = SelectedPCXHelper.GetSelPCXFromLB();
     if (pcxlist == null || pcxlist.Count == 0)
     {
         MessageBox.Show(this, "还没选择训练样本,请按步骤来", "提示信息", MessageBoxButtons.OK);
     }
     else
     {
         OutlineFeatureForm ofform = new OutlineFeatureForm();
         ofform.ShowDialog();
     }
 }
Ejemplo n.º 2
0
        private void browerDirButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbdlg = new FolderBrowserDialog();

            fbdlg.Description         = "选择要进行计算的目录";
            fbdlg.RootFolder          = Environment.SpecialFolder.MyComputer;
            fbdlg.ShowNewFolderButton = true;
            DialogResult result = fbdlg.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                this.textDirPath.Text     = fbdlg.SelectedPath;
                SelectedPCXHelper.pcxlist = new List <PCXImage>(); //每次单击浏览的时候都初始化
                SelectedPCXHelper.SetSelectedPCXImage(this.textDirPath.Text);
                //this.selPcximage = SelectedPCXHelper.GetSelectedPCXImage(this.textDirPath.Text);
                LoadLB(this.textDirPath.Text); //加载ListBox的数据
            }
        }
Ejemplo n.º 3
0
        private void confirmButton_Click(object sender, EventArgs e)
        {
            if (selectedSampleListBox.Items.Count == 0)
            {
                MessageBox.Show(this, "你没有选择训练样本,请先选择", "提示信息", MessageBoxButtons.OK);
            }
            else
            {
                List <PCXImage> pcxList = new List <PCXImage>();
                int             count   = selectedSampleListBox.Items.Count;
                IList           list    = selectedSampleListBox.Items;
                List <string>   strlist = (List <string>)SelectedPCXHelper.ListConvert(list);
                foreach (string str in strlist)
                {
                    string   filename = string.Format(@"{0}", str);
                    Image    image    = PCXHelper.LoadPCX(filename);
                    PCXImage pcximage = PCXHelper.GetPCXImage(filename);
                    PCXImage pImage   = PCXHelper.GetPCXImage(pcximage);
                    pcxList.Add(pImage);
                }
                pcxList = pcxList.ToList();
                SelectedPCXHelper.SetSelPCXFromLB(pcxList, textDirPath.Text);

                List <PCXImage> unselpcxList = new List <PCXImage>();
                IList           unsellist    = unSelSampleListBox.Items;
                List <string>   unselstrlist = (List <string>)SelectedPCXHelper.ListConvert(unsellist);
                foreach (string str in unselstrlist)
                {
                    string   filename = string.Format(@"{0}", str);
                    Image    image    = PCXHelper.LoadPCX(filename);
                    PCXImage pcximage = PCXHelper.GetPCXImage(filename);
                    PCXImage pImage   = PCXHelper.GetPCXImage(pcximage);
                    unselpcxList.Add(pImage);
                }
                unselpcxList = unselpcxList.ToList();
                SelectedPCXHelper.SetUnselPCXList(unselpcxList, textDirPath.Text);
                this.Close();
            }
        }
Ejemplo n.º 4
0
        // 分类运算
        private void classifyButton_Click(object sender, EventArgs e)
        {
            //前提检验
            if (SelectedPCXHelper.GetSelPCXFromLB().Count == 0 || SelectedPCXHelper.GetUnselPCXList().Count == 0)
            {
                MessageBox.Show(this, "您还未提取样本特征,或者还未设置测试样本集!", "提示信息", MessageBoxButtons.OK);
            }

            else
            {
                string correctRate = null; //正确率
                string myfilepath  = filepathText.Text.ToString();

                Features feature;

                #region 这里做一个文件名为unknown.pcx的判断

                string filename = FeatureHelper.GetUnknownName(myfilepath);
                if (filename.ToLower().Equals("unknown"))
                {
                    feature = new Features(myfilepath);
                }
                else
                {
                    int classID = Convert.ToInt32(FeatureHelper.GetUpperFoldername(myfilepath));
                    feature = new Features(myfilepath, classID);
                }

                #endregion

                #region Bayes分类法

                if (rbBayes.Checked)
                {
                    #region 数据初始化

                    CheckInit();
                    double correctCount = 0.0;

                    #endregion

                    IList sampleList = FeatureHelper.GetFeaturesList();            //获取原始训练样本
                    //从降维器获取降维后的新样本
                    IList newSampleList = MDAHelper.GetMDSampleList();
                    MVHelper.SetSampleList((ArrayList)newSampleList);

                    Bayes bayes = Bayes.GetInstance();
                    bayes.TrainSampleList = newSampleList;                 //向贝叶斯分类器注入降维后的训练样本

                    //int classID = Convert.ToInt32(FeatureHelper.GetUpperFoldername(myfilepath));
                    //Features feature = new Features(myfilepath, classID);
                    feature = MDAHelper.MDSample(feature);             //测试样本降维
                    int testClassID = bayes.DecisionFunction(feature); //用贝叶斯决策进行测试样本分类
                    //结果显示
                    lblunknownclassify.Text = testClassID.ToString("000");
                    if (feature.classID == testClassID)
                    {
                        lblerrorinfo.Text      = "Bayes分类法分类正确";
                        lblerrorinfo.ForeColor = Color.Green;
                    }

                    //unknown.pcx处理
                    else if (feature.classID == -1)
                    {
                    }
                    else
                    {
                        lblerrorinfo.Text      = "Bayes分类法分类失败";
                        lblerrorinfo.ForeColor = Color.Green;
                    }
                }

                #endregion

                #region Kn近邻法

                if (rbKn.Checked)
                {
                    #region 相关数据初始化

                    CheckInit();
                    int    testResult   = -1;
                    double correctCount = 0.0;
                    int    kvalue       = Constant.kvalue;

                    #endregion

                    #region  效的情况下进行计算

                    if (KCheck(kvalue))
                    {
                        KnNear my_knearest = new KnNear();

                        //int classID = Convert.ToInt32(FeatureHelper.GetUpperFoldername(myfilepath));
                        //Features currfeature = new Features(myfilepath, classID);
                        testResult = my_knearest.DoK_nearest(feature, FeatureHelper.GetFeaturesList(), kvalue);

                        //testResult为K近邻的分类结果
                        // 其实testResult的结果直接就是result求的值
                        string result = testResult.ToString("000");
                        lblunknownclassify.Text = result;
                        result = ResultConvert(result);

                        int testID = Convert.ToInt32(result);
                        if (testID > 0 && testID == feature.classID)
                        {
                            //correctRate = "分类正确率: " + Constant.kn_Rate;
                            lblerrorinfo.Text      = "Kn近邻法分类正确";
                            lblerrorinfo.ForeColor = Color.Green;
                        }

                        //unknown.pcx处理
                        else if (feature.classID == -1)
                        {
                        }

                        else
                        {
                            lblerrorinfo.Text      = "Kn近邻法分类失败!";
                            lblerrorinfo.ForeColor = Color.Green;
                        }
                    }

                    #endregion
                }

                #endregion

                #region 最近邻法

                if (rbnearest.Checked)
                {
                    #region 初始化

                    CheckInit();
                    int    testResult   = -1;
                    double correctCount = 0.0;

                    #endregion

                    #region 最近邻分类

                    if (NearestCheck())
                    {
                        Nearest nearest = new Nearest();
                        //int classID = Convert.ToInt32(FeatureHelper.GetUpperFoldername(myfilepath));
                        //Features currfeature = new Features(myfilepath, classID);
                        testResult = nearest.Do_Nearest(feature, FeatureHelper.GetFeaturesList());

                        string result = testResult.ToString("000");
                        lblunknownclassify.Text = result;
                        if (testResult > 0 && testResult == feature.classID)
                        {
                            lblerrorinfo.Text      = "最近邻法分类正确";
                            lblerrorinfo.ForeColor = Color.Green;
                        }

                        //unknown.pcx处理
                        else if (feature.classID == -1)
                        {
                        }

                        else
                        {
                            lblerrorinfo.Text      = "最近邻法分类失败!";
                            lblerrorinfo.ForeColor = Color.Green;
                        }
                    }

                    #endregion
                }

                #endregion
            }
        }
Ejemplo n.º 5
0
        // 对测试样本和训练样本在ListBox中的初始化
        private void LoadLB(string path)
        {
            this.pcxlist = SelectedPCXHelper.GetPCXImageList(path);// 这是最初开始加载的pcxlist,不是移动变化后的
            selectedSampleListBox.Items.Clear();
            unSelSampleListBox.Items.Clear();
            // 在这里做一个radiobutton进行开闭测试的选择
            if (this.closeRadioButton.Checked)
            {
                Constant.openchecked = false;
                foreach (PCXImage pcx in pcxlist)
                {
                    selectedSampleListBox.Items.Add(pcx.OldFilename);
                    unSelSampleListBox.Items.Add(pcx.OldFilename);
                }
            }
            else
            {
                Constant.openchecked = true;
                foreach (PCXImage pcx in pcxlist)
                {
                    selectedSampleListBox.Items.Add(pcx.OldFilename);
                }
                Random ran     = new Random();
                int    count   = pcxlist.Count;
                int    testnum = (int)(count * Constant.testRate);

                #region 随机在每个类里面选择80%做训练样本,20%做测试样本

                #region 循环里面最好不要进行数值的初始化

                int p = count;
                int k = 0;
                int m = k + 20;

                #endregion

                for (int i = 0; i < testnum; i++)
                {
                    // 注意题意是:对每个人的手写签名,用其中80%的图像作为训练样本进行训练,用余下的20%的图像进行测试,所以这里的处理有个技巧性。
                    //int _index = ran.Next(0, count); //产生0到pcx总数的随机数,且count的数必须每次循环后减一,即count--。

                    int _index = ran.Next(k, m);

                    #region 数据移除
                    // 如果从训练样本中已经移除,则会出现_index大于剩余训练样本总和的情况,会报错。注意添加删除的顺序
                    //if (_index >= selectedSampleListBox.Items.Count)
                    //{
                    //    int number = selectedSampleListBox.Items.Count - 2;
                    //    selectedSampleListBox.Items.RemoveAt(number); //At 15
                    //    unSelSampleListBox.Items.Add(selectedSampleListBox.Items[number - 1]); //添加的是 14
                    //}
                    //else
                    //{
                    //    selectedSampleListBox.Items.RemoveAt(_index);
                    //    unSelSampleListBox.Items.Add(selectedSampleListBox.Items[_index - 1]);
                    //}
                    #endregion

                    int number = selectedSampleListBox.Items.Count;
                    // 顺序不能乱 先添加后删除
                    unSelSampleListBox.Items.Add(selectedSampleListBox.Items[_index]);
                    selectedSampleListBox.Items.RemoveAt(_index);
                    //count--;

                    // 20%做测试样本,80%做训练样本
                    if (selectedSampleListBox.Items.Count == p - 4) //说明移除四个
                    {
                        k += 16;                                    //20-4
                        p -= 4;
                        m  = k + 20;
                    }
                    else
                    {
                        m--;
                    }
                }

                #endregion
            }
        }