Guest User

Untitled

a guest
Jan 12th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.46 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. using System.Windows.Media.Imaging;
  8. using System.Windows.Shapes;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Collections.Generic;
  12. using System.Threading;
  13.  
  14. namespace Asocjator
  15. {
  16.     /// <summary>
  17.     /// Interaction logic for MainWindow.xaml
  18.     /// </summary>
  19.     ///
  20.     public static class ArrayExtensions
  21.     {
  22.         public static IEnumerable<T> GetRow<T>(this T[,] items, int row)
  23.         {
  24.             for (var i = 0; i < items.GetLength(1); i++)
  25.             {
  26.                 yield return items[row, i];
  27.             }
  28.         }
  29.     }
  30.  
  31.  
  32.     public static class Extensions
  33.     {
  34.  
  35.         public static byte[] ToByteArray(this BitmapSource bitmapSource)
  36.         {
  37.  
  38.             var encoder = new JpegBitmapEncoder();
  39.             encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
  40.  
  41.             using (var stream = new MemoryStream())
  42.             {
  43.                 encoder.Save(stream);
  44.                 return stream.ToArray();
  45.             }
  46.         }
  47.  
  48.         public static BitmapSource ToBitmapSource(this byte[] bytes)
  49.         {
  50.  
  51.             using (var stream = new MemoryStream(bytes))
  52.             {
  53.                 var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default);
  54.                 return decoder.Frames.First();
  55.             }
  56.         }
  57.     }
  58.  
  59.  
  60.     public partial class MainWindow : Window
  61.     {
  62.  
  63.  
  64.         System.Windows.Point currentPoint = new System.Windows.Point();
  65.         List<WriteableBitmap> images = new List<WriteableBitmap>();
  66.         byte[,] pixels;
  67.         Random rand = new Random();
  68.         static List<BitmapImage> imagelist = new List<BitmapImage>(5) {
  69.                 (new BitmapImage(new Uri(@"c:\\obrazki\\obrazek1.jpg"))),
  70.                 (new BitmapImage(new Uri(@"c:\\obrazki\\obrazek2.jpg"))),
  71.                 (new BitmapImage(new Uri(@"c:\\obrazki\\obrazek3.jpg"))),
  72.                 (new BitmapImage(new Uri(@"c:\\obrazki\\obrazek4.jpg"))),
  73.                 (new BitmapImage(new Uri(@"c:\\obrazki\\obrazek5.jpg")))
  74.         };
  75.         static List<BitmapImage> imagelist2 = new List<BitmapImage>(5) {
  76.                 (new BitmapImage(new Uri(@"c:\\obrazek1.jpg"))),
  77.                 (new BitmapImage(new Uri(@"c:\\obrazek2.jpg"))),
  78.                 (new BitmapImage(new Uri(@"c:\\obrazek3.jpg"))),
  79.                 (new BitmapImage(new Uri(@"c:\\obrazek4.jpg"))),
  80.                 (new BitmapImage(new Uri(@"c:\\obrazek5.jpg")))
  81.         };
  82.         static List<WriteableBitmap> _bitmap = new List<WriteableBitmap>(5);
  83.  
  84.  
  85.         static List<WriteableBitmap> _bitmap1 = new List<WriteableBitmap>(5);
  86.         //{
  87.         //        new WriteableBitmap(imagelist[0]),
  88.         //        new WriteableBitmap(imagelist[1]),
  89.         //        new WriteableBitmap(imagelist[2]),
  90.         //        new WriteableBitmap(imagelist[3]),
  91.         //        new WriteableBitmap(imagelist[4])
  92.         //};
  93.  
  94.         int arraySize;
  95.         static int bok = 100;
  96.         int stride;
  97.         static int NUMBER_OF_ITERATIONS = 500;
  98.         static int NUMBER_OF_EXAMPLES = 50;
  99.         List<byte[]> zaburzone = new List<byte[]>(NUMBER_OF_EXAMPLES);
  100.  
  101.         static Perceptron.Perceptro[] p = new Perceptron.Perceptro[bok*bok];
  102.         static Random rnd = new Random();
  103.              
  104.         public MainWindow()
  105.         {
  106.             InitializeComponent();
  107.             //paintSurface.Children.Add(new Image() { Source = new BitmapImage(new Uri(@"c:\\obrazek1.jpg")) });
  108.  
  109.             images = Load();
  110.             int r = rand.Next(0,5);
  111.  
  112.             for(int i = 0; i < NUMBER_OF_EXAMPLES; i++)
  113.             {
  114.                 zaburzone.Add(zaburz_obrazek(pixels.GetRow(i%5).ToArray()));
  115.                 //Debug.WriteLine("Tworze przyklady do nauki... [{0}/{1}]", i + 1, NUMBER_OF_EXAMPLES);
  116.             }
  117.  
  118.             for (int i = 0; i < bok*bok; i++)
  119.             {
  120.                 p[i].losuj(bok*bok);
  121.                 if (i % 1000 == 0)
  122.                 {
  123.                     Debug.WriteLine("Tworze perceptrony... [{0}/{1}]", i+1, bok*bok);
  124.  
  125.                 }
  126.             }
  127.  
  128.             Debug.WriteLine("Ucze...");
  129.  
  130.             new Thread(() =>
  131.             {
  132.                 for (int i = 0; i < NUMBER_OF_ITERATIONS; ++i)
  133.                 {
  134.                     r = rand.Next(NUMBER_OF_EXAMPLES);
  135.                     for (int j = 0; j < bok * bok; j++)
  136.                     {
  137.                         int wyjscie = p[j].wyjscie(zaburzone[r]);
  138.                         if (wyjscie != ((zaburzone[r][j] == 1) ? 1 : -1))
  139.                         {
  140.                             p[j].przegeneruj(zaburzone[r], ((zaburzone[r][j] == 1) ? 1 : -1));
  141.                         }
  142.                     }
  143.                     Debug.WriteLine("Ucze... [{0}/{1}]", i + 1, NUMBER_OF_ITERATIONS);
  144.                     // System.Console.WriteLine("Ucze... [{0}/{1}]", i + 1, NUMBER_OF_ITERATIONS);
  145.  
  146.  
  147.  
  148.  
  149.  
  150.                 }
  151.                 for (int j = 0; j < bok * bok; j++)
  152.                 {
  153.                     p[j].koniec_nauki();
  154.                 }
  155.                 for (int j = 0; j < bok * bok; j++)
  156.                 {
  157.                     Debug.WriteLine(p[j].najlepszy_wiek);
  158.                 }
  159.             }).Start();
  160.  
  161.  
  162.  
  163.             //img.Source = _bitmap;
  164.             Image img = new Image();
  165.             img.Source = images[r];
  166.             //img.Source = imagelist[4];
  167.             paintSurface.Children.Add(img);
  168.  
  169.             Debug.WriteLine("nope");
  170.         }
  171.  
  172.         static byte[] zaburz_obrazek(byte[] obrazek)
  173.         {
  174.             byte[] ret = new byte[bok*bok];
  175.             for (int i = 0; i < bok*bok; ++i)
  176.             {
  177.                 if (rnd.Next(Int32.MaxValue) < Int32.MaxValue / 10)
  178.                     ret[i] = (obrazek[i]) == 1 ? (byte)0 : (byte)1;
  179.                 else ret[i] = obrazek[i];
  180.             }
  181.             return ret;
  182.         }
  183.  
  184.  
  185.         public List<WriteableBitmap> Load()
  186.         {
  187.             // BitmapImage image = new BitmapImage(new Uri(@"c:\\czarno-biale-tkaniny-4.jpg"));
  188.  
  189.             pixels = new byte[imagelist.Count, bok*bok];
  190.             //BitmapImage image = new BitmapImage(new Uri(@"c:\\small_frank.jpg"));
  191.             for (int i = 0; i < imagelist.Count; ++i) {
  192.                 int width = (int)imagelist[i].Width;
  193.                 //int width = 300;
  194.                 Debug.WriteLine("Width: {0}", width);
  195.  
  196.                 int height = (int)imagelist[i].Height;
  197.                 //int height = 300;
  198.                 Debug.WriteLine("Height: {0}", height);
  199.                 _bitmap.Add(new WriteableBitmap(imagelist[i]));
  200.                 _bitmap[i] = new WriteableBitmap(imagelist[i]);
  201.  
  202.                 stride = height * ((_bitmap[i].Format.BitsPerPixel + 7) / 8);
  203.                 Debug.WriteLine("Stride: {0}", stride);
  204.                 arraySize = stride * height;
  205.                 Debug.WriteLine("ArraySize: {0}", arraySize);
  206.                 byte[] pixels1 = new byte[arraySize];
  207.                 byte[] pixels2 = new byte[40000];
  208.                 _bitmap[i].CopyPixels(pixels2, height * ((_bitmap[i].Format.BitsPerPixel + 7) / 8), 0);
  209.                 int j = 0;
  210.                 int s = 0;
  211.                 for (int k = 0; k < pixels2.Length; k = k+4)
  212.                 {
  213.                    
  214.                     if (pixels2[k] < 127 || pixels2[k + 1] < 127 || pixels2[k + 2] < 127)
  215.                     {
  216.  
  217.  
  218.                         pixels1[k] = 0;
  219.                         pixels1[k + 1] = 0;
  220.                         pixels1[k + 2] = 0;
  221.                         pixels1[k + 3] = 255;
  222.  
  223.                         pixels[i, s] = 0;
  224.                     }
  225.                     else
  226.                     {
  227.                         pixels1[k] = 255;
  228.                         pixels1[k + 1] = 255;
  229.                         pixels1[k + 2] = 255;
  230.                        
  231.                         pixels[i, s] = 1;
  232.                     }
  233.  
  234.                     s++;
  235.                     if(s >= 10000)
  236.                     {
  237.                         Debug.Write("S WIEKSZE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  238.                     }
  239.                     //if (j == 1200)
  240.                     //    j = 3600;
  241.                 }
  242.  
  243.                 try
  244.                 {
  245.  
  246.                     _bitmap[i].WritePixels(new Int32Rect(0, 0, width, height), pixels1, stride, 0);
  247.                 }
  248.                 catch (ArgumentNullException e)
  249.                 {
  250.                     if (e.Source != null)
  251.                         Console.WriteLine("IOException source: {0}, Message: {1}, Source: {2}", e.Source, e.Message, e.ParamName);
  252.                     throw;
  253.                 }
  254.             }
  255.             return _bitmap;
  256.         }
  257.  
  258.         public WriteableBitmap PutPixels(byte[] pixels)
  259.         {
  260.             WriteableBitmap temp = new WriteableBitmap(SaveAsWriteableBitmap(paintSurface));
  261.             byte[] pixels1 = new byte[arraySize];
  262.             int j = 0;
  263.             for (int i = 0; i < pixels.Length; i++)
  264.             {
  265.  
  266.  
  267.                 if (pixels[i] == 0)
  268.                 {
  269.                     pixels1[j] = 0;
  270.                     pixels1[j + 1] = 0;
  271.                     pixels1[j + 2] = 0;
  272.                     pixels1[j + 3] = 255;
  273.                    
  274.  
  275.                 }
  276.                 else
  277.                 {
  278.                     pixels1[j] = 255;
  279.                     pixels1[j + 1] = 255;
  280.                     pixels1[j + 2] = 255;
  281.                     pixels1[j + 3] = 255;
  282.                    
  283.                 }
  284.                 j += 4;
  285.                 // Debug.WriteLine("{0}, {1}, {2}, {3}", pixels[j], pixels[j + 1], pixels[j + 2], pixels[j + 3]);
  286.  
  287.             }
  288.  
  289.             temp.WritePixels(new Int32Rect(0, 0, 100, 100), pixels1, stride, 0);
  290.  
  291.             return temp;
  292.         }
  293.  
  294.         public byte[] GetPixels()
  295.         {
  296.             WriteableBitmap temp = new WriteableBitmap(SaveAsWriteableBitmap(paintSurface));
  297.             byte[] pix = new byte[arraySize];
  298.             byte[] temppix = new byte[bok*bok];
  299.  
  300.             temp.CopyPixels(pix, stride, 0);
  301.  
  302.             temp.WritePixels(new Int32Rect(0, 0, 100, 100), pix, stride, 0);
  303.             int a = 0;
  304.             for (int i = 0; i < pix.Length / 4; ++i)
  305.             {
  306.                 //Debug.WriteLine("{0}, {1}, {2}, {3}", pix[a], pix[a + 1], pix[a + 2], pix[a + 3]);
  307.  
  308.                 if (pix[a] < 127 )
  309.                 {
  310.  
  311.                     temppix[i] = 0;
  312.                 }
  313.                 else
  314.                 {
  315.  
  316.                     temppix[i] = 1;
  317.                 }
  318.  
  319.                 a += 4;
  320.  
  321.             }
  322.  
  323.             //Image img = new Image();
  324.             //img.Source = temp;
  325.             //img.Source = imagelist[4];
  326.             //paintSurface.Children.Add(img);
  327.  
  328.             return temppix;
  329.            
  330.         }
  331.  
  332.         private static BitmapImage LoadImage(byte[] imageData)
  333.         {
  334.             if (imageData == null || imageData.Length == 0) return null;
  335.             var image = new BitmapImage();
  336.             using (var mem = new MemoryStream(imageData))
  337.             {
  338.                 mem.Position = 0;
  339.                 image.BeginInit();
  340.                 image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
  341.                 image.CacheOption = BitmapCacheOption.OnLoad;
  342.                 image.UriSource = null;
  343.                 image.StreamSource = mem;
  344.                 image.EndInit();
  345.             }
  346.             image.Freeze();
  347.             return image;
  348.         }
  349.         private void paintSurface_MouseDown(object sender, MouseButtonEventArgs e)
  350.         {
  351.             if (e.ButtonState == MouseButtonState.Pressed)
  352.                 currentPoint = e.GetPosition(this);
  353.         }
  354.  
  355.         private void paintSurface_MouseMove(object sender, MouseEventArgs e)
  356.  
  357.            
  358.         {
  359.             Debug.WriteLine("(x,y) = ({0},{1})", currentPoint.X, currentPoint.Y);
  360.             if (e.LeftButton == MouseButtonState.Pressed)
  361.             {
  362.                 currentPoint = e.GetPosition(this);
  363.                 SolidColorBrush whiteBrush = new SolidColorBrush();
  364.                 whiteBrush.Color = Colors.White;
  365.                
  366.                 Line line = new Line();
  367.                 line.StrokeThickness = 5;
  368.                
  369.  
  370.                 line.Stroke = whiteBrush;
  371.                 line.X1 = currentPoint.X;
  372.                 line.Y1 = currentPoint.Y;
  373.                
  374.                 line.X2 = e.GetPosition(this).X;
  375.                 line.Y2 = e.GetPosition(this).Y;
  376.  
  377.                 currentPoint = e.GetPosition(this);
  378.  
  379.                 paintSurface.Children.Add(line);
  380.  
  381.             }
  382.         }
  383.  
  384.         private void Losuj_Click(object sender, RoutedEventArgs e)
  385.         {
  386.             //ExportToPng(new Uri("C:\\Users\\dawid\\source\\repos\\Asocjator\\obrazek.bmp"), paintSurface);
  387.             int r = rand.Next(0, 5);
  388.             Image img = new Image();
  389.             img.Source = images[r];
  390.             //img.Source = imagelist[4];
  391.             paintSurface.Children.Add(img);
  392.         }
  393.  
  394.         private void oczysc_Click(object sender, RoutedEventArgs e)
  395.         {
  396.  
  397.             WriteableBitmap temp;
  398.             byte[] img = new byte[bok * bok];
  399.             img = GetPixels();
  400.             byte[] img_new = new byte[bok*bok];
  401.  
  402.             for(int i = 0; i < bok*bok; i++)
  403.             {
  404.                 int wy = p[i].wyjscie(img);
  405.                 img_new[i] = (wy == 1 ? (byte)1 : (byte)0);
  406.                 if (i % 100 == 0) ;
  407.                     //Debug.WriteLine("\rOczyszczam... [{0}/{1}]", i, bok * bok);
  408.             }
  409.  
  410.             temp = new WriteableBitmap(PutPixels(img_new));
  411.             Image img1 = new Image();
  412.             img1.Source = temp;
  413.             //img.Source = imagelist[4];
  414.             paintSurface.Children.Add(img1);
  415.  
  416.         }
  417.  
  418.         public WriteableBitmap SaveAsWriteableBitmap(Canvas surface)
  419.         {
  420.             if (surface == null) return null;
  421.  
  422.             // Save current canvas transform
  423.             Transform transform = surface.LayoutTransform;
  424.             // reset current transform (in case it is scaled or rotated)
  425.             surface.LayoutTransform = null;
  426.  
  427.             // Get the size of canvas
  428.             Size size = new Size(surface.ActualWidth, surface.ActualHeight);
  429.             // Measure and arrange the surface
  430.             // VERY IMPORTANT
  431.             surface.Measure(size);
  432.             surface.Arrange(new Rect(size));
  433.  
  434.             // Create a render bitmap and push the surface to it
  435.             RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
  436.               (int)size.Width,
  437.               (int)size.Height,
  438.               96d,
  439.               96d,
  440.               PixelFormats.Pbgra32);
  441.             renderBitmap.Render(surface);
  442.  
  443.  
  444.             //Restore previously saved layout
  445.             surface.LayoutTransform = transform;
  446.  
  447.             //create and return a new WriteableBitmap using the RenderTargetBitmap
  448.             return new WriteableBitmap(renderBitmap);
  449.  
  450.         }
  451.  
  452.         private void zaszum_Click(object sender, RoutedEventArgs e)
  453.         {
  454.             WriteableBitmap temp;
  455.             byte[] img = new byte[bok * bok];
  456.             img = GetPixels();
  457.  
  458.             img = zaburz_obrazek(img);
  459.  
  460.             temp = new WriteableBitmap(PutPixels(img));
  461.             Image img1 = new Image();
  462.             img1.Source = temp;
  463.             //img.Source = imagelist[4];
  464.             paintSurface.Children.Add(img1);
  465.  
  466.         }
  467.     }
  468.  
  469.  
  470.  
  471. }
Add Comment
Please, Sign In to add comment