今日は、C# での画像処理で sth new を試してみました。
次のようなシナリオ: 2 つの白黒画像があります。ここで、両方の画像からすべての白いピクセル (x、y) を取得し、それらを戻り画像に入れたいと考えています。したがって、最終的に私のimage3には、image1とimage2のすべての白いピクセルが含まれています。安全でないポインターは高速であるため、使用しています。
したがって、コードでは、(x,y) の image1 == (x,y) の image2 かどうかを確認します。これは、両方の写真の同じ場所に白いピクセルがある可能性が非常に低いためです。
今の私のアプローチ:
private unsafe static Bitmap combine_img(Bitmap img1, Bitmap img2)
{
Bitmap retBitmap = img1;
int width = img1.Width;
int height = img1.Height;
BitmapData image1 = retBitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData image2 = img2.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); // here an error
byte* scan1 = (byte*)image1.Scan0.ToPointer();
int stride1 = image1.Stride;
byte* scan2 = (byte*)image2.Scan0.ToPointer();
int stride2 = image2.Stride;
for (int y = 0; y < height; y++)
{
byte* row1 = scan1 + (y * stride1);
byte* row2 = scan2 + (y * stride2);
for (int x = 0; x < width; x++)
{
if (row1[x] == row2[x])
row1[x] = 255;
}
}
img1.UnlockBits(image1);
img2.UnlockBits(image2);
return retBitmap;
}
残念ながら、2 番目の画像をロックしようとすると、画像が既にロックされているというエラーが返されます。