Example #1
0
        // click on processing buttons
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string          buttonName = (sender as Button).Content.ToString();
            ImageProcessing process    = new ImageProcessing();

            byte[] processedImageBytes;
            //try
            //{
            processedImageBytes   = process.FindContours(originalImageBytes, originalImage.PixelHeight, originalImage.PixelWidth);
            grayscalePanel.Source = ImageConvertor.ByteArrayToImage(processedImageBytes, originalImage.PixelWidth, originalImage.PixelHeight, 1);
            //}
            //catch (Exception ex) {
            //MessageBox.Show("Smth went so wrong...");
            //}
        }
        public byte[] FindContours(byte[] originalImage, int xsize, int ysize)
        {
            byte[] preimage = setGrayscale(originalImage);
            Pixel[,] pixels     = ImageConvertor.GetPixelArrayFromImage(preimage, xsize, ysize);
            Pixel[,] new_pixels = new Pixel[pixels.GetLength(0), pixels.GetLength(1)];
            for (int y = 0; y < pixels.GetLength(0) - 1; y++)
            {
                for (int x = 0; x < pixels.GetLength(1) - 1; x++)
                {
                    Pixel temp1 = pixels[y, x] - pixels[y + 1, x + 1];
                    Pixel temp2 = pixels[y, x + 1] - pixels[y + 1, x];
                    new_pixels[y, x] = new Pixel()
                    {
                        R     = (byte)(Math.Sqrt(temp1.R * temp1.R + temp2.R * temp2.R)),
                        G     = (byte)(Math.Sqrt(temp1.G * temp1.G + temp2.G * temp2.G)),
                        B     = (byte)(Math.Sqrt(temp1.B * temp1.B + temp2.B * temp2.B)),
                        Alpha = (byte)(Math.Sqrt(temp1.Alpha * temp1.Alpha + temp2.Alpha * temp2.Alpha)),
                    };
                }
            }
            List <byte> result = new List <byte>();

            for (int y = 0; y < new_pixels.GetLength(0); y++)
            {
                for (int x = 0; x < new_pixels.GetLength(1); x++)
                {
                    byte result_sum = (byte)((new_pixels[y, x].R + new_pixels[y, x].G + new_pixels[y, x].B + new_pixels[y, x].Alpha) / 4);
                    //result.Add(pixels[y, x].B);
                    //result.Add(pixels[y, x].G);
                    //result.Add(pixels[y, x].R);
                    //result.Add(pixels[y, x].Alpha);
                    result.Add(result_sum);
                    result.Add(result_sum);
                    result.Add(result_sum);
                    result.Add(result_sum);
                }
            }
            return(result.ToArray());
        }
        // click on processing buttons
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string          buttonName = (sender as Button).Content.ToString();
            ImageProcessing process    = new ImageProcessing();

            byte[] processedImageBytes;
            try
            {
                switch (buttonName[0])
                {
                // if the button is "Red filter"
                case 'R':
                    processedImageBytes   = process.setRedFilter(originalImageBytes);
                    grayscalePanel.Source = ImageConvertor.ByteArrayToImage(processedImageBytes, originalImage.PixelWidth, originalImage.PixelHeight, 4);
                    break;

                // if the button is "Invert"
                case 'I':
                    processedImageBytes   = process.setInvert(originalImageBytes);
                    grayscalePanel.Source = ImageConvertor.ByteArrayToImage(processedImageBytes, originalImage.PixelWidth, originalImage.PixelHeight, 4);
                    break;

                // if the button is "Gray-scale"
                case 'G':
                    processedImageBytes   = process.setGrayscale(originalImageBytes);
                    grayscalePanel.Source = ImageConvertor.ByteArrayToImage(processedImageBytes, originalImage.PixelWidth, originalImage.PixelHeight, 1);
                    break;

                // if smth stupid happend
                default:
                    break;
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Smth went so wrong...");
            }
        }
 // show image on the window
 private void showImage(string filename)
 {
     originalImage        = ImageConvertor.FilenameToImage(filename);
     originalImageBytes   = ImageConvertor.ImageToByteArray(filename);
     originalPanel.Source = originalImage;
 }