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 _20190425_3x3の平均ぼかしとガウシアンフィルタ
{
<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="pixels"></param>
<param name="width"></param>
<param name="height"></param>
<returns></returns>
private (byte[] pixels, BitmapSource bitmap) Filter近傍8平均(byte[] pixels, int width, int height)
{
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++)
{
int total = 0;
int p = x + y * stride;
total += pixels[p - stride - 1];
total += pixels[p - stride];
total += pixels[p - stride + 1];
total += pixels[p - 1];
total += pixels[p];
total += pixels[p + 1];
total += pixels[p + stride - 1];
total += pixels[p + stride];
total += pixels[p + stride + 1];
int average = total / 9;
filtered[p] = (byte)average;
}
}
return (filtered, BitmapSource.Create(
width, height, 96, 96, PixelFormats.Gray8, null, filtered, width));
}
private (byte[] pixels, BitmapSource bitmap) Filter近傍8平均2(byte[] pixels, int width, int height)
{
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++)
{
int total = 0;
for (int i = -1; i < 2; i++)
{
int p = x + ((y + i) * stride);
for (int j = -1; j < 2; j++)
{
total += pixels[p + j];
}
}
int average = total / 9;
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近傍8平均補正あり(byte[] pixels, int width, int height)
{
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;
int p = x + y * stride;
total += Math.Pow(pixels[p - stride - 1], 2);
total += Math.Pow(pixels[p - stride], 2);
total += Math.Pow(pixels[p - stride + 1], 2);
total += Math.Pow(pixels[p - 1], 2);
total += Math.Pow(pixels[p], 2);
total += Math.Pow(pixels[p + 1], 2);
total += Math.Pow(pixels[p + stride - 1], 2);
total += Math.Pow(pixels[p + stride], 2);
total += Math.Pow(pixels[p + stride + 1], 2);
double average = total / 9;
average = Math.Sqrt(average);
filtered[p] = (byte)average;
}
}
return (filtered, BitmapSource.Create(
width, height, 96, 96, PixelFormats.Gray8, null, filtered, width));
}
private (byte[] pixels, BitmapSource bitmap) Filterガウシアンフィルタ(byte[] pixels, int width, int height)
{
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++)
{
int total = 0;
int p = x + y * stride;
total += pixels[p - stride - 1];
total += pixels[p - stride] * 2;
total += pixels[p - stride + 1];
total += pixels[p - 1] * 2;
total += pixels[p] * 4;
total += pixels[p + 1] * 2;
total += pixels[p + stride - 1];
total += pixels[p + stride] * 2;
total += pixels[p + stride + 1];
int average = total / 16;
filtered[p] = (byte)average;
}
}
return (filtered, BitmapSource.Create(
width, height, 96, 96, PixelFormats.Gray8, null, filtered, width));
}
private (byte[] pixels, BitmapSource bitmap) Filterガウシアンフィルタ2(byte[] pixels, int width, int height)
{
int[][] weight = new int[][] {
new int[] { 1, 2, 1 },
new int[] { 2, 4, 2 },
new int[] { 1, 2, 1 } };
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++)
{
int total = 0;
for (int i = -1; i < 2; i++)
{
int p = x + ((y + i) * stride);
for (int j = -1; j < 2; j++)
{
total += pixels[p + j] * weight[i + 1][j + 1];
}
}
int average = total / 16;
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ガウシアンフィルタ補正あり(byte[] pixels, int width, int height)
{
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;
int p = x + y * stride;
total += Math.Pow(pixels[p - stride - 1], 2);
total += Math.Pow(pixels[p - stride], 2) * 2;
total += Math.Pow(pixels[p - stride + 1], 2);
total += Math.Pow(pixels[p - 1], 2) * 2;
total += Math.Pow(pixels[p], 2) * 4;
total += Math.Pow(pixels[p + 1], 2) * 2;
total += Math.Pow(pixels[p + stride - 1], 2);
total += Math.Pow(pixels[p + stride], 2) * 2;
total += Math.Pow(pixels[p + stride + 1], 2);
double average = total / 16;
average = Math.Sqrt(average);
filtered[p] = (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近傍8平均(
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近傍8平均補正あり(
MyPixels, MyBitmapOrigin.PixelWidth, MyBitmapOrigin.PixelHeight);
MyImage.Source = bitmap;
MyPixels = pixels;
}
private void Button_Click_5(object sender, RoutedEventArgs e)
{
if (MyPixels == null) { return; }
(byte[] pixels, BitmapSource bitmap) = Filterガウシアンフィルタ(
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ガウシアンフィルタ補正あり(
MyPixels, MyBitmapOrigin.PixelWidth, MyBitmapOrigin.PixelHeight);
MyImage.Source = bitmap;
MyPixels = pixels;
}
}
}