コード例 #1
0
        private void bGo_Click(object sender, EventArgs e)
        {
            try
            {
                TeachingSample[] samples = null;

                if (rbText.Checked)
                {
                    MessageBox.Show("Please, choose text file to generate teaching samples");

                    OpenFileDialog dlg = new OpenFileDialog();
                    dlg.ShowDialog();

                    if (dlg.FileName != null && dlg.FileName != "")
                    {
                        samples = TeachingSample.generateTeachingSamplesFromFile(dlg.FileName);

                        if (samples == null)
                        {
                            throw new Exception();
                        }
                    }
                }
                else if (rbImage.Checked)
                {
                    MessageBox.Show("Please, choose images to generate teaching samples");

                    OpenFileDialog dlg = new OpenFileDialog();
                    dlg.Multiselect = true;
                    dlg.ShowDialog();
                    if (dlg.FileNames.Length > 0)
                    {
                        samples = new TeachingSample[dlg.FileNames.Length];
                        for (int i = 0; i < samples.Length; i++)
                        {
                            List <string[]> anns_info = ann_manager.getTableInfo();

                            int input_length  = 0;
                            int output_length = 0;
                            foreach (var info in anns_info)
                            {
                                if (info[0] == ann_manager.getSelectedANNName())
                                {
                                    input_length  = Convert.ToInt32(info[1]);
                                    output_length = Convert.ToInt32(info[3]);
                                    break;
                                }
                            }

                            samples[i] = new TeachingSample(input_length, output_length);
                            if (!samples[i].generateTeachingSampleFromImage(dlg.FileNames[i]))
                            {
                                MessageBox.Show("one of the files selected is bad");
                                return;
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("No files for teaching sample selected!");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("An odd situation occured. This is strange as f**k (also as div by 0), and program cant proceed...");
                    throw new Exception();
                }

                if (samples != null)
                {
                    double precision  = Convert.ToDouble(textBox2.Text);
                    int    iterations = Convert.ToInt32(textBox1.Text);

                    Close();

                    string teachingResults = ann_manager.teachANN(precision, samples, iterations);

                    MessageBox.Show("Teaching results: " + teachingResults);
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception) { MessageBox.Show("An error occured. Probably, you entered wrong values, or choose wrong file(s)"); };
        }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: Despairon/EZ_ANN
        private void recognizeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ann_manager.getSelectedANNName() != "None")
            {
                /* this is for debug purposes */

                Bitmap bitmap = new Bitmap(pbLetterImage.Image.GetThumbnailImage(16, 16, null, System.IntPtr.Zero));
                double[,] values = new double[bitmap.Width, bitmap.Height];
                for (int x = 0; x < bitmap.Width; x++)
                {
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        Color bitmapPixelColor = bitmap.GetPixel(x, y);
                        values[x, y] = (bitmapPixelColor.R == Color.White.R) &&
                                       (bitmapPixelColor.G == Color.White.G) &&
                                       (bitmapPixelColor.B == Color.White.B) ? 0 : 1;
                    }
                }
                double[] true_values = new double[values.Length];

                Buffer.BlockCopy(values, 0, true_values, 0, values.Length);

                double[] output = ann_manager.useANN(true_values);

                values = new double[bitmap.Width, bitmap.Height];

                int xx = 0;
                int yy = 0;
                foreach (var val in output)
                {
                    values[xx, yy] = val;
                    yy++;
                    if (yy == bitmap.Width)
                    {
                        xx++;
                        yy = 0;
                    }
                }
                Color c;
                for (int x = 0; x < bitmap.Width; x++)
                {
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        if (values[x, y] >= 0.5 && values[x, y] <= 0.6)
                        {
                            c = Color.LightGray;
                        }
                        else if (values[x, y] >= 0.6 && values[x, y] <= 0.7)
                        {
                            c = Color.SlateGray;
                        }
                        else if (values[x, y] >= 0.7 && values[x, y] <= 0.8)
                        {
                            c = Color.DarkSlateGray;
                        }
                        else if (values[x, y] >= 0.8 && values[x, y] <= 0.9)
                        {
                            c = Color.Gray;
                        }
                        else if (values[x, y] >= 0.9 && values[x, y] < 1)
                        {
                            c = Color.DarkGray;
                        }
                        else if (values[x, y] == 1)
                        {
                            c = Color.Black;
                        }
                        else
                        {
                            c = Color.White;
                        }
                        bitmap.SetPixel(x, y, c);
                    }
                }

                MessageBox.Show("select where to save recreated image");

                SaveFileDialog saveDlg = new SaveFileDialog();
                saveDlg.ShowDialog();
                try
                {
                    bitmap.Save(saveDlg.FileName);
                }
                catch (Exception) { }
                /* end of debug purposes */
            }
            else
            {
                MessageBox.Show("Select ANN from ANN manager first!");
            }
        }