/// <summary> /// Convert picture to vector /// </summary> /// <param name="picture">The picture.</param> /// <returns></returns> public sbyte[] ConvertToVector(PictureContainer picture) { vectorLength = picture.Size * picture.Size; var vector = new sbyte[vectorLength]; for (var i = 0; i < picture.Size; i++) { for (var j = 0; j < picture.Size; j++) { var pixel = picture.Picture.GetPixel(i, j); if (pixel.R > 250) { vector[i * picture.Size + j] = 1; } else { vector[i * picture.Size + j] = -1; } } } return vector; }
/// <summary> /// Generates the specified path. /// </summary> /// <param name="path">The path.</param> /// <param name="noiseLevel">The noise level.</param> /// <param name="pictureSize">Size of the picture.</param> /// <returns></returns> public static String Generate(String path, int noiseLevel, int pictureSize) { var noisePicturePath = CreateNewPath(); File.Copy(path, noisePicturePath); var picture = new PictureContainer(noisePicturePath, pictureSize); return Generate(picture, noiseLevel); }
/// <summary> /// Buttons the recognize click. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void ButtonRecognizeClick(object sender, EventArgs e) { if (helper == null) { return; } var neuronHelper = new NeuronHelper(); var picture = new PictureContainer(picturePath, 10); var vector = neuronHelper.ConvertToVector(picture); var percentage = helper.Recognize(vector); textBoxA.Text = Math.Abs(percentage[0]).ToString(CultureInfo.InvariantCulture); textBoxB.Text = Math.Abs(percentage[1]).ToString(CultureInfo.InvariantCulture); textBoxC.Text = Math.Abs(percentage[2]).ToString(CultureInfo.InvariantCulture); picture.Picture.Dispose(); }
/// <summary> /// Generates the specified picture. /// </summary> /// <param name="picture">The picture.</param> /// <param name="noiseLevel">The noise level.</param> /// <returns></returns> private static String Generate(PictureContainer picture, int noiseLevel) { int randomValue; Color pixel; for (var i = 0; i < picture.Size; i++) { for (var j = 0; j < picture.Size; j++) { pixel = picture.Picture.GetPixel(i, j); randomValue = Random.Next(0, 100); if (randomValue < noiseLevel) { picture.Picture.SetPixel(i, j, InvertPixel(pixel)); } } } var oldPicture = PicturePath; PicturePath = CreateNewPath(); picture.Picture.Save(PicturePath); picture.Picture.Dispose(); DeleteFile(oldPicture); return PicturePath; }
/// <summary> /// Buttons the statistics click. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void ButtonStatisticsClick(object sender, EventArgs e) { if (picturePath == null) { MessageBox.Show(Resources.ApplicationForm_ButtonStatisticsClick_Choose_picture_); return; } var helper = new NeuronHelper(); var picture = new PictureContainer(picturePath, 13); var vector = helper.ConvertToVector(picture); var network = new HopfieldNetwork(); var thread = new Thread(() => ChooseCorrectVector(network, vector), 214748364); thread.Start(); thread.Join(); picture.Picture.Dispose(); }
/// <summary> /// Buttons the teach click. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void ButtonTeachClick(object sender, EventArgs e) { var helper = new NeuronHelper(); var picture = new PictureContainer(PicturesPath.PathToOriginalA, 13); NeuronHelper.VectorA = helper.ConvertToVector(picture); var matrixA = helper.CreateMatrix(NeuronHelper.VectorA); picture = new PictureContainer(PicturesPath.PathToOriginalB, 13); NeuronHelper.VectorB = helper.ConvertToVector(picture); var matrixB = helper.CreateMatrix(NeuronHelper.VectorB); picture = new PictureContainer(PicturesPath.PathToOriginalC, 13); NeuronHelper.VectorC = helper.ConvertToVector(picture); var matrixC = helper.CreateMatrix(NeuronHelper.VectorC); HopfieldNetwork.MatrixW = helper.CreateCoefficientsMatrix(matrixA, matrixB, matrixC); textBoxStatistics.Text += @"Network has been taught." + Environment.NewLine; ScrollTextBox(); }