public void ApplyGaussFilterFromFileTest()
        {
            // preperation

            FileStream inputFile          = new FileStream("..\\..\\..\\Resources\\OriginalCat.bmp", FileMode.Open, FileAccess.Read);
            FileStream expectedOutputFile = new FileStream("..\\..\\..\\Resources\\GaussFilterCat.bmp", FileMode.Open, FileAccess.Read);

            byte[] temp = new byte[54];
            inputFile.Read(temp, 0, 54);
            BMPFileHeader bmpInfo = new BMPFileHeader(temp);
            GaussFilter   image   = new GaussFilter(bmpInfo, inputFile);

            // checking

            image.ApplyGaussFilter();
            byte[] outputFile = new byte[bmpInfo.FileSize];
            image.WriteImage(outputFile);

            for (uint i = 0; i < bmpInfo.FileSize; i++)
            {
                Assert.IsTrue(outputFile[i] == (byte)expectedOutputFile.ReadByte());
            }

            inputFile.Close();
            expectedOutputFile.Close();
        }
        private void GaussFilter_Click(object sender, EventArgs e)
        {
            var bitmap      = GetPictureBox().Image;
            var gaussFilter = new GaussFilter();

            pictureBox5.Image = gaussFilter.FilterProcessImage(trackBar2.Value, (Bitmap)bitmap);
        }
        private static void testFiltering(string filePath, string outputPath)
        {
            ImageDescription inputImage = ImageFileHandler.loadFromPath(filePath);
            //ImageFilter filter = new CannyAppenderFilter();
            //ImageFilter filter = new KirschAppenderFilter();
            ImageFilter      filter      = new SobelAppenderFilter();
            ImageDescription outputImage = filter.filter(inputImage);
            GaussFilter      gaussFilter = new GaussFilter(5, 1.4f, new HashSet <ColorChannelEnum> {
                ColorChannelEnum.Red, ColorChannelEnum.Green, ColorChannelEnum.Blue
            });

            outputImage = gaussFilter.filter(outputImage);
            ImageFileHandler.saveToPath(outputImage, outputPath, ".png");

            //ImageDescription newOutputImage = new ImageDescription();
            //newOutputImage.sizeX = outputImage.sizeX;
            //newOutputImage.sizeY = outputImage.sizeY;
            //newOutputImage.grayscale = true;
            //newOutputImage.setColorChannel(ColorChannelEnum.Gray, outputImage.getColorChannel(ColorChannelEnum.Canny));
            //ImageFileHandler.saveToPath(newOutputImage, "test2", ".png");
        }
Esempio n. 4
0
 public SequenceGenerator(double oversampling, double BT)
 {
     Oversampling = oversampling;
     Sampler      = new Resampler((decimal)Oversampling);
     Filter       = new GaussFilter(BT);
 }
        static void Main(string[] args)
        {
            PrintDescription();

            if (args.Length != 3)
            {
                Console.WriteLine(
                    "Неверное количество аргументов. Формат: <программа> <имя_входного_файла> <название_фильтра> <имя выходного файла>");
                return;
            }

            string currentFilter = args[1];

            if (!currentFilter.Equals("median") && !currentFilter.Equals("gauss") && !currentFilter.Equals("sobelx") &&
                !currentFilter.Equals("sobely") && !currentFilter.Equals("gray"))
            {
                Console.WriteLine("Введённый фильтр не относится к списку доступных. Попробуйте ещё раз.");
                return;
            }

            FileStream fileIn;

            try
            {
                fileIn = new FileStream(args[0], FileMode.Open, FileAccess.ReadWrite);
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Ошибка! Входной файл не был найден! Попробуйте ещё раз.");
                return;
            }

            FileStream fileOut;

            try
            {
                fileOut = new FileStream(args[2], FileMode.Create, FileAccess.ReadWrite);
            }
            catch (Exception)
            {
                fileIn.Close();
                Console.WriteLine("Ошибка! Не удалось создать/открыть выходной файл. Попробуйте ещё раз.");
                return;
            }

            BitMapFile file = new(fileIn);

            fileIn.Close();

            if (currentFilter.Equals("gray"))
            {
                Console.WriteLine("Выбран фильтр серого");
                GrayFilter.ApplyFilter(file);
            }
            else if (currentFilter.Equals("median"))
            {
                Console.WriteLine("Выбран усредняющий фильтр");
                MedianFilter.ApplyFilter(file);
            }
            else if (currentFilter.Equals("gauss"))
            {
                Console.WriteLine("Выбран фильтр Гаусса");
                GaussFilter.ApplyFilter(file);
            }
            else if (currentFilter.Equals("sobelx"))
            {
                Console.WriteLine("Выбран фильтр Собеля по X");
                SobelFilter.ApplyFilter(file, SobelFilter.Type.X);
            }
            else if (currentFilter.Equals("sobely"))
            {
                Console.WriteLine("Выбран фильтр Собеля по Y");
                SobelFilter.ApplyFilter(file, SobelFilter.Type.Y);
            }

            Console.WriteLine("Фильтр был успешно применён");

            file.WriteResult(fileOut);
            fileOut.Close();
        }
Esempio n. 6
0
        private void btnCreate_Click(object sender, EventArgs e)
        {
            string srcBits = txtSequence.Text;

            double BT           = 0;
            double Oversampling = 0;

            try
            {
                BT           = double.Parse(txtBT.Text);
                Oversampling = double.Parse(txtOversampling.Text);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("Invalid parameter. Either BT or Oversampling value is wrong.");
                return;
            }


            double[] srcData = new double[srcBits.Length];

            for (int pos = 0; pos < srcBits.Length; pos++)
            {
                if (srcBits.Substring(pos, 1) == "1")
                {
                    srcData[pos] = 1.0;
                }
                else if (srcBits.Substring(pos, 1) == "0")
                {
                    srcData[pos] = -1.0;
                }
                else
                {
                    MessageBox.Show("Invalid binary sequence.");
                    return;
                }
            }

            //            byte[] srcData = new byte[] {0xB9, 0x62, 0x04, 0x0F, 0x2D, 0x45, 0x76, 0x1B};
            //            srcData = DifferenceCode.Encode(srcData);

            Resampler sampler = new Resampler((decimal)Oversampling);

            double[] samples = sampler.Resample(srcData);

            GaussFilter filter = new GaussFilter(BT);

            double[] gaussSamples = filter.Process(samples, Oversampling);

            waveformDisplay.Clear();
            waveformDisplay.XAxisUnit = Oversampling;

            waveformDisplay.XAxisGridOffset = 128;
            waveformDisplay.ProcessData(new double[128]);

            waveformDisplay.ProcessData(gaussSamples);

            if (ShmemChannel != null)
            {
                double[] diffSamples = Differenciator.Differenciate(gaussSamples);
                ShmemChannel.Write(ByteUtil.convertToBytesInterleaved(gaussSamples, diffSamples));
            }
        }
Esempio n. 7
0
using System;
using System.Drawing;
using System.IO;
using ImageFilter.Filters;
using ImageFilter.Noises;
using ImageFilter.Extensions;
using OxyPlot;
using OxyPlot.WindowsForms;
using OxyPlot.Axes;
using OxyPlot.Series;
using System.Collections.Generic;

namespace ImageFilter
{
    public class ImageLoader : IDisposable
    {
        #region Fields

        private bool isDisposed;

        #endregion

        #region Propeties

        public Image Image { get; set; }
        public string ImagePath { get; private set; }

        #endregion

        #region Methods

        public double CalculatePSNR(Image image)
        {
            var img1 = (Bitmap) Image;
            var img2 = (Bitmap) image;

            int width = image.Width;
            int height = image.Height;

            double psnrY = 0;
            using (var img1BMP = new ConcurrentBitmap(img1))
            {
                using (var img2BMP = new ConcurrentBitmap(img2))
                {
                    // For each line
                    for (var y = 0; y < height; y++)
                    {
                        // For each pixel
                        for (var x = 0; x < width; x++)
                        {
                            Color img1Color = img1BMP.GetPixel(x, y);

                            // Assumes that img2 is not in Y component
                            Color tmpColor = img2BMP.GetPixel(x, y);
                            var I = (int) (tmpColor.R * 0.299
                                           + tmpColor.G * 0.587
                                           + tmpColor.B * 0.114);

                            Color img2Color = Color.FromArgb(I, I, I);

                            psnrY += Math.Pow(img1Color.R - img2Color.R, 2);
                        }
                    }
                }
            }

            psnrY = 10 * Math.Log10(width * height * Math.Pow(Math.Pow(2, 8) - 1, 2) / psnrY);
            /*Console.WriteLine($"Y: {psnrY}");*/
            return psnrY;
        }

        public ImageLoader Load(string filePath)
        {
            var fileInfo = new FileInfo(filePath);

            if (!fileInfo.Exists)
            {
                throw new FileNotFoundException(filePath);
            }

            ImagePath = filePath;

            // Open a file stream

            using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                var stream = new MemoryStream();
                fileStream.CopyTo(stream);
                stream.Position = 0;

                Image = Image.FromStream(stream);
            }

            return this;
        }

        

        public ImageLoader Save(string filePath)
        {
            var dirInfo = new DirectoryInfo(Path.GetDirectoryName(filePath));

            if (!dirInfo.Exists)
            {
                dirInfo.Create();
            }

            Image.Save(filePath);

            return this;
        }

        public ImageLoader AddImage(Image I, double[,] img)
        {
            Bitmap one = (Bitmap) I;

            //Bitmap two = (Bitmap) img;

            Bitmap res = new Bitmap(one.Width, one.Height);

            int height = Image.Height;
            int width =  Image.Width;
            for (int y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    double blue;
                    double green;
                    double red = green = blue = 0;

                    Color colorOne = one.GetPixel(x, y);
                    int colorTwo = (int) img[y, x];//two.GetPixel(x, y);

                    red = colorOne.R + colorTwo;
                    green = colorOne.G + colorTwo;
                    blue = colorOne.B + colorTwo;

                    Color destinationColor = Color.FromArgb(
                                    Convert.ToByte(red.Clamp(0, 255)),
                                    Convert.ToByte(green.Clamp(0, 255)),
                                    Convert.ToByte(blue.Clamp(0, 255)));

                    res.SetPixel(x, y, destinationColor);
                }
            }
            Image = res;

            return this;
        }

        public ImageLoader Add128(double[,] I)
        {
            Bitmap image = (Bitmap) Image;
            Bitmap res = new Bitmap(Image.Width, Image.Height);
            int height = Image.Height;
            int width = Image.Width;
            for (int y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    double blue;
                    double green;
                    double red = green = blue = 0;

                    red = I[y, x];// 128;
                    blue = I[y, x];// + 128;
                    green = I[y, x];//

                    red += 128;
                    blue += 128;
                    green += 128;

                    Color destinationColor = Color.FromArgb(
                                    Convert.ToByte(red.Clamp(0, 255)),
                        Convert.ToByte(green.Clamp(0, 255)),
                                    Convert.ToByte(blue.Clamp(0, 255)));

                    res.SetPixel(x, y, destinationColor);
                }
            }
            Image = res;
            return this;
        }

        public ImageLoader AddNoise(INoise noise)
        {
            Image = noise.ProcessPicture(this);

            return this;
        }

        public ImageLoader AddBoxFilter(int size)
        {
            var boxFilter = new BoxFilter(size);
            Image = boxFilter.ProcessPicture(this);

            return this;
        }

        public ImageLoader AddGaussFilter(int size, double sigma)
        {
            var boxFilter = new GaussFilter(size, sigma);
            Image = boxFilter.ProcessPicture(this);

            return this;
        }

        public ImageLoader AddMedianFilter(int size)
        {
            var medianFilter = new MedianFilter(size);
            Image = medianFilter.ProcessPicture(this);

            return this;
        }

        public ImageLoader AddLaplacianFilter(double alpha)
        {
            var laplacianFilter = new LaplacianFilter(alpha);
            Image = laplacianFilter.ProcessPicture(this);

            return this;
        }
        public double[,] AddLaplacianFilterAlpha0()
        {
            var laplacianFilter = new LaplacianFilter(0);
            double[,] res = laplacianFilter.ProcessPictureAlpha0(this);

            return res;
        }
        public ImageLoader AddSobelFilter(string direction)
        {
            var sobelFilter = new SobelFilter(direction == "V" ? 1 : 0);

            Image = sobelFilter.ProcessPicture(this);

            return this;
        }

        public double[,] GetSobelFilterDouble(string direction)
        {
            var sobelFilter = new SobelFilter(direction == "V" ? 1 : 0);

            return sobelFilter.Get(this);
        }

        public double CalculateAvgLuminocity()
        {
            double luminocity = 0;
            Bitmap image = (Bitmap)Image;
            int height = image.Height;
            int width = image.Width;
            for (int y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    luminocity += image.GetPixel(x, y).R;
                }
            }

            return luminocity / (double) (height * width);
        }

        private SortedDictionary<int, int> GetFrequency()
        {
            Bitmap image = (Bitmap)Image;
            int height = image.Height;
            int width = image.Width;

            var result = new SortedDictionary<int, int>();
            for (int y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    int Y = image.GetPixel(x, y).R;

                    if (result.ContainsKey(Y))
                    {
                        result[Y] = result[Y] + 1;
                    } else
                    {
                        result.Add(Y, 1);
                    }
                }
            }
            return result;
        }

        public void PlotHistogram(string name, string outputPath, FileInfo fileInfo)
        {
            PngExporter pngExporter = new PngExporter { Width = 1280, Height = 720, Background = OxyColors.White };

            var freq = GetFrequency();

            var x = freq.Values;
            var y = freq.Keys;

            var plotGauss = new PlotModel { Title = $"Histogram for {name}" };
            plotGauss.Axes.Add(new LinearAxis { Title = "x", Position = AxisPosition.Bottom });
            plotGauss.Axes.Add(new LinearAxis { Title = "y", Position = AxisPosition.Left });

            var barSeries = new LinearBarSeries
            {

            };

            foreach (var item in freq)
            {
                barSeries.Points.Add(new DataPoint(item.Key, item.Value));
            }

            plotGauss.Series.Add(barSeries);
            pngExporter.ExportToFile(plotGauss, $"{outputPath}/{fileInfo.Name}/histograms/{name}{fileInfo.Extension}");
        }

        #region Dispose

        ~ImageLoader()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);

            // Already cleaned up in dispose method, supress GC
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (isDisposed)
            {
                return;
            }

            if (disposing)
            {
                if (Image != null)
                {
                    Image.Dispose();
                    Image = null;
                }
            }

            isDisposed = true;
        }

        #endregion

        #endregion
    }
}