public void ApplySobelFilterFromFileTest() { // preperation FileStream inputFile = new FileStream("..\\..\\..\\Resources\\OriginalCat.bmp", FileMode.Open, FileAccess.Read); FileStream expectedOutputFile = new FileStream("..\\..\\..\\Resources\\SobelFilterCat.bmp", FileMode.Open, FileAccess.Read); byte[] temp = new byte[54]; inputFile.Read(temp, 0, 54); BMPFileHeader bmpInfo = new BMPFileHeader(temp); SobelFilter image = new SobelFilter(bmpInfo, inputFile); // checking SobelFilter image.ApplySobelFilter(SobelFilter.FilterType.SobelFilter); 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 операторПоОсиXToolStripMenuItem_Click(object sender, EventArgs e) { char oper = 'X'; Filter filter = new SobelFilter(oper); backgroundWorker1.RunWorkerAsync(filter); }
private void timerTick(object sender, EventArgs e) { Console.WriteLine("TICK!"); var image = new Bitmap(videoCapture.QueryFrame().Bitmap); var undistoredImage = UndistorImage(image); var resultAfterStep1 = (MyImage)undistoredImage; resultAfterStep1 = GrayScaleFilter.Convert(resultAfterStep1); var resultAfterStep2 = BinarizationConverter.Convert(resultAfterStep1); var resultAfterStep3 = SobelFilter.Convert(resultAfterStep2); var candidateCorners = CornerDetector.FindCorners(resultAfterStep3); var markerCorners = MarkersRecognizer.SelectMarkers(candidateCorners, undistoredImage); var miniImage = new Bitmap(undistoredImage, new Size(undistoredImage.Width / sizer, undistoredImage.Height / sizer)); g.DrawImage(miniImage, 0, 0, miniImage.Width, miniImage.Height); foreach (var corners in markerCorners) { g.DrawPolygon(pen, corners.Select(p => new Point(p.X / sizer, p.Y / sizer)).ToArray()); } }
protected override List <ulong> CalculatePreceptiveHashesByImage(Color[,] image) { var imageWithSuperimposedSobelFilter = SobelFilter.Transform(image); var threshold = OtcuBinarization.CalcThreshold(imageWithSuperimposedSobelFilter); return(PreceptiveHash.ByImage(imageWithSuperimposedSobelFilter, threshold)); }
/// <summary> /// Applies the sobel filter. /// </summary> public async void ApplySobelFilter() { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { var sobelFilter = new SobelFilter(this.CopiedPicture); sobelFilter.ApplySobelFilter(); }); }
private void sobelToolStripMenuItem_Click(object sender, EventArgs e) { if (pictureBox1.Image != null) { cashBack.Push(image); } Filters filter = new SobelFilter(); backgroundWorker1.RunWorkerAsync(filter); }
private void sobelButton_Click(object sender, EventArgs e) { if (sobelInputImgBox.Image != null) { var inputBitmap = new Bitmap(sobelInputImgBox.Image); var sobelFilterClient = new SobelFilter(inputBitmap, Settings.Default.StoreAsColorWherePossible); sobelOutputImgBox.Image = sobelFilterClient.Filter(); } }
private void SobelFilter_Click(object sender, RoutedEventArgs e) { try { var bitmap = SobelFilter.ToSobelFilter(originalImage); filteredImage.Source = GetBitmapSource(bitmap); } catch (OriginalImageDontExistException exception) { MessageBox.Show(exception.Message); } }
private AbstractFilter GetFilter() { AbstractFilter tmpFilter = null; if (homogenityRB.Checked) { tmpFilter = new HomogenityFilter(); } if (differenceRB.Checked) { tmpFilter = new DifferenceFilter(); } if (sobelRB.Checked) { tmpFilter = new SobelFilter(); } if (cannyRB.Checked) { tmpFilter = new CannyFilter(); } return(tmpFilter); }
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(); }
private void собельToolStripMenuItem_Click(object sender, EventArgs e) { Filters filter = new SobelFilter(); backgroundWorker1.RunWorkerAsync(filter); }
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 } }