Exemple #1
1
        private void button_Click(object sender, EventArgs e)
        {
            Stream stream = null;

            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = "c:\\";
            openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((stream = openFileDialog.OpenFile()) != null)
                    {
                        using (stream)
                        {
                            string pathName = openFileDialog.FileName;

                            Bitmap image = new Bitmap(pathName);

                            pictureBox1.Image = image.Resize(pictureBox1.Width, pictureBox1.Height);

                            Mask mask = new Mask(new int[,] { { 1, 1, 1 }, { 1, -8, 1 }, { 1, 1, 1 } });

                            image = LaplacianSharper.SharpImage(image, mask);

                            pictureBox2.Image = image.Resize(pictureBox2.Width, pictureBox2.Height);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }
        public static Bitmap SharpImage(Bitmap bitmap, Mask mask)
        {
            GetIntensities(bitmap);

            SubstractImage(ImposeMask(mask));

            ModifyImage();

            return CreateBitmap();
        }
        private static int[,] ImposeMask(Mask mask)
        {
            int[,] imageWithMask = new int[width, height];

            for (int x = 1; x < width - 1; x++)
            {
                for (int y = 1; y < height - 1; y++)
                {
                    imageWithMask[x, y] = pixelArray[x - 1, y - 1] * mask.GetCell(0, 0) +
                                          pixelArray[x, y - 1] * mask.GetCell(1, 0) +
                                          pixelArray[x + 1, y - 1] * mask.GetCell(2, 0) +
                                          pixelArray[x - 1, y] * mask.GetCell(0, 1) +
                                          pixelArray[x, y] * mask.GetCell(1, 1) +
                                          pixelArray[x + 1, y] * mask.GetCell(2, 1) +
                                          pixelArray[x - 1, y + 1] * mask.GetCell(0, 2) +
                                          pixelArray[x, y + 1] * mask.GetCell(1, 2) +
                                          pixelArray[x + 1, y + 1] * mask.GetCell(2, 2);
                }
            }

            return imageWithMask;
        }