###前提・実現したいこと
C#で画像の差分をしたいと考えております。
使用する画像A、Bはどちらもグレースケールに変換済みです。
画像の輝度のみで差分したいです。
方法として画像A,Bのピクセルデータを取得し端からA輝度-B輝度を計算、再び画像に戻す操作をします
###発生している問題・エラーメッセージ
・下記の輝度の計算img.SetPixel(x, y) = _img1.GetPixel(x, y).GetBrightness() - _img1.GetPixel(x, y).GetBrightness();がうまくいきません
・画像の取得についてもっと良い方法がありましたらご教授のほどを宜しくお願いします
###該当のソースコード
public static Bitmap CreateImage(Bitmap img ,Bitmap _img1,Bitmap _img2)
{
//画像A取得
Bitmap newImgA = new Bitmap(_img1.Width, _img1.Height,
PixelFormat.Format1bppIndexed);
BitmapData bmpDateA = newImgA.LockBits(
new Rectangle(0, 0, newImgA.Width, newImgA.Height),
ImageLockMode.WriteOnly, newImgA.PixelFormat);
//画像B取得 Bitmap newImgB = new Bitmap(_img2.Width, _img2.Height, PixelFormat.Format1bppIndexed); BitmapData bmpDateB = newImgB.LockBits( new Rectangle(0, 0, newImgB.Width, newImgB.Height), ImageLockMode.WriteOnly, newImgB.PixelFormat); //1bppイメージを作成する Bitmap newImg = new Bitmap(img.Width, img.Height, PixelFormat.Format1bppIndexed); //Bitmapをロックする BitmapData bmpDate = newImg.LockBits( new Rectangle(0, 0, newImg.Width, newImg.Height), ImageLockMode.WriteOnly, newImg.PixelFormat); //新しい画像のピクセルデータを作成する byte[] pixels = new byte[bmpDate.Width * bmpDate.Height]; for (int y = 0; y < bmpDate.Height; y++) { for (int x = 0; x < bmpDate.Width; x++) { img.SetPixel(x, y) = _img1.GetPixel(x, y).GetBrightness() - _img1.GetPixel(x, y).GetBrightness(); } } //作成したピクセルデータをコピーする IntPtr ptr = bmpDate.Scan0; System.Runtime.InteropServices.Marshal.Copy(pixels, 0, ptr, pixels.Length); //ロック解除 newImg.UnlockBits(bmpDateA); newImg.UnlockBits(bmpDateB); newImg.UnlockBits(bmpDate); return newImg; }
###試したこと
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報