Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using System.IO;
- using System.Linq;
- using System.Collections.Generic;
- using System.Threading;
- namespace Asocjator
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- ///
- public static class ArrayExtensions
- {
- public static IEnumerable<T> GetRow<T>(this T[,] items, int row)
- {
- for (var i = 0; i < items.GetLength(1); i++)
- {
- yield return items[row, i];
- }
- }
- }
- public static class Extensions
- {
- public static byte[] ToByteArray(this BitmapSource bitmapSource)
- {
- var encoder = new JpegBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
- using (var stream = new MemoryStream())
- {
- encoder.Save(stream);
- return stream.ToArray();
- }
- }
- public static BitmapSource ToBitmapSource(this byte[] bytes)
- {
- using (var stream = new MemoryStream(bytes))
- {
- var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default);
- return decoder.Frames.First();
- }
- }
- }
- public partial class MainWindow : Window
- {
- System.Windows.Point currentPoint = new System.Windows.Point();
- List<WriteableBitmap> images = new List<WriteableBitmap>();
- byte[,] pixels;
- Random rand = new Random();
- static List<BitmapImage> imagelist = new List<BitmapImage>(5) {
- (new BitmapImage(new Uri(@"c:\\obrazki\\obrazek1.jpg"))),
- (new BitmapImage(new Uri(@"c:\\obrazki\\obrazek2.jpg"))),
- (new BitmapImage(new Uri(@"c:\\obrazki\\obrazek3.jpg"))),
- (new BitmapImage(new Uri(@"c:\\obrazki\\obrazek4.jpg"))),
- (new BitmapImage(new Uri(@"c:\\obrazki\\obrazek5.jpg")))
- };
- static List<BitmapImage> imagelist2 = new List<BitmapImage>(5) {
- (new BitmapImage(new Uri(@"c:\\obrazek1.jpg"))),
- (new BitmapImage(new Uri(@"c:\\obrazek2.jpg"))),
- (new BitmapImage(new Uri(@"c:\\obrazek3.jpg"))),
- (new BitmapImage(new Uri(@"c:\\obrazek4.jpg"))),
- (new BitmapImage(new Uri(@"c:\\obrazek5.jpg")))
- };
- static List<WriteableBitmap> _bitmap = new List<WriteableBitmap>(5);
- static List<WriteableBitmap> _bitmap1 = new List<WriteableBitmap>(5);
- //{
- // new WriteableBitmap(imagelist[0]),
- // new WriteableBitmap(imagelist[1]),
- // new WriteableBitmap(imagelist[2]),
- // new WriteableBitmap(imagelist[3]),
- // new WriteableBitmap(imagelist[4])
- //};
- int arraySize;
- static int bok = 100;
- int stride;
- static int NUMBER_OF_ITERATIONS = 500;
- static int NUMBER_OF_EXAMPLES = 50;
- List<byte[]> zaburzone = new List<byte[]>(NUMBER_OF_EXAMPLES);
- static Perceptron.Perceptro[] p = new Perceptron.Perceptro[bok*bok];
- static Random rnd = new Random();
- public MainWindow()
- {
- InitializeComponent();
- //paintSurface.Children.Add(new Image() { Source = new BitmapImage(new Uri(@"c:\\obrazek1.jpg")) });
- images = Load();
- int r = rand.Next(0,5);
- for(int i = 0; i < NUMBER_OF_EXAMPLES; i++)
- {
- zaburzone.Add(zaburz_obrazek(pixels.GetRow(i%5).ToArray()));
- //Debug.WriteLine("Tworze przyklady do nauki... [{0}/{1}]", i + 1, NUMBER_OF_EXAMPLES);
- }
- for (int i = 0; i < bok*bok; i++)
- {
- p[i].losuj(bok*bok);
- if (i % 1000 == 0)
- {
- Debug.WriteLine("Tworze perceptrony... [{0}/{1}]", i+1, bok*bok);
- }
- }
- Debug.WriteLine("Ucze...");
- new Thread(() =>
- {
- for (int i = 0; i < NUMBER_OF_ITERATIONS; ++i)
- {
- r = rand.Next(NUMBER_OF_EXAMPLES);
- for (int j = 0; j < bok * bok; j++)
- {
- int wyjscie = p[j].wyjscie(zaburzone[r]);
- if (wyjscie != ((zaburzone[r][j] == 1) ? 1 : -1))
- {
- p[j].przegeneruj(zaburzone[r], ((zaburzone[r][j] == 1) ? 1 : -1));
- }
- }
- Debug.WriteLine("Ucze... [{0}/{1}]", i + 1, NUMBER_OF_ITERATIONS);
- // System.Console.WriteLine("Ucze... [{0}/{1}]", i + 1, NUMBER_OF_ITERATIONS);
- }
- for (int j = 0; j < bok * bok; j++)
- {
- p[j].koniec_nauki();
- }
- for (int j = 0; j < bok * bok; j++)
- {
- Debug.WriteLine(p[j].najlepszy_wiek);
- }
- }).Start();
- //img.Source = _bitmap;
- Image img = new Image();
- img.Source = images[r];
- //img.Source = imagelist[4];
- paintSurface.Children.Add(img);
- Debug.WriteLine("nope");
- }
- static byte[] zaburz_obrazek(byte[] obrazek)
- {
- byte[] ret = new byte[bok*bok];
- for (int i = 0; i < bok*bok; ++i)
- {
- if (rnd.Next(Int32.MaxValue) < Int32.MaxValue / 10)
- ret[i] = (obrazek[i]) == 1 ? (byte)0 : (byte)1;
- else ret[i] = obrazek[i];
- }
- return ret;
- }
- public List<WriteableBitmap> Load()
- {
- // BitmapImage image = new BitmapImage(new Uri(@"c:\\czarno-biale-tkaniny-4.jpg"));
- pixels = new byte[imagelist.Count, bok*bok];
- //BitmapImage image = new BitmapImage(new Uri(@"c:\\small_frank.jpg"));
- for (int i = 0; i < imagelist.Count; ++i) {
- int width = (int)imagelist[i].Width;
- //int width = 300;
- Debug.WriteLine("Width: {0}", width);
- int height = (int)imagelist[i].Height;
- //int height = 300;
- Debug.WriteLine("Height: {0}", height);
- _bitmap.Add(new WriteableBitmap(imagelist[i]));
- _bitmap[i] = new WriteableBitmap(imagelist[i]);
- stride = height * ((_bitmap[i].Format.BitsPerPixel + 7) / 8);
- Debug.WriteLine("Stride: {0}", stride);
- arraySize = stride * height;
- Debug.WriteLine("ArraySize: {0}", arraySize);
- byte[] pixels1 = new byte[arraySize];
- byte[] pixels2 = new byte[40000];
- _bitmap[i].CopyPixels(pixels2, height * ((_bitmap[i].Format.BitsPerPixel + 7) / 8), 0);
- int j = 0;
- int s = 0;
- for (int k = 0; k < pixels2.Length; k = k+4)
- {
- if (pixels2[k] < 127 || pixels2[k + 1] < 127 || pixels2[k + 2] < 127)
- {
- pixels1[k] = 0;
- pixels1[k + 1] = 0;
- pixels1[k + 2] = 0;
- pixels1[k + 3] = 255;
- pixels[i, s] = 0;
- }
- else
- {
- pixels1[k] = 255;
- pixels1[k + 1] = 255;
- pixels1[k + 2] = 255;
- pixels[i, s] = 1;
- }
- s++;
- if(s >= 10000)
- {
- Debug.Write("S WIEKSZE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
- }
- //if (j == 1200)
- // j = 3600;
- }
- try
- {
- _bitmap[i].WritePixels(new Int32Rect(0, 0, width, height), pixels1, stride, 0);
- }
- catch (ArgumentNullException e)
- {
- if (e.Source != null)
- Console.WriteLine("IOException source: {0}, Message: {1}, Source: {2}", e.Source, e.Message, e.ParamName);
- throw;
- }
- }
- return _bitmap;
- }
- public WriteableBitmap PutPixels(byte[] pixels)
- {
- WriteableBitmap temp = new WriteableBitmap(SaveAsWriteableBitmap(paintSurface));
- byte[] pixels1 = new byte[arraySize];
- int j = 0;
- for (int i = 0; i < pixels.Length; i++)
- {
- if (pixels[i] == 0)
- {
- pixels1[j] = 0;
- pixels1[j + 1] = 0;
- pixels1[j + 2] = 0;
- pixels1[j + 3] = 255;
- }
- else
- {
- pixels1[j] = 255;
- pixels1[j + 1] = 255;
- pixels1[j + 2] = 255;
- pixels1[j + 3] = 255;
- }
- j += 4;
- // Debug.WriteLine("{0}, {1}, {2}, {3}", pixels[j], pixels[j + 1], pixels[j + 2], pixels[j + 3]);
- }
- temp.WritePixels(new Int32Rect(0, 0, 100, 100), pixels1, stride, 0);
- return temp;
- }
- public byte[] GetPixels()
- {
- WriteableBitmap temp = new WriteableBitmap(SaveAsWriteableBitmap(paintSurface));
- byte[] pix = new byte[arraySize];
- byte[] temppix = new byte[bok*bok];
- temp.CopyPixels(pix, stride, 0);
- temp.WritePixels(new Int32Rect(0, 0, 100, 100), pix, stride, 0);
- int a = 0;
- for (int i = 0; i < pix.Length / 4; ++i)
- {
- //Debug.WriteLine("{0}, {1}, {2}, {3}", pix[a], pix[a + 1], pix[a + 2], pix[a + 3]);
- if (pix[a] < 127 )
- {
- temppix[i] = 0;
- }
- else
- {
- temppix[i] = 1;
- }
- a += 4;
- }
- //Image img = new Image();
- //img.Source = temp;
- //img.Source = imagelist[4];
- //paintSurface.Children.Add(img);
- return temppix;
- }
- private static BitmapImage LoadImage(byte[] imageData)
- {
- if (imageData == null || imageData.Length == 0) return null;
- var image = new BitmapImage();
- using (var mem = new MemoryStream(imageData))
- {
- mem.Position = 0;
- image.BeginInit();
- image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
- image.CacheOption = BitmapCacheOption.OnLoad;
- image.UriSource = null;
- image.StreamSource = mem;
- image.EndInit();
- }
- image.Freeze();
- return image;
- }
- private void paintSurface_MouseDown(object sender, MouseButtonEventArgs e)
- {
- if (e.ButtonState == MouseButtonState.Pressed)
- currentPoint = e.GetPosition(this);
- }
- private void paintSurface_MouseMove(object sender, MouseEventArgs e)
- {
- Debug.WriteLine("(x,y) = ({0},{1})", currentPoint.X, currentPoint.Y);
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- currentPoint = e.GetPosition(this);
- SolidColorBrush whiteBrush = new SolidColorBrush();
- whiteBrush.Color = Colors.White;
- Line line = new Line();
- line.StrokeThickness = 5;
- line.Stroke = whiteBrush;
- line.X1 = currentPoint.X;
- line.Y1 = currentPoint.Y;
- line.X2 = e.GetPosition(this).X;
- line.Y2 = e.GetPosition(this).Y;
- currentPoint = e.GetPosition(this);
- paintSurface.Children.Add(line);
- }
- }
- private void Losuj_Click(object sender, RoutedEventArgs e)
- {
- //ExportToPng(new Uri("C:\\Users\\dawid\\source\\repos\\Asocjator\\obrazek.bmp"), paintSurface);
- int r = rand.Next(0, 5);
- Image img = new Image();
- img.Source = images[r];
- //img.Source = imagelist[4];
- paintSurface.Children.Add(img);
- }
- private void oczysc_Click(object sender, RoutedEventArgs e)
- {
- WriteableBitmap temp;
- byte[] img = new byte[bok * bok];
- img = GetPixels();
- byte[] img_new = new byte[bok*bok];
- for(int i = 0; i < bok*bok; i++)
- {
- int wy = p[i].wyjscie(img);
- img_new[i] = (wy == 1 ? (byte)1 : (byte)0);
- if (i % 100 == 0) ;
- //Debug.WriteLine("\rOczyszczam... [{0}/{1}]", i, bok * bok);
- }
- temp = new WriteableBitmap(PutPixels(img_new));
- Image img1 = new Image();
- img1.Source = temp;
- //img.Source = imagelist[4];
- paintSurface.Children.Add(img1);
- }
- public WriteableBitmap SaveAsWriteableBitmap(Canvas surface)
- {
- if (surface == null) return null;
- // Save current canvas transform
- Transform transform = surface.LayoutTransform;
- // reset current transform (in case it is scaled or rotated)
- surface.LayoutTransform = null;
- // Get the size of canvas
- Size size = new Size(surface.ActualWidth, surface.ActualHeight);
- // Measure and arrange the surface
- // VERY IMPORTANT
- surface.Measure(size);
- surface.Arrange(new Rect(size));
- // Create a render bitmap and push the surface to it
- RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
- (int)size.Width,
- (int)size.Height,
- 96d,
- 96d,
- PixelFormats.Pbgra32);
- renderBitmap.Render(surface);
- //Restore previously saved layout
- surface.LayoutTransform = transform;
- //create and return a new WriteableBitmap using the RenderTargetBitmap
- return new WriteableBitmap(renderBitmap);
- }
- private void zaszum_Click(object sender, RoutedEventArgs e)
- {
- WriteableBitmap temp;
- byte[] img = new byte[bok * bok];
- img = GetPixels();
- img = zaburz_obrazek(img);
- temp = new WriteableBitmap(PutPixels(img));
- Image img1 = new Image();
- img1.Source = temp;
- //img.Source = imagelist[4];
- paintSurface.Children.Add(img1);
- }
- }
- }
Add Comment
Please, Sign In to add comment