カラー画像もグレースケールで表示される
処理の重ねがけ
元画像とぼかし処理後画像の切り替え
画像を左クリック押している間は元画像、離すと処理後画像表示
今回のアプリのダウンロード先
デザイン画面
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace _20190422_平均ぼかし処理
{
<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;
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];
total += pixels[p - 1];
total += pixels[p];
total += pixels[p + 1];
total += pixels[p + stride];
int average = total / 5;
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上下左右(MyPixels, MyBitmapOrigin.PixelWidth, MyBitmapOrigin.PixelHeight);
MyImage.Source = bitmap;
MyPixels = pixels;
}
}
}
61~183行目は、ぼかし処理と関係ない
エクセル方眼紙で
(1,1)の輝度200は
(200+200+200+100+100)/5=160
(1,2)の輝度100は
(100+200+100+200+100)/5=140
たとえば一番左上
ピクセル(0,0)の場合だと、上と左の
ピクセルは存在しないので、別の処理をする必要がある、存在する
ピクセルだけでの平均とか、元画像の値そのままにするとか
今回は(も)めんどくさいので処理しないで0で埋めることにした
なので画像の外周は黒色になる
右上付近を30倍に拡大と値表示
BitmapSourceクラスのCopyPixelsメソッドを使って得られるものを
Filter上下左右に渡す
配列の中の注目
ピクセルの位置がわかれば、左は-1、右は+1
上下は1行分のbyte数(stride)で、上は-stride、下は+strideになる
PixelFormats.Gray8専用なのでstrideは画像の横幅
ピクセルと同じ
4x4
ピクセルの画像のときCopyPixelsの配列は
4x4=16の要素
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
strideは横幅と同じなので4
このとき注目
ピクセルが1,2のとき、配列での位置(インデックス)は9
これは79行目のp = x + y * stride;は1+2*4=9
左は9-1で8、右は9+1で10、上は9-strideは9-4=5、下は9+strideは9+4=13
参照したところ
ギットハブ
関連記事
2019/04/23は次の日
2019/04/25