5x5以上は違いが感じられない
カーネル 3x3 5x5 7x7 9x9
距離 0, 1, 2, 3, 4
影響 100, 61, 14, 1, 0
5x5と9x9だと14%の差があるけど、この程度だと違いがわからない
それ以上は差は出ているけど小さいかな
んーでも5x5と9x9を比較すると結構差がある感じもする
カーネル 3x3 5x5 7x7 9x9
距離 0, 1, 2, 3, 4
影響 100, 88, 61, 32, 14
<feff>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace _20190427_ガウス関数
{
<summary>
</summary>
public partial class MainWindow : Window
{
string ImageFileFullPath;
BitmapSource MyBitmapOrigin;
byte[] MyPixelsOrigin;
byte[] MyPixels;
public MainWindow()
{
InitializeComponent();
this.Drop += MainWindow_Drop;
this.AllowDrop = true;
}
<summary>
</summary>
<param name="stdev">
</param>
<param name="size"></param>
<returns></returns>
private (double[,] kernel, double div) Makeガウシアンカーネル(double stdev, int size)
{
double variance = stdev * stdev;
double f = 1 / Math.Sqrt(2 * Math.PI * variance);
int length = size / 2;
double[] temp = new double[size];
for (int i = 0; i < size; i++)
{
var f2 = -(Math.Pow(i - length, 2) / (2 * variance));
var f3 = f * Math.Pow(Math.E, f2);
temp[i] = f3;
}
double[,] kernel = new double[size, size];
double div = 0;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
kernel[i, j] = temp[i] * temp[j];
div += kernel[i, j];
}
}
return (kernel, div);
}
<summary>
</summary>
<param name="stdev"></param>
<param name="size"></param>
<param name="pixels"></param>
<param name="width"></param>
<param name="height"></param>
<returns></returns>
private (byte[] pixels, BitmapSource bitmap) Filterガウシアンフィルタ3x3(
double stdev, int size, byte[] pixels, int width, int height)
{
(double[,] kernel, double div) = Makeガウシアンカーネル(stdev, size);
byte[] filtered = new byte[pixels.Length];
int stride = width;
for (int y = 1; y < height - 1; y++)
{
for (int x = 1; x < width - 1; x++)
{
double total = 0;
for (int i = 0; i < 3; i++)
{
int p = x + ((y + i - 1) * stride);
for (int j = 0; j < 3; j++)
{
total += pixels[p + j - 1] * kernel[i, j];
}
}
int average = (int)(total / div);
filtered[x + y * stride] = (byte)average;
}
}
return (filtered, BitmapSource.Create(
width, height, 96, 96, PixelFormats.Gray8, null, filtered, width));
}
private (byte[] pixels, BitmapSource bitmap) Filterガウシアンフィルタ2サイズ指定(
double stdev, int size, byte[] pixels, int width, int height)
{
(double[,] kernel, double div) = Makeガウシアンカーネル(stdev, size);
byte[] filtered = new byte[pixels.Length];
int stride = width;
int range = kernel.GetLength(0) / 2;
for (int y = range; y < height - range; y++)
{
for (int x = range; x < width - range; x++)
{
double total = 0;
for (int i = 0; i < size; i++)
{
int p = x + ((y + i - range) * stride);
for (int j = 0; j < size; j++)
{
total += pixels[p + j - range] * kernel[i, j];
}
}
int average = (int)(total / div);
average = average < 0 ? 0 : average > 255 ? 255 : average;
filtered[x + y * stride] = (byte)average;
}
}
return (filtered, BitmapSource.Create(
width, height, 96, 96, PixelFormats.Gray8, null, filtered, width));
}
private (byte[] pixels, BitmapSource bitmap) Filterガウシアンフィルタサイズ指定色補正あり(
double stdev, int size, byte[] pixels, int width, int height)
{
(double[,] kernel, double div) = Makeガウシアンカーネル(stdev, size);
byte[] filtered = new byte[pixels.Length];
int stride = width;
int range = kernel.GetLength(0) / 2;
for (int y = range; y < height - range; y++)
{
for (int x = range; x < width - range; x++)
{
double total = 0;
for (int i = 0; i < size; i++)
{
int p = x + ((y + i - range) * stride);
for (int j = 0; j < size; j++)
{
total += Math.Pow(pixels[p + j - range], 2.0) * kernel[i, j];
}
}
int average = (int)Math.Sqrt(total / div);
filtered[x + y * stride] = (byte)average;
}
}
return (filtered, BitmapSource.Create(
width, height, 96, 96, PixelFormats.Gray8, null, filtered, width));
}
#region その他
private void MainWindow_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop) == false) { return; }
string[] filePath = (string[])e.Data.GetData(DataFormats.FileDrop);
var (pixels, bitmap) = MakeBitmapSourceAndByteArray(filePath[0], PixelFormats.Gray8, 96, 96);
if (bitmap == null)
{
MessageBox.Show("画像ファイルじゃないみたい");
}
else
{
MyPixels = pixels;
MyPixelsOrigin = pixels;
MyBitmapOrigin = bitmap;
MyImage.Source = bitmap;
MyImageOrigin.Source = bitmap;
ImageFileFullPath = filePath[0];
}
}
private void SaveImage(BitmapSource source)
{
var saveFileDialog = new Microsoft.Win32.SaveFileDialog();
saveFileDialog.Filter = "*.png|*.png|*.bmp|*.bmp|*.tiff|*.tiff";
saveFileDialog.AddExtension = true;
saveFileDialog.FileName = System.IO.Path.GetFileNameWithoutExtension(ImageFileFullPath) + "_";
saveFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(ImageFileFullPath);
if (saveFileDialog.ShowDialog() == true)
{
BitmapEncoder encoder = new BmpBitmapEncoder();
if (saveFileDialog.FilterIndex == 1)
{
encoder = new PngBitmapEncoder();
}
else if (saveFileDialog.FilterIndex == 2)
{
encoder = new BmpBitmapEncoder();
}
else if (saveFileDialog.FilterIndex == 3)
{
encoder = new TiffBitmapEncoder();
}
encoder.Frames.Add(BitmapFrame.Create(source));
using (var fs = new System.IO.FileStream(saveFileDialog.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
encoder.Save(fs);
}
}
}
<summary>
</summary>
<param name="filePath"></param>
<param name="pixelFormat"></param>
<param name="dpiX"></param>
<param name="dpiY"></param>
<returns></returns>
private (byte[] array, BitmapSource source) MakeBitmapSourceAndByteArray(string filePath, PixelFormat pixelFormat, double dpiX = 0, double dpiY = 0)
{
byte[] pixels = null;
BitmapSource source = null;
try
{
using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
var bf = BitmapFrame.Create(fs);
var convertedBitmap = new FormatConvertedBitmap(bf, pixelFormat, null, 0);
int w = convertedBitmap.PixelWidth;
int h = convertedBitmap.PixelHeight;
int stride = (w * pixelFormat.BitsPerPixel + 7) / 8;
pixels = new byte[h * stride];
convertedBitmap.CopyPixels(pixels, stride, 0);
if (dpiX == 0) { dpiX = bf.DpiX; }
if (dpiY == 0) { dpiY = bf.DpiY; }
source = BitmapSource.Create(
w, h, dpiX, dpiY,
convertedBitmap.Format,
convertedBitmap.Palette, pixels, stride);
};
}
catch (Exception)
{
}
return (pixels, source);
}
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
int aa = Panel.GetZIndex(MyImage);
Panel.SetZIndex(MyImageOrigin, aa + 1);
}
private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
int aa = Panel.GetZIndex(MyImage);
Panel.SetZIndex(MyImageOrigin, aa - 1);
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
MyImage.Source = MyBitmapOrigin;
MyPixels = MyPixelsOrigin;
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
if (MyImage.Source == null) { return; }
SaveImage((BitmapSource)MyImage.Source);
}
#endregion
private void Button_Click(object sender, RoutedEventArgs e)
{
if (MyPixels == null) { return; }
(byte[] pixels, BitmapSource bitmap) = Filterガウシアンフィルタ3x3(
Slider標準偏差.Value,
3,
MyPixels,
MyBitmapOrigin.PixelWidth,
MyBitmapOrigin.PixelHeight);
MyImage.Source = bitmap;
MyPixels = pixels;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (MyPixels == null) { return; }
(byte[] pixels, BitmapSource bitmap) = Filterガウシアンフィルタ2サイズ指定(
Slider標準偏差.Value,
(int)SliderKernelサイズ.Value,
MyPixels,
MyBitmapOrigin.PixelWidth,
MyBitmapOrigin.PixelHeight);
MyImage.Source = bitmap;
MyPixels = pixels;
}
private void Button_Click_7(object sender, RoutedEventArgs e)
{
if (MyPixels == null) { return; }
(byte[] pixels, BitmapSource bitmap) = Filterガウシアンフィルタサイズ指定色補正あり(
Slider標準偏差.Value,
(int)SliderKernelサイズ.Value,
MyPixels,
MyBitmapOrigin.PixelWidth,
MyBitmapOrigin.PixelHeight);
MyImage.Source = bitmap;
MyPixels = pixels;
}
}
}