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 _20190528_メディアンフィルタ
{
<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メディアン(byte[] pixels, int width, int height)
{
byte[] filtered = new byte[pixels.Length];
int stride = width;
byte[] v = new byte[9];
for (int y = 1; y < height - 1; y++)
{
for (int x = 1; x < width - 1; x++)
{
int p = x + y * stride;
v[0] = pixels[p - stride - 1];
v[1] = pixels[p - stride];
v[2] = pixels[p - stride + 1];
v[3] = pixels[p - 1];
v[4] = pixels[p];
v[5] = pixels[p + 1];
v[6] = pixels[p + stride - 1];
v[7] = pixels[p + stride];
v[8] = pixels[p + stride + 1];
filtered[p] = v.OrderBy(z => z).ToList()[4];
}
}
return (filtered, BitmapSource.Create(
width, height, 96, 96, PixelFormats.Gray8, null, filtered, width));
}
private (byte[] pixels, BitmapSource bitmap) Filterメディアン4近傍(byte[] pixels, int width, int height)
{
byte[] filtered = new byte[pixels.Length];
int stride = width;
byte[] v = new byte[5];
for (int y = 1; y < height - 1; y++)
{
for (int x = 1; x < width - 1; x++)
{
int p = x + y * stride;
v[0] = pixels[p - stride];
v[1] = pixels[p - 1];
v[2] = pixels[p];
v[3] = pixels[p + 1];
v[4] = pixels[p + stride];
filtered[p] = v.OrderBy(z => z).ToList()[2];
}
}
return (filtered, BitmapSource.Create(
width, height, 96, 96, PixelFormats.Gray8, null, filtered, width));
}
private (byte[] pixels, BitmapSource bitmap) Filterメディアン4近傍斜め(byte[] pixels, int width, int height)
{
byte[] filtered = new byte[pixels.Length];
int stride = width;
byte[] v = new byte[5];
for (int y = 1; y < height - 1; y++)
{
for (int x = 1; x < width - 1; x++)
{
int p = x + y * stride;
v[0] = pixels[p - stride - 1];
v[1] = pixels[p - stride + 1];
v[2] = pixels[p];
v[3] = pixels[p + stride - 1];
v[4] = pixels[p + stride + 1];
filtered[p] = v.OrderBy(z => z).ToList()[2];
}
}
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;
byte[] v = new byte[8];
for (int y = 1; y < height - 1; y++)
{
for (int x = 1; x < width - 1; x++)
{
int p = x + y * stride;
v[0] = pixels[p - stride - 1];
v[1] = pixels[p - stride];
v[2] = pixels[p - stride + 1];
v[3] = pixels[p - 1];
v[4] = pixels[p + 1];
v[5] = pixels[p + stride - 1];
v[6] = pixels[p + stride];
v[7] = pixels[p + stride + 1];
var temp = v.OrderBy(z => z).ToList();
filtered[p] = (byte)Math.Round((temp[3] + temp[4]) / 2.0, MidpointRounding.AwayFromZero);
}
}
return (filtered, BitmapSource.Create(
width, height, 96, 96, PixelFormats.Gray8, null, filtered, width));
}
private (byte[] pixels, BitmapSource bitmap) Filterメディアン24近傍(byte[] pixels, int width, int height)
{
byte[] filtered = new byte[pixels.Length];
int stride = width;
byte[] v = new byte[25];
for (int y = 2; y < height - 2; y++)
{
for (int x = 2; x < width - 2; x++)
{
for (int i = 0; i < 5; i++)
{
int pp = (y + i - 2) * stride + x - 2;
for (int j = 0; j < 5; j++)
{
v[i * 5 + j] = pixels[pp + j];
}
}
var neko = v.OrderBy(z => z).ToList()[12];
filtered[y * stride + x] = v.OrderBy(z => z).ToList()[12];
}
}
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_1(object sender, RoutedEventArgs e)
{
var source = Clipboard.GetImage();
if (source == null)
{
MessageBox.Show("(クリップボードに画像は)ないです");
}
else
{
int w = source.PixelWidth;
int h = source.PixelHeight;
int stride = w;
var gray = new FormatConvertedBitmap(source, PixelFormats.Gray8, null, 0);
byte[] pixels = new byte[h * stride];
gray.CopyPixels(pixels, stride, 0);
MyPixels = pixels;
MyPixelsOrigin = pixels;
MyBitmapOrigin = gray;
MyImage.Source = gray;
MyImageOrigin.Source = gray;
}
}
private void Button_Click_4(object sender, RoutedEventArgs e)
{
Clipboard.SetImage((BitmapSource)MyImage.Source);
}
private void Button_Click(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_5(object sender, RoutedEventArgs e)
{
if (MyPixels == null) { return; }
(byte[] pixels, BitmapSource bitmap) = Filterメディアン4近傍(
MyPixels,
MyBitmapOrigin.PixelWidth,
MyBitmapOrigin.PixelHeight);
MyImage.Source = bitmap;
MyPixels = pixels;
}
private void Button_Click_6(object sender, RoutedEventArgs e)
{
if (MyPixels == null) { return; }
(byte[] pixels, BitmapSource bitmap) = Filterメディアン4近傍斜め(
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;
}
private void Button_Click_8(object sender, RoutedEventArgs e)
{
if (MyPixels == null) { return; }
(byte[] pixels, BitmapSource bitmap) = Filterメディアン24近傍(
MyPixels,
MyBitmapOrigin.PixelWidth,
MyBitmapOrigin.PixelHeight);
MyImage.Source = bitmap;
MyPixels = pixels;
}
}
}